refactor(config/init) add hooks to identity generation. useful for displaying messages to the user
@jbenet this commit re-introduces the peer identity line. It's very useful. I understand it may have been removed because of the clashing of the IDs. To alleviate this, this commit places some negative space between the two lines. 'to test' -> 'to get started' as a stronger call to action ``` λ. ipfs2 init -f initializing ipfs node at /Users/btc/.go-ipfs generating key pair...done. peer identity: QmWzjxNEYKjDAxuHJqvtLP1dZTDjreBSUsArWoHai1v9yP to get started, enter: ipfs cat QmYpv2VEsxzTTXRYX3PjDg961cnJE3kY1YDXLycHGQ3zZB ``` License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 16, 2014 at 07:52 UTC
c8c062fe13066ff2d351709db66301887a69efa5
1 file changed
+30
-10
cmd/ipfs2/init.go
+30
-10
@@ -103,10 +103,23 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
103
return nil, err
104
}
105
106
+ err = addTheWelcomeFile(conf, func(k u.Key) {
107
+ fmt.Printf("\nto get started, enter: ipfs cat %s\n", k)
108
+ })
109
+ if err != nil {
110
+ return nil, err
111
+ }
112
+
113
+ return nil, nil
114
+}
115
+
116
+// addTheWelcomeFile adds a file containing the welcome message to the newly
117
+// minted node. On success, it calls onSuccess
118
+func addTheWelcomeFile(conf *config.Config, onSuccess func(u.Key)) error {
119
// TODO extract this file creation operation into a function
120
nd, err := core.NewIpfsNode(conf, false)
121
if err != nil {
109
- return nil, err
122
+ return err
123
}
124
defer nd.Close()
125
@@ -115,15 +128,15 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
128
129
defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
130
if err != nil {
118
- return nil, err
131
+ return err
132
}
133
134
k, err := defnd.Key()
135
if err != nil {
123
- return nil, fmt.Errorf("failed to write test file: %s", err)
136
+ return fmt.Errorf("failed to write test file: %s", err)
137
}
125
- fmt.Printf("done.\nto test, enter: ipfs cat %s\n", k)
126
- return nil, nil
138
+ onSuccess(k)
139
+ return nil
140
}
141
142
func datastoreConfig(dspath string) (config.Datastore, error) {
@@ -152,7 +165,12 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
165
return nil, err
166
}
167
155
- identity, err := identityConfig(nBitsForKeypair)
168
+ identity, err := identityConfig(nBitsForKeypair, func() {
169
+ fmt.Printf("generating key pair...")
170
+ }, func(ident config.Identity) {
171
+ fmt.Printf("done.\n")
172
+ fmt.Printf("peer identity: %s\n", ident.PeerID)
173
+ })
174
if err != nil {
175
return nil, err
176
}
@@ -202,14 +220,17 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
220
return conf, nil
221
}
222
205
-func identityConfig(nbits int) (config.Identity, error) {
223
+// identityConfig initializes a new identity. It calls onBegin when it begins
224
+// to generate the identity and it calls onSuccess once the operation is
225
+// completed successfully
226
+func identityConfig(nbits int, onBegin func(), onSuccess func(config.Identity)) (config.Identity, error) {
227
// TODO guard higher up
228
ident := config.Identity{}
229
if nbits < 1024 {
230
return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
231
}
232
212
- fmt.Println("generating key pair...")
233
+ onBegin()
234
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
235
if err != nil {
236
return ident, err
@@ -228,8 +249,7 @@ func identityConfig(nbits int) (config.Identity, error) {
249
return ident, err
250
}
251
ident.PeerID = id.Pretty()
231
- fmt.Println("peer identity: %s", id.Pretty())
232
-
252
+ onSuccess(ident)
253
return ident, nil
254
}
255