From c8c062fe13066ff2d351709db66301887a69efa5 Mon Sep 17 00:00:00 2001
From: Brian Tiger Chow <brian.holderchow@gmail.com>
Date: Sun, 16 Nov 2014 07:52:36 -0800
Subject: [PATCH] refactor(config/init) add hooks to identity generation.
 useful for displaying messages to the user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

@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>
---
 cmd/ipfs2/init.go | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/cmd/ipfs2/init.go b/cmd/ipfs2/init.go
index 08afa1786..9be58f6c1 100644
--- a/cmd/ipfs2/init.go
+++ b/cmd/ipfs2/init.go
@@ -103,10 +103,23 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
 		return nil, err
 	}
 
+	err = addTheWelcomeFile(conf, func(k u.Key) {
+		fmt.Printf("\nto get started, enter: ipfs cat %s\n", k)
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+// addTheWelcomeFile adds a file containing the welcome message to the newly
+// minted node. On success, it calls onSuccess
+func addTheWelcomeFile(conf *config.Config, onSuccess func(u.Key)) error {
 	// TODO extract this file creation operation into a function
 	nd, err := core.NewIpfsNode(conf, false)
 	if err != nil {
-		return nil, err
+		return err
 	}
 	defer nd.Close()
 
@@ -115,15 +128,15 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
 
 	defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
 	if err != nil {
-		return nil, err
+		return err
 	}
 
 	k, err := defnd.Key()
 	if err != nil {
-		return nil, fmt.Errorf("failed to write test file: %s", err)
+		return fmt.Errorf("failed to write test file: %s", err)
 	}
-	fmt.Printf("done.\nto test, enter: ipfs cat %s\n", k)
-	return nil, nil
+	onSuccess(k)
+	return nil
 }
 
 func datastoreConfig(dspath string) (config.Datastore, error) {
@@ -152,7 +165,12 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
 		return nil, err
 	}
 
-	identity, err := identityConfig(nBitsForKeypair)
+	identity, err := identityConfig(nBitsForKeypair, func() {
+		fmt.Printf("generating key pair...")
+	}, func(ident config.Identity) {
+		fmt.Printf("done.\n")
+		fmt.Printf("peer identity: %s\n", ident.PeerID)
+	})
 	if err != nil {
 		return nil, err
 	}
@@ -202,14 +220,17 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
 	return conf, nil
 }
 
-func identityConfig(nbits int) (config.Identity, error) {
+// identityConfig initializes a new identity. It calls onBegin when it begins
+// to generate the identity and it calls onSuccess once the operation is
+// completed successfully
+func identityConfig(nbits int, onBegin func(), onSuccess func(config.Identity)) (config.Identity, error) {
 	// TODO guard higher up
 	ident := config.Identity{}
 	if nbits < 1024 {
 		return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
 	}
 
-	fmt.Println("generating key pair...")
+	onBegin()
 	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
 	if err != nil {
 		return ident, err
@@ -228,8 +249,7 @@ func identityConfig(nbits int) (config.Identity, error) {
 		return ident, err
 	}
 	ident.PeerID = id.Pretty()
-	fmt.Println("peer identity: %s", id.Pretty())
-
+	onSuccess(ident)
 	return ident, nil
 }
 
-- 
GitLab