Revert "Revert "Implements repository initialization with default config""
This reverts commit 77ef3913d9f5682c5a0dedbba5488e794e9da1b5. License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Jun 7, 2016 at 20:54 UTC
6822c0c6703407ec11a42a9d24449eb173de0283
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"
@@ -33,7 +34,9 @@ environment variable:
34
export IPFS_PATH=/path/to/ipfsrepo
35
`,
36
},
36
-
37
+ Arguments: []cmds.Argument{
38
+ cmds.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
39
+ },
40
Options: []cmds.Option{
41
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
42
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
@@ -76,7 +79,24 @@ environment variable:
79
return
80
}
81
79
- if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair); err != nil {
82
+ var conf *config.Config
83
+
84
+ f := req.Files()
85
+ if f != nil {
86
+ confFile, err := f.NextFile()
87
+ if err != nil {
88
+ res.SetError(err, cmds.ErrNormal)
89
+ return
90
+ }
91
+
92
+ conf = &config.Config{}
93
+ if err := json.NewDecoder(confFile).Decode(conf); err != nil {
94
+ res.SetError(err, cmds.ErrNormal)
95
+ return
96
+ }
97
+ }
98
+
99
+ if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, conf); err != nil {
100
res.SetError(err, cmds.ErrNormal)
101
return
102
}
@@ -88,10 +108,10 @@ Reinitializing would overwrite your keys.
108
`)
109
110
func initWithDefaults(out io.Writer, repoRoot string) error {
91
- return doInit(out, repoRoot, false, nBitsForKeypairDefault)
111
+ return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil)
112
}
113
94
-func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) error {
114
+func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, conf *config.Config) error {
115
if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
116
return err
117
}
@@ -104,9 +124,12 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) err
124
return errRepoExists
125
}
126
107
- conf, err := config.Init(out, nBitsForKeypair)
108
- if err != nil {
109
- return err
127
+ if conf == nil {
128
+ var err error
129
+ conf, err = config.Init(out, nBitsForKeypair)
130
+ if err != nil {
131
+ return err
132
+ }
133
}
134
135
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