Skip to content

Commit 2ee20b3

Browse files
authored
feat: support more cli args (#557)
Adds support and tests for `--algorithm` and `--migrate`
1 parent 1392ccb commit 2ee20b3

File tree

3 files changed

+217
-54
lines changed

3 files changed

+217
-54
lines changed

src/ipfsd-daemon.js

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const execa = require('execa')
88
const { nanoid } = require('nanoid')
99
const path = require('path')
1010
const os = require('os')
11-
const tempWrite = require('temp-write')
12-
const { checkForRunningApi, repoExists, tmpDir, defaultRepo } = require('./utils')
11+
const { checkForRunningApi, repoExists, tmpDir, defaultRepo, buildInitArgs, buildStartArgs } = require('./utils')
1312
const waitFor = require('p-wait-for')
1413

1514
const daemonLog = {
@@ -87,35 +86,23 @@ class Daemon {
8786
return this
8887
}
8988

90-
const opts = merge(
91-
{
92-
emptyRepo: false,
93-
profiles: this.opts.test ? ['test'] : []
94-
},
95-
typeof this.opts.ipfsOptions.init === 'boolean' ? {} : this.opts.ipfsOptions.init,
96-
typeof initOptions === 'boolean' ? {} : initOptions
89+
initOptions = merge({
90+
emptyRepo: false,
91+
profiles: this.opts.test ? ['test'] : []
92+
},
93+
typeof this.opts.ipfsOptions.init === 'boolean' ? {} : this.opts.ipfsOptions.init,
94+
typeof initOptions === 'boolean' ? {} : initOptions
9795
)
9896

99-
const args = ['init']
100-
101-
// default-config only for JS
102-
if (this.opts.ipfsOptions.config && this.opts.type === 'js') {
103-
args.push(tempWrite.sync(JSON.stringify(this.opts.ipfsOptions.config)))
104-
}
97+
const opts = merge(
98+
this.opts, {
99+
ipfsOptions: {
100+
init: initOptions
101+
}
102+
}
103+
)
105104

106-
// Translate ipfs options to cli args
107-
if (opts.bits) {
108-
args.push('--bits', opts.bits)
109-
}
110-
if (opts.pass && this.opts.type === 'js') {
111-
args.push('--pass', '"' + opts.pass + '"')
112-
}
113-
if (opts.emptyRepo) {
114-
args.push('--empty-repo')
115-
}
116-
if (Array.isArray(opts.profiles) && opts.profiles.length) {
117-
args.push('--profile', opts.profiles.join(','))
118-
}
105+
const args = buildInitArgs(opts)
119106

120107
const { stdout, stderr } = await execa(this.exec, args, {
121108
env: this.env
@@ -166,30 +153,7 @@ class Daemon {
166153
} else if (!this.exec) {
167154
throw new Error('No executable specified')
168155
} else {
169-
const args = ['daemon']
170-
const opts = this.opts.ipfsOptions
171-
// add custom args
172-
args.push(...this.opts.args)
173-
174-
if (opts.pass && this.opts.type === 'js') {
175-
args.push('--pass', '"' + opts.pass + '"')
176-
}
177-
178-
if (opts.offline) {
179-
args.push('--offline')
180-
}
181-
182-
if (opts.preload && this.opts.type === 'js') {
183-
args.push('--enable-preload', Boolean(opts.preload.enabled))
184-
}
185-
186-
if (opts.EXPERIMENTAL && opts.EXPERIMENTAL.sharding && this.opts.type === 'js') {
187-
args.push('--enable-sharding-experiment')
188-
}
189-
190-
if (opts.EXPERIMENTAL && opts.EXPERIMENTAL.ipnsPubsub) {
191-
args.push('--enable-namesys-pubsub')
192-
}
156+
const args = buildStartArgs(this.opts)
193157

194158
let output = ''
195159

src/utils.js

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const fs = require('fs-extra')
66
const debug = require('debug')
77
const { nanoid } = require('nanoid')
8+
const tempWrite = require('temp-write')
89

910
const log = debug('ipfsd-ctl:utils')
1011

@@ -46,10 +47,83 @@ const tmpDir = (type = '') => {
4647
return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`)
4748
}
4849

50+
function buildInitArgs (opts = {}) {
51+
const args = ['init']
52+
const ipfsOptions = opts.ipfsOptions || {}
53+
const initOptions = ipfsOptions.init || {}
54+
55+
// default-config only for JS
56+
if (opts.type === 'js') {
57+
if (ipfsOptions.config) {
58+
args.push(tempWrite.sync(JSON.stringify(opts.ipfsOptions.config)))
59+
}
60+
61+
if (initOptions.pass) {
62+
args.push('--pass', '"' + initOptions.pass + '"')
63+
}
64+
}
65+
66+
// Translate IPFS options to cli args
67+
if (initOptions.bits) {
68+
args.push('--bits', initOptions.bits)
69+
}
70+
71+
if (initOptions.algorithm) {
72+
args.push('--algorithm', initOptions.algorithm)
73+
}
74+
75+
if (initOptions.emptyRepo) {
76+
args.push('--empty-repo')
77+
}
78+
79+
if (Array.isArray(initOptions.profiles) && initOptions.profiles.length) {
80+
args.push('--profile', initOptions.profiles.join(','))
81+
}
82+
83+
return args
84+
}
85+
86+
function buildStartArgs (opts = {}) {
87+
const ipfsOptions = opts.ipfsOptions || {}
88+
const customArgs = opts.args || []
89+
90+
const args = ['daemon'].concat(customArgs)
91+
92+
if (opts.type === 'js') {
93+
if (ipfsOptions.pass) {
94+
args.push('--pass', '"' + ipfsOptions.pass + '"')
95+
}
96+
97+
if (ipfsOptions.preload != null) {
98+
args.push('--enable-preload', Boolean(ipfsOptions.preload.enabled))
99+
}
100+
101+
if (ipfsOptions.EXPERIMENTAL && ipfsOptions.EXPERIMENTAL.sharding) {
102+
args.push('--enable-sharding-experiment')
103+
}
104+
}
105+
106+
if (ipfsOptions.offline) {
107+
args.push('--offline')
108+
}
109+
110+
if (ipfsOptions.EXPERIMENTAL && ipfsOptions.EXPERIMENTAL.ipnsPubsub) {
111+
args.push('--enable-namesys-pubsub')
112+
}
113+
114+
if (ipfsOptions.repoAutoMigrate) {
115+
args.push('--migrate')
116+
}
117+
118+
return args
119+
}
120+
49121
module.exports = {
50122
removeRepo,
51123
repoExists,
52124
defaultRepo,
53125
checkForRunningApi,
54-
tmpDir
126+
tmpDir,
127+
buildInitArgs,
128+
buildStartArgs
55129
}

test/node.utils.js

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const { expect } = require('aegir/utils/chai')
66
const os = require('os')
77
const path = require('path')
8-
const { tmpDir, checkForRunningApi, defaultRepo, repoExists, removeRepo } = require('../src/utils')
8+
const { tmpDir, checkForRunningApi, defaultRepo, repoExists, removeRepo, buildInitArgs, buildStartArgs } = require('../src/utils')
99
const { createFactory, createController } = require('../src')
1010

1111
describe('utils node version', function () {
@@ -73,4 +73,129 @@ describe('utils node version', function () {
7373
expect(await repoExists('random')).to.be.false()
7474
})
7575
})
76+
77+
describe('buildStartArgs', function () {
78+
it('custom args', () => {
79+
expect(buildStartArgs({
80+
args: ['--foo=bar']
81+
}).join(' ')).to.include('--foo=bar')
82+
})
83+
84+
it('pass', () => {
85+
expect(buildStartArgs({
86+
type: 'js',
87+
ipfsOptions: {
88+
pass: 'baz'
89+
}
90+
}).join(' ')).to.include('--pass "baz"')
91+
})
92+
93+
it('preload', () => {
94+
expect(buildStartArgs({
95+
type: 'js',
96+
ipfsOptions: {
97+
preload: true
98+
}
99+
}).join(' ')).to.include('--enable-preload')
100+
})
101+
102+
it('preload disabled', () => {
103+
expect(buildStartArgs({
104+
type: 'js',
105+
ipfsOptions: {
106+
preload: false
107+
}
108+
}).join(' ')).to.include('--enable-preload false')
109+
})
110+
111+
it('sharding', () => {
112+
expect(buildStartArgs({
113+
type: 'js',
114+
ipfsOptions: {
115+
EXPERIMENTAL: {
116+
sharding: true
117+
}
118+
}
119+
}).join(' ')).to.include('--enable-sharding-experiment')
120+
})
121+
122+
it('offline', () => {
123+
expect(buildStartArgs({
124+
type: 'js',
125+
ipfsOptions: {
126+
offline: true
127+
}
128+
}).join(' ')).to.include('--offline')
129+
})
130+
131+
it('ipns pubsub', () => {
132+
expect(buildStartArgs({
133+
type: 'js',
134+
ipfsOptions: {
135+
EXPERIMENTAL: {
136+
ipnsPubsub: true
137+
}
138+
}
139+
}).join(' ')).to.include('--enable-namesys-pubsub')
140+
})
141+
142+
it('migrate', () => {
143+
expect(buildStartArgs({
144+
ipfsOptions: {
145+
repoAutoMigrate: true
146+
}
147+
}).join(' ')).to.include('--migrate')
148+
})
149+
})
150+
151+
describe('buildInitArgs', function () {
152+
it('pass', () => {
153+
expect(buildStartArgs({
154+
type: 'js',
155+
ipfsOptions: {
156+
pass: 'baz'
157+
}
158+
}).join(' ')).to.include('--pass "baz"')
159+
})
160+
161+
it('bits', () => {
162+
expect(buildInitArgs({
163+
ipfsOptions: {
164+
init: {
165+
bits: 512
166+
}
167+
}
168+
}).join(' ')).to.include('--bits 512')
169+
})
170+
171+
it('algorithm', () => {
172+
expect(buildInitArgs({
173+
ipfsOptions: {
174+
init: {
175+
algorithm: 'rsa'
176+
}
177+
}
178+
}).join(' ')).to.include('--algorithm rsa')
179+
})
180+
181+
it('empty repo', () => {
182+
expect(buildInitArgs({
183+
ipfsOptions: {
184+
init: {
185+
emptyRepo: true
186+
}
187+
}
188+
}).join(' ')).to.include('--empty-repo')
189+
})
190+
191+
it('profiles', () => {
192+
expect(buildInitArgs({
193+
ipfsOptions: {
194+
init: {
195+
profiles: ['foo', 'bar']
196+
}
197+
}
198+
}).join(' ')).to.include('--profile foo,bar')
199+
})
200+
})
76201
})

0 commit comments

Comments
 (0)