@cryptotaxi247 / kubo / commits / 6989686b8

Implements repository initialization with default config

Modified init command to receive default configuration from stdin. The changes enable us to use existing key-pair, datastore configuration while initializing new ipfs node. License: MIT Signed-off-by: Sivachandran <sivachandran.p@gmail.com>

Siva Chandran committed Apr 19, 2016 at 14:53 UTC 6989686b8beb0b0381e9c3df766cae1d3e232214
2 files changed +85 -7
cmd/ipfs/init.go
+30 -7
@@ -1,6 +1,7 @@
1 package main
2
3 import (
4 + "encoding/json"
5 "errors"
6 "fmt"
7 "io"
@@ -30,7 +31,9 @@ at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable
31 export IPFS_PATH=/path/to/ipfsrepo
32 `,
33 },
33 -
34 + Arguments: []cmds.Argument{
35 + cmds.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
36 + },
37 Options: []cmds.Option{
38 cmds.IntOption("bits", "b", fmt.Sprintf("Number of bits to use in the generated RSA private key (defaults to %d)", nBitsForKeypairDefault)),
39 cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage."),
@@ -77,7 +80,24 @@ at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable
80 nBitsForKeypair = nBitsForKeypairDefault
81 }
82
80 - if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair); err != nil {
83 + var conf *config.Config
84 +
85 + f := req.Files()
86 + if f != nil {
87 + confFile, err := f.NextFile()
88 + if err != nil {
89 + res.SetError(err, cmds.ErrNormal)
90 + return
91 + }
92 +
93 + conf = &config.Config{}
94 + if err := json.NewDecoder(confFile).Decode(conf); err != nil {
95 + res.SetError(err, cmds.ErrNormal)
96 + return
97 + }
98 + }
99 +
100 + if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, conf); err != nil {
101 res.SetError(err, cmds.ErrNormal)
102 return
103 }
@@ -89,10 +109,10 @@ Reinitializing would overwrite your keys.
109 `)
110
111 func initWithDefaults(out io.Writer, repoRoot string) error {
92 - return doInit(out, repoRoot, false, nBitsForKeypairDefault)
112 + return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil)
113 }
114
95 -func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) error {
115 +func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, conf *config.Config) error {
116 if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
117 return err
118 }
@@ -105,9 +125,12 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) err
125 return errRepoExists
126 }
127
108 - conf, err := config.Init(out, nBitsForKeypair)
109 - if err != nil {
110 - return err
128 + if conf == nil {
129 + var err error
130 + conf, err = config.Init(out, nBitsForKeypair)
131 + if err != nil {
132 + return err
133 + }
134 }
135
136 if err := fsrepo.Init(repoRoot, conf); err != nil {
test/sharness/t0022-init-default.sh new
+55
@@ -0,0 +1,55 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2014 Christian Couder
4 +# MIT Licensed; see the LICENSE file in this repository.
5 +#
6 +
7 +test_description="Test init command with default config"
8 +
9 +. lib/test-lib.sh
10 +
11 +cfg_key="Addresses.API"
12 +cfg_val="/ip4/0.0.0.0/tcp/5001"
13 +
14 +# test that init succeeds
15 +test_expect_success "ipfs init succeeds" '
16 + export IPFS_PATH="$(pwd)/.ipfs" &&
17 + echo "IPFS_PATH: \"$IPFS_PATH\"" &&
18 + BITS="2048" &&
19 + ipfs init --bits="$BITS" >actual_init ||
20 + test_fsh cat actual_init
21 +'
22 +
23 +test_expect_success ".ipfs/config has been created" '
24 + test -f "$IPFS_PATH"/config ||
25 + test_fsh ls -al .ipfs
26 +'
27 +
28 +test_expect_success "ipfs config succeeds" '
29 + ipfs config $cfg_flags "$cfg_key" "$cfg_val"
30 +'
31 +
32 +test_expect_success "ipfs read config succeeds" '
33 + IPFS_DEFAULT_CONFIG=$(cat "$IPFS_PATH"/config)
34 +'
35 +
36 +test_expect_success "clean up ipfs dir" '
37 + rm -rf "$IPFS_PATH"
38 +'
39 +
40 +test_expect_success "ipfs init default config succeeds" '
41 + echo $IPFS_DEFAULT_CONFIG | ipfs init >actual_init ||
42 + test_fsh cat actual_init
43 +'
44 +
45 +test_expect_success "ipfs config output looks good" '
46 + echo "$cfg_val" >expected &&
47 + ipfs config "$cfg_key" >actual &&
48 + test_cmp expected actual
49 +'
50 +
51 +test_launch_ipfs_daemon
52 +
53 +test_kill_ipfs_daemon
54 +
55 +test_done