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

Commit

Permalink
feat(object): add template option to object.new
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 12, 2016
1 parent 1af30c1 commit 9058118
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
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
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

0 comments on commit 9058118

Please sign in to comment.