fix: do not create and print out an unused new peerID when initializing from a config
Adin Schmahmann committed
Oct 19, 2020 at 18:24 UTC
91c1aaa1b5a87eefa6ba1da904288de447e62d08
2 files changed
+34
-34
cmd/ipfs/daemon.go
+12
-6
@@ -250,14 +250,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
250
}
251
}
252
253
- identity, err := config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
254
- options.Key.Type(algorithmDefault),
255
- })
256
- if err != nil {
257
- return err
253
+ if conf == nil {
254
+ identity, err := config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
255
+ options.Key.Type(algorithmDefault),
256
+ })
257
+ if err != nil {
258
+ return err
259
+ }
260
+ conf, err = config.InitWithIdentity(identity)
261
+ if err != nil {
262
+ return err
263
+ }
264
}
265
260
- if err = doInit(os.Stdout, cctx.ConfigRoot, false, &identity, profiles, conf); err != nil {
266
+ if err = doInit(os.Stdout, cctx.ConfigRoot, false, profiles, conf); err != nil {
267
return err
268
}
269
}
cmd/ipfs/init.go
+22
-28
@@ -113,24 +113,30 @@ environment variable:
113
}
114
}
115
116
- var err error
117
- var identity config.Identity
118
- if nBitsGiven {
119
- identity, err = config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
120
- options.Key.Size(nBitsForKeypair),
121
- options.Key.Type(algorithm),
122
- })
123
- } else {
124
- identity, err = config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
125
- options.Key.Type(algorithm),
126
- })
127
- }
128
- if err != nil {
129
- return err
116
+ if conf == nil {
117
+ var err error
118
+ var identity config.Identity
119
+ if nBitsGiven {
120
+ identity, err = config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
121
+ options.Key.Size(nBitsForKeypair),
122
+ options.Key.Type(algorithm),
123
+ })
124
+ } else {
125
+ identity, err = config.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
126
+ options.Key.Type(algorithm),
127
+ })
128
+ }
129
+ if err != nil {
130
+ return err
131
+ }
132
+ conf, err = config.InitWithIdentity(identity)
133
+ if err != nil {
134
+ return err
135
+ }
136
}
137
138
profiles, _ := req.Options[profileOptionName].(string)
133
- return doInit(os.Stdout, cctx.ConfigRoot, empty, &identity, profiles, conf)
139
+ return doInit(os.Stdout, cctx.ConfigRoot, empty, profiles, conf)
140
},
141
}
142
@@ -152,7 +158,7 @@ func applyProfiles(conf *config.Config, profiles string) error {
158
return nil
159
}
160
155
-func doInit(out io.Writer, repoRoot string, empty bool, identity *config.Identity, confProfiles string, conf *config.Config) error {
161
+func doInit(out io.Writer, repoRoot string, empty bool, confProfiles string, conf *config.Config) error {
162
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
163
return err
164
}
@@ -165,18 +171,6 @@ func doInit(out io.Writer, repoRoot string, empty bool, identity *config.Identit
171
return errRepoExists
172
}
173
168
- if identity == nil {
169
- return fmt.Errorf("No Identity provided for initialization")
170
- }
171
-
172
- if conf == nil {
173
- var err error
174
- conf, err = config.InitWithIdentity(*identity)
175
- if err != nil {
176
- return err
177
- }
178
- }
179
-
174
if err := applyProfiles(conf, confProfiles); err != nil {
175
return err
176
}