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

Commit 18f3a19

Browse files
dirkmcAlan Shaw
authored and
Alan Shaw
committed
feat: ipfs init --profile option
1 parent f4e6b9b commit 18f3a19

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/cli/commands/init.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ const { ipfsPathHelp } = require('../utils')
44

55
module.exports = {
66
command: 'init [config] [options]',
7-
describe: 'Initialize a local IPFS node',
7+
describe: 'Initialize a local IPFS node\n\n' +
8+
'If you are going to run IPFS in a server environment, you may want to ' +
9+
`initialize it using the 'server' profile.\n\n` +
10+
'For the list of available profiles run `jsipfs config profile ls`',
811
builder (yargs) {
912
return yargs
1013
.epilog(ipfsPathHelp)
1114
.positional('config', {
12-
describe: 'Node config, this should JSON and will be merged with the default config. Check https://github.com/ipfs/js-ipfs#optionsconfig',
15+
describe: 'Node config, this should be JSON and will be merged with the default config. See https://github.com/ipfs/js-ipfs#optionsconfig',
1316
type: 'string'
1417
})
1518
.option('bits', {
@@ -28,6 +31,11 @@ module.exports = {
2831
type: 'string',
2932
describe: 'Pre-generated private key to use for the repo'
3033
})
34+
.option('profile', {
35+
alias: 'p',
36+
type: 'string',
37+
describe: `Apply profile settings to config. Multiple profiles can be separated by ','`
38+
})
3139
},
3240

3341
handler (argv) {
@@ -52,6 +60,7 @@ module.exports = {
5260
bits: argv.bits,
5361
privateKey: argv.privateKey,
5462
emptyRepo: argv.emptyRepo,
63+
profile: argv.profile,
5564
pass: argv.pass,
5665
log: argv.print
5766
})

src/core/components/init.js

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const IPNS = require('../ipns')
1717
const OfflineDatastore = require('../ipns/routing/offline-datastore')
1818

1919
const addDefaultAssets = require('./init-assets')
20+
const { profiles } = require('./config')
2021

2122
module.exports = function init (self) {
2223
return promisify((opts, callback) => {
@@ -61,6 +62,21 @@ module.exports = function init (self) {
6162
opts.log = opts.log || function () {}
6263

6364
const config = mergeOptions(defaultConfig(), self._options.config)
65+
66+
// Apply profiles (eg "server,lowpower") to config
67+
if (opts.profile) {
68+
const profileNames = opts.profile.split(',')
69+
for (const profileName of profileNames) {
70+
const profile = profiles.find(p => p.name === profileName)
71+
if (!profile) {
72+
return done(new Error(`Could not find profile with name '${profileName}'`))
73+
}
74+
75+
self.log(`applying profile ${profileName}`)
76+
profile.transform(config)
77+
}
78+
}
79+
6480
let privateKey
6581

6682
waterfall([

test/cli/init.js

+30
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ describe('init', function () {
2525
return !f.startsWith('.')
2626
})
2727
}
28+
29+
const repoConfSync = (p) => {
30+
return JSON.parse(fs.readFileSync(path.join(repoPath, 'config')))
31+
}
32+
2833
beforeEach(() => {
2934
repoPath = os.tmpdir() + '/ipfs-' + hat()
3035
ipfs = ipfsExec(repoPath)
@@ -67,6 +72,31 @@ describe('init', function () {
6772
})
6873
})
6974

75+
it('profile', async function () {
76+
this.timeout(40 * 1000)
77+
78+
await ipfs('init --profile lowpower')
79+
expect(repoConfSync().Swarm.ConnMgr.LowWater).to.equal(20)
80+
})
81+
82+
it('profile multiple', async function () {
83+
this.timeout(40 * 1000)
84+
85+
await ipfs('init --profile server,lowpower')
86+
expect(repoConfSync().Discovery.MDNS.Enabled).to.equal(false)
87+
expect(repoConfSync().Swarm.ConnMgr.LowWater).to.equal(20)
88+
})
89+
90+
it('profile non-existent', async function () {
91+
this.timeout(40 * 1000)
92+
93+
try {
94+
await ipfs('init --profile doesnt-exist')
95+
} catch (err) {
96+
expect(err.stdout).includes('Could not find profile')
97+
}
98+
})
99+
70100
it('should present ipfs path help when option help is received', function (done) {
71101
this.timeout(100 * 1000)
72102

0 commit comments

Comments
 (0)