@@ -7,27 +7,33 @@ import (
7
7
"fmt"
8
8
"io"
9
9
"os"
10
- "path"
10
+ "path/filepath "
11
11
"strings"
12
12
13
+ cserial "github.com/ipfs/go-ipfs-config/serialize"
13
14
assets "github.com/ipfs/go-ipfs/assets"
14
15
oldcmds "github.com/ipfs/go-ipfs/commands"
15
16
core "github.com/ipfs/go-ipfs/core"
16
17
namesys "github.com/ipfs/go-ipfs/namesys"
17
18
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18
19
19
- "github.com/ipfs/go-ipfs-cmds"
20
- "github.com/ipfs/go-ipfs-config"
21
- "github.com/ipfs/go-ipfs-files"
20
+ cmds "github.com/ipfs/go-ipfs-cmds"
21
+ config "github.com/ipfs/go-ipfs-config"
22
+ files "github.com/ipfs/go-ipfs-files"
22
23
)
23
24
24
25
const (
25
26
nBitsForKeypairDefault = 2048
26
27
bitsOptionName = "bits"
27
28
emptyRepoOptionName = "empty-repo"
28
29
profileOptionName = "profile"
30
+ configOptionName = "config-file"
29
31
)
30
32
33
+ var errRepoExists = errors .New (`ipfs configuration file already exists!
34
+ Reinitializing would overwrite your keys.
35
+ ` )
36
+
31
37
var initCmd = & cmds.Command {
32
38
Helptext : cmds.HelpText {
33
39
Tagline : "Initializes ipfs config file." ,
@@ -53,6 +59,7 @@ environment variable:
53
59
cmds .IntOption (bitsOptionName , "b" , "Number of bits to use in the generated RSA private key." ).WithDefault (nBitsForKeypairDefault ),
54
60
cmds .BoolOption (emptyRepoOptionName , "e" , "Don't add and pin help files to the local storage." ),
55
61
cmds .StringOption (profileOptionName , "p" , "Apply profile settings to config. Multiple profiles can be separated by ','" ),
62
+ cmds .StringOption (configOptionName , "Use supplied config as a base instead of the default" ),
56
63
57
64
// TODO need to decide whether to expose the override as a file or a
58
65
// directory. That is: should we allow the user to also specify the
@@ -102,31 +109,38 @@ environment variable:
102
109
}
103
110
}
104
111
105
- profile , _ := req .Options [profileOptionName ].(string )
106
-
107
- var profiles []string
108
- if profile != "" {
109
- profiles = strings .Split (profile , "," )
112
+ cfgLocation , _ := req .Options [configOptionName ].(string )
113
+ profiles , _ := req .Options [profileOptionName ].(string )
114
+ if cfgLocation != "" {
115
+ var err error
116
+ if conf , err = cserial .Load (cfgLocation ); err != nil {
117
+ return err
118
+ }
110
119
}
111
120
112
121
return doInit (os .Stdout , cctx .ConfigRoot , empty , nBitsForKeypair , profiles , conf )
113
122
},
114
123
}
115
124
116
- var errRepoExists = errors .New (`ipfs configuration file already exists!
117
- Reinitializing would overwrite your keys.
118
- ` )
119
-
120
- func initWithDefaults (out io.Writer , repoRoot string , profile string ) error {
121
- var profiles []string
122
- if profile != "" {
123
- profiles = strings .Split (profile , "," )
125
+ func applyProfiles (conf * config.Config , profiles string ) error {
126
+ if profiles == "" {
127
+ return nil
124
128
}
125
129
126
- return doInit (out , repoRoot , false , nBitsForKeypairDefault , profiles , nil )
130
+ for _ , profile := range strings .Split (profiles , "," ) {
131
+ transformer , ok := config .Profiles [profile ]
132
+ if ! ok {
133
+ return fmt .Errorf ("invalid configuration profile: %s" , profile )
134
+ }
135
+
136
+ if err := transformer .Transform (conf ); err != nil {
137
+ return err
138
+ }
139
+ }
140
+ return nil
127
141
}
128
142
129
- func doInit (out io.Writer , repoRoot string , empty bool , nBitsForKeypair int , confProfiles [] string , conf * config.Config ) error {
143
+ func doInit (out io.Writer , repoRoot string , empty bool , nBitsForKeypair int , confProfiles string , conf * config.Config ) error {
130
144
if _ , err := fmt .Fprintf (out , "initializing IPFS node at %s\n " , repoRoot ); err != nil {
131
145
return err
132
146
}
@@ -147,15 +161,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
147
161
}
148
162
}
149
163
150
- for _ , profile := range confProfiles {
151
- transformer , ok := config .Profiles [profile ]
152
- if ! ok {
153
- return fmt .Errorf ("invalid configuration profile: %s" , profile )
154
- }
155
-
156
- if err := transformer .Transform (conf ); err != nil {
157
- return err
158
- }
164
+ if err := applyProfiles (conf , confProfiles ); err != nil {
165
+ return err
159
166
}
160
167
161
168
if err := fsrepo .Init (repoRoot , conf ); err != nil {
@@ -175,7 +182,7 @@ func checkWritable(dir string) error {
175
182
_ , err := os .Stat (dir )
176
183
if err == nil {
177
184
// dir exists, make sure we can write to it
178
- testfile := path .Join (dir , "test" )
185
+ testfile := filepath .Join (dir , "test" )
179
186
fi , err := os .Create (testfile )
180
187
if err != nil {
181
188
if os .IsPermission (err ) {
0 commit comments