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

Commit

Permalink
Implement cli pin add and offline test
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamStone committed Jun 5, 2016
1 parent 742f341 commit abed0fb
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/cli/commands/pin/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:pin')
log.error = debug('cli:pin:error')
const bs58 = require('bs58')

module.exports = Command.extend({
desc: 'Pins objects to local storage.',

options: {
// Recursively pin the object linked to by the specified object(s)
recursive: {
alias: 'r',
type: 'boolean',
default: true
}
},

run: (recursive, path) => {
utils.getIPFS((err, ipfs) => {
if (err) {
console.error(err)
throw err
}
const matched = path.match(/^(?:\/ipfs\/)?([^\/]+(?:\/[^\/]+)*)\/?$/)
if (!matched) {
err = new Error('invalid ipfs ref path')
console.error(err)
throw err
}
const split = matched[1].split('/')
const rootHash = split[0]
const key = new Buffer(bs58.decode(rootHash))
const links = split.slice(1, split.length)
const pathFn = (err, obj) => {
if (err) {
console.error(err)
throw err
}
if (links.length) {
const linkName = links.shift()
const nextLink = obj.links.filter((link) => {
return (link.name === linkName)
})
if (!nextLink.length) {
err = new Error(
'pin: no link named ' + linkName +
' under ' + obj.toJSON().Hash
)
console.error(err)
throw err
}
const nextHash = nextLink[0].hash
ipfs.object.get(nextHash, pathFn)
} else {
ipfs.pinner.pin(obj, recursive, (err) => {
if (err) {
console.error(err)
throw err
}
const mode = recursive ? ' recursively' : ' directly'
console.log('pinned ' + obj.toJSON().Hash + mode)
})
}
}

ipfs.object.get(key, pathFn)
})
}
})
58 changes: 58 additions & 0 deletions test/cli/test-pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const nexpect = require('nexpect')
const repoPath = require('./index').repoPath
const _ = require('lodash')

// use a tree of ipfs objects for recursive tests:
// root
// |`leaf
// `branch
// `subLeaf

const filenames = [
'root.json', 'leaf.json', 'branch.json', 'subLeaf.json'
]
const keys = {
'root.json': 'QmWQwS2Xh1SFGMPzUVYQ52b7RC7fTfiaPHm3ZyTRZuHmer',
'leaf.json': 'QmaZoTQ6wFe7EtvaePBUeXavfeRqCAq3RUMomFxBpZLrLA',
'branch.json': 'QmNxjjP7dtx6pzxWGBRCrgmjX3JqKL7uF2Kjx7ExiZDbSB',
'subLeaf.json': 'QmUzzznkyQL7FjjBztG3D1tTjBuxeArLceDZnuSowUggXL'
}
const rootHash = keys['root.json']

describe('pin', function () {
this.timeout(20 * 1000)
const env = _.clone(process.env)
env.IPFS_PATH = repoPath
const opts = {env: env, stream: 'all'}
const bin = process.cwd() + '/src/cli/bin.js'
const filesDir = process.cwd() + '/test/test-data/tree/'

before((done) => {
var doneCount = 0
filenames.forEach((filename) => {
const hash = keys[filename]
nexpect.spawn('node', [bin, 'object', 'put', filesDir + filename], opts)
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(stdout[0]).to.equal('added ' + hash)
doneCount === filenames.length - 1 ? done() : doneCount++
})
})
})

describe('api offline', () => {
it('add (recursively by default)', (done) => {
nexpect.spawn('node', [bin, 'pin', 'add', rootHash], opts)
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout[0]).to.equal('pinned ' + rootHash + ' recursively')
done()
})
})
})
})
1 change: 1 addition & 0 deletions test/test-data/tree/branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Links":[{"Name":"subLeaf","Hash":"QmUzzznkyQL7FjjBztG3D1tTjBuxeArLceDZnuSowUggXL","Size":15}],"Data":"\u0008\u0001"}
1 change: 1 addition & 0 deletions test/test-data/tree/leaf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Links":[],"Data":"\u0008\u0002\u0012\u0004leaf\u0018\u0004"}
1 change: 1 addition & 0 deletions test/test-data/tree/root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Links":[{"Name":"leaf","Hash":"QmaZoTQ6wFe7EtvaePBUeXavfeRqCAq3RUMomFxBpZLrLA","Size":12},{"Name":"branch","Hash":"QmNxjjP7dtx6pzxWGBRCrgmjX3JqKL7uF2Kjx7ExiZDbSB","Size":68}],"Data":"\u0008\u0001"}
1 change: 1 addition & 0 deletions test/test-data/tree/subLeaf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Links":[],"Data":"\u0008\u0002\u0012\u0007subLeaf\u0018\u0007"}

0 comments on commit abed0fb

Please sign in to comment.