Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Feat/object template #661

Merged
merged 3 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"form-data": "^2.1.2",
"fs-pull-blob-store": "^0.4.1",
"gulp": "^3.9.1",
"interface-ipfs-core": "^0.22.0",
"interface-ipfs-core": "^0.22.1",
"left-pad": "^1.1.3",
"lodash": "^4.17.2",
"ncp": "^2.0.0",
Expand All @@ -80,7 +80,7 @@
"hapi": "^16.0.1",
"hapi-set-header": "^1.0.2",
"idb-pull-blob-store": "^0.5.1",
"ipfs-api": "^12.0.3",
"ipfs-api": "^12.1.0",
"ipfs-bitswap": "^0.8.2",
"ipfs-block": "^0.5.1",
"ipfs-block-service": "^0.7.1",
Expand Down Expand Up @@ -150,4 +150,4 @@
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/object/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = {
command: 'new',
command: 'new [<template>]',

describe: 'Create new ipfs objects',

Expand All @@ -16,7 +16,7 @@ module.exports = {
handler (argv) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.new(cb)
(ipfs, cb) => ipfs.object.new(argv.template, cb)
], (err, node) => {
if (err) {
throw err
Expand Down
24 changes: 21 additions & 3 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const peerId = require('peer-id')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')

const addDefaultAssets = require('./init-assets')

Expand Down Expand Up @@ -51,11 +52,28 @@ module.exports = function init (self) {
},
(cb) => self._repo.config.set(config, cb),
(cb) => {
if (typeof addDefaultAssets === 'function' && !opts.emptyRepo) {
return addDefaultAssets(self, opts.log, cb)
if (opts.emptyRepo) {
return cb(null, true)
}

cb(null, true)
const tasks = [
// add empty unixfs dir object (go-ipfs assumes this exists)
(cb) => self.object.new('unixfs-dir', cb)
]

if (typeof addDefaultAssets === 'function') {
tasks.push(
(cb) => addDefaultAssets(self, opts.log, cb)
)
}

parallel(tasks, (err) => {
if (err) {
return cb(err)
}

cb(null, true)
})
}
], callback)
}
Expand Down
20 changes: 18 additions & 2 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const CID = require('cids')
const mh = require('multihashes')
const Unixfs = require('ipfs-unixfs')
const assert = require('assert')

function normalizeMultihash (multihash, enc) {
if (typeof multihash === 'string') {
Expand Down Expand Up @@ -91,8 +93,22 @@ module.exports = function object (self) {
}

return {
new: promisify((callback) => {
DAGNode.create(new Buffer(0), (err, node) => {
new: promisify((template, callback) => {
if (typeof template === 'function') {
callback = template
template = undefined
}

let data

if (template) {
assert(template === 'unixfs-dir', 'unkown template')
data = (new Unixfs('directory')).marshal()
} else {
data = new Buffer(0)
}

DAGNode.create(data, (err, node) => {
if (err) {
return callback(err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/http-api/resources/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ exports.parseKey = (request, reply) => {

exports.new = (request, reply) => {
const ipfs = request.server.app.ipfs
const template = request.query.arg

ipfs.object.new((err, node) => {
ipfs.object.new(template, (err, node) => {
if (err) {
log.error(err)
return reply({
Expand Down
8 changes: 8 additions & 0 deletions test/cli/test-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('object', () => {
})
})

it('new unixfs-dir', () => {
return ipfs('object new unixfs-dir').then((out) => {
expect(out).to.be.eql(
'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
)
})
})

it('get', () => {
return ipfs('object get QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n').then((out) => {
const result = JSON.parse(out)
Expand Down