Skip to content

Commit

Permalink
mcclient get/add/remove commands for manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
yusefnapora committed Jan 12, 2017
1 parent 0ce123e commit 7464300
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/client/api/RestClient.js
Expand Up @@ -336,6 +336,27 @@ class RestClient {
.then(r => r.json())
}

getNodeManifest (): Promise<string> {
return this.getRequest('manifest/node')
.then(trimTextResponse)
}

getManifests (peerId: ?string = null): Promise<Array<Object>> {
let path = (peerId == null)
? 'manifest'
: `manifest/${peerId}`

return this.getRequest(path)
.then(r => new NDJsonResponse(r))
.then(r => r.values())
}

setManifests (...manifests: Array<Object>): Promise<boolean> {
const body = manifests.map(m => JSON.stringify(m)).join('\n')
return this.postRequest('manifest', body, false)
.then(parseBoolResponse)
}

shutdown (): Promise<boolean> {
return this.postRequest('shutdown', '', false)
.then(() => true)
Expand Down
1 change: 0 additions & 1 deletion src/client/cli/commands/config/manifest.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/client/cli/commands/manifest.js
@@ -0,0 +1,15 @@
// @flow

const path = require('path')

module.exports = {
command: 'manifest <subcommand>',
describe: 'Commands for setting and retrieving identity manifests. Use "manifest --help" to see subcommands.\n',
builder: (yargs: Function) => {
return yargs
.commandDir(path.join(__dirname, './manifest'))
.help()
.strict()
},
handler: () => {}
}
44 changes: 44 additions & 0 deletions src/client/cli/commands/manifest/add.js
@@ -0,0 +1,44 @@
// @flow

const fs = require('fs')
const _ = require('lodash')
const RestClient = require('../../../api/RestClient')
const { subcommand, println } = require('../../util')
const { consumeStream } = require('../../../../common/util')

module.exports = {
command: 'add [filename]',
description: 'Add a signed manifest to the local node. ' +
'If `filename` is not given, will read from standard input.\n',
handler: subcommand((opts: {client: RestClient, filename?: string}) => {
const {client, filename} = opts
let streamName = 'standard input'
let inputStream = process.stdin
if (filename != null) {
streamName = filename
inputStream = fs.createReadStream(filename)
}

let manifest: Object

return consumeStream(inputStream)
.catch(err => {
throw new Error(`Error reading from ${streamName}: ${err.message}`)
})
.then(contents => {
manifest = JSON.parse(contents)
})
.then(() => client.getManifests())
.then(manifests => {
if (_.some(manifests, m => _.isEqual(m, manifest))) {
println('Node already contains manifest, ignoring')
return
}
manifests.push(manifest)
return client.setManifests(...manifests)
.then(() => {
println('Manifest added successfully')
})
})
})
}
14 changes: 14 additions & 0 deletions src/client/cli/commands/manifest/get.js
@@ -0,0 +1,14 @@
// @flow

const RestClient = require('../../../api/RestClient')
const { subcommand, printJSON } = require('../../util')

module.exports = {
command: 'get [remotePeer]',
description: `Get the signed manifests for the local node or a remote peer.\n`,
handler: subcommand((opts: {client: RestClient, remotePeer?: string}) => {
const {client, remotePeer} = opts
return client.getManifests(remotePeer)
.then(m => printJSON(m))
})
}
14 changes: 14 additions & 0 deletions src/client/cli/commands/manifest/node.js
@@ -0,0 +1,14 @@
// @flow

const RestClient = require('../../../api/RestClient')
const { subcommand, println } = require('../../util')

module.exports = {
command: 'node',
description: `Get the "node manifest" for the local node, suitable for signing by mcid to produce a manifest.\n`,
handler: subcommand((opts: {client: RestClient}) => {
const {client} = opts
return client.getNodeManifest()
.then(m => println(m))
})
}
44 changes: 44 additions & 0 deletions src/client/cli/commands/manifest/remove.js
@@ -0,0 +1,44 @@
// @flow

const fs = require('fs')
const _ = require('lodash')
const RestClient = require('../../../api/RestClient')
const { subcommand, println } = require('../../util')
const { consumeStream } = require('../../../../common/util')

module.exports = {
command: 'remove [filename]',
description: 'Remove a signed manifest from the local node. ' +
'If `filename` is not given, will read from standard input.\n',
handler: subcommand((opts: {client: RestClient, filename?: string}) => {
const {client, filename} = opts
let streamName = 'standard input'
let inputStream = process.stdin
if (filename != null) {
streamName = filename
inputStream = fs.createReadStream(filename)
}

let manifest: Object

return consumeStream(inputStream)
.catch(err => {
throw new Error(`Error reading from ${streamName}: ${err.message}`)
})
.then(contents => {
manifest = JSON.parse(contents)
})
.then(() => client.getManifests())
.then(manifests => {
const without = _.filter(manifests, m => !_.isEqual(m, manifest))
if (without.length === manifests.length) {
println('Node does not contain manifest, ignoring')
return
}
return client.setManifests(...without)
.then(() => {
println('Manifest removed successfully')
})
})
})
}
21 changes: 19 additions & 2 deletions src/common/util.js
@@ -1,7 +1,7 @@
// @flow

const Multihashing = require('multihashing')
import type { Writable } from 'stream'
import type { Writable, Readable } from 'stream'
import type { WriteStream } from 'tty'

/**
Expand Down Expand Up @@ -101,6 +101,22 @@ function printlnErr (output: string) {
writeln(output, process.stderr)
}
/**
* Read a stream until it ends, returning its contents as a string.
* @param stream
* @returns {Promise}
*/
function consumeStream (stream: Readable): Promise<string> {
return new Promise((resolve, reject) => {
const chunks = []
stream.on('error', err => reject(err))
stream.on('data', chunk => { chunks.push(chunk) })
stream.on('end', () => {
resolve(Buffer.concat(chunks).toString('utf-8'))
})
})
}

module.exports = {
promiseHash,
promiseTimeout,
Expand All @@ -109,5 +125,6 @@ module.exports = {
isB58Multihash,
writeln,
println,
printlnErr
printlnErr,
consumeStream
}

0 comments on commit 7464300

Please sign in to comment.