Skip to content

Commit 73615a8

Browse files
committed
use profile transforms on init config-file
1 parent f1f726c commit 73615a8

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

cmd/ipfs/daemon.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,23 +237,19 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
237237
profiles, _ := req.Options[initProfileOptionKwd].(string)
238238

239239
if cfgLocation != "" {
240-
if profiles != "" {
241-
return errInitConfigArgs
242-
}
243-
244240
conf, err := cserial.Load(cfgLocation)
245241
if err != nil {
246242
return err
247243
}
248244

249-
if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf); err != nil {
245+
if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil {
250246
return err
251247
}
252248
} else {
253249
cfgLocation = cctx.ConfigRoot
254250

255251
if !fsrepo.IsInitialized(cfgLocation) {
256-
err := initWithDefaults(os.Stdout, cfgLocation, profiles)
252+
err := doInit(os.Stdout, cfgLocation, false, nBitsForKeypairDefault, profiles, nil)
257253
if err != nil {
258254
return err
259255
}

cmd/ipfs/init.go

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ const (
3030
configOptionName = "config-file"
3131
)
3232

33-
var (
34-
errRepoExists = errors.New(`ipfs configuration file already exists!
33+
var errRepoExists = errors.New(`ipfs configuration file already exists!
3534
Reinitializing would overwrite your keys.
3635
`)
37-
errInitConfigArgs = errors.New("Config file <-> Profile merging not implemented")
38-
)
3936

4037
var initCmd = &cmds.Command{
4138
Helptext: cmds.HelpText{
@@ -113,40 +110,40 @@ environment variable:
113110
}
114111

115112
cfgLocation, _ := req.Options[configOptionName].(string)
116-
profile, _ := req.Options[profileOptionName].(string)
113+
profiles, _ := req.Options[profileOptionName].(string)
117114

118115
if cfgLocation != "" {
119-
if profile != "" {
120-
return errInitConfigArgs
121-
}
122-
123116
conf, err := cserial.Load(cfgLocation)
124117
if err != nil {
125118
return err
126119
}
127120

128-
return doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf)
129-
}
130-
131-
var profiles []string
132-
if profile != "" {
133-
profiles = strings.Split(profile, ",")
121+
return doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf)
134122
}
135123

136124
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
137125
},
138126
}
139127

140-
func initWithDefaults(out io.Writer, repoRoot string, profile string) error {
141-
var profiles []string
142-
if profile != "" {
143-
profiles = strings.Split(profile, ",")
128+
func applyProfiles(conf *config.Config, profiles string) error {
129+
if profiles == "" {
130+
return nil
144131
}
145132

146-
return doInit(out, repoRoot, false, nBitsForKeypairDefault, profiles, nil)
133+
for _, profile := range strings.Split(profiles, ",") {
134+
transformer, ok := config.Profiles[profile]
135+
if !ok {
136+
return fmt.Errorf("invalid configuration profile: %s", profile)
137+
}
138+
139+
if err := transformer.Transform(conf); err != nil {
140+
return err
141+
}
142+
}
143+
return nil
147144
}
148145

149-
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error {
146+
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config) error {
150147
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
151148
return err
152149
}
@@ -167,15 +164,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
167164
}
168165
}
169166

170-
for _, profile := range confProfiles {
171-
transformer, ok := config.Profiles[profile]
172-
if !ok {
173-
return fmt.Errorf("invalid configuration profile: %s", profile)
174-
}
175-
176-
if err := transformer.Transform(conf); err != nil {
177-
return err
178-
}
167+
if err := applyProfiles(conf, confProfiles); err != nil {
168+
return err
179169
}
180170

181171
if err := fsrepo.Init(repoRoot, conf); err != nil {

0 commit comments

Comments
 (0)