@cryptotaxi247 / kubo / commits / d89492415

refactor(init) return an error

Brian Tiger Chow committed Nov 4, 2014 at 03:36 UTC d894924152104b13ba69ba279a45df6f17be5d2b
1 file changed +21 -28
cmd/ipfs2/init.go
+21 -28
@@ -27,38 +27,39 @@ var initCmd = &cmds.Command{
27 new keypair.
28 `,
29 Run: func(res cmds.Response, req cmds.Request) {
30 - foo(res, req)
30 + err := foo(res, req)
31 + if err != nil {
32 + res.SetError(err, cmds.ErrNormal)
33 + return
34 + }
35 },
36 }
37
34 -func foo(res cmds.Response, req cmds.Request) {
38 +func foo(res cmds.Response, req cmds.Request) error {
39 ctx := req.Context()
40
41 u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
42 filename, err := config.Filename(ctx.ConfigRoot)
43 if err != nil {
40 - res.SetError(errors.New("Couldn't get home directory path"), cmds.ErrNormal)
41 - return
44 + return errors.New("Couldn't get home directory path")
45 }
46
47 arg, found := req.Option("d")
48 dspath, ok := arg.(string)
49 if found && !ok {
47 - res.SetError(errors.New("failed to parse datastore flag"), cmds.ErrNormal)
48 - return
50 + return errors.New("failed to parse datastore flag")
51 }
52
53 fi, err := os.Lstat(filename)
54 arg, found = req.Option("f")
55 force, ok := arg.(bool)
56 if found && !ok {
55 - res.SetError(errors.New("failed to parse force flag"), cmds.ErrNormal)
56 - return
57 + return errors.New("failed to parse force flag")
58 }
59 if fi != nil || (err != nil && !os.IsNotExist(err)) {
60 if !force {
60 - res.SetError(errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)"), cmds.ErrNormal)
61 - return
61 + // TODO multi-line string
62 + return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
63 }
64 }
65 cfg := new(config.Config)
@@ -67,8 +68,7 @@ func foo(res cmds.Response, req cmds.Request) {
68 if len(dspath) == 0 {
69 dspath, err = config.DataStorePath("")
70 if err != nil {
70 - res.SetError(err, cmds.ErrNormal)
71 - return
71 + return err
72 }
73 }
74 cfg.Datastore.Path = dspath
@@ -76,16 +76,14 @@ func foo(res cmds.Response, req cmds.Request) {
76
77 // Construct the data store if missing
78 if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
79 - res.SetError(err, cmds.ErrNormal)
80 - return
79 + return err
80 }
81
82 // Check the directory is writeable
83 if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
84 os.Remove(f.Name())
85 } else {
87 - res.SetError(errors.New("Datastore '"+dspath+"' is not writeable"), cmds.ErrNormal)
88 - return
86 + return errors.New("Datastore '" + dspath + "' is not writeable")
87 }
88
89 cfg.Identity = config.Identity{}
@@ -105,36 +103,31 @@ func foo(res cmds.Response, req cmds.Request) {
103 arg, found = req.Option("b")
104 nbits, ok := arg.(int)
105 if found && !ok {
108 - res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
109 - return
106 + return errors.New("failed to get bits flag")
107 } else if !found {
108 nbits = 4096
109 }
110 if nbits < 1024 {
114 - res.SetError(errors.New("Bitsize less than 1024 is considered unsafe."), cmds.ErrNormal)
115 - return
111 + return errors.New("Bitsize less than 1024 is considered unsafe.")
112 }
113
114 u.POut("generating key pair\n")
115 sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
116 if err != nil {
121 - res.SetError(err, cmds.ErrNormal)
122 - return
117 + return err
118 }
119
120 // currently storing key unencrypted. in the future we need to encrypt it.
121 // TODO(security)
122 skbytes, err := sk.Bytes()
123 if err != nil {
129 - res.SetError(err, cmds.ErrNormal)
130 - return
124 + return err
125 }
126 cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
127
128 id, err := peer.IDFromPubKey(pk)
129 if err != nil {
136 - res.SetError(err, cmds.ErrNormal)
137 - return
130 + return err
131 }
132 cfg.Identity.PeerID = id.Pretty()
133
@@ -155,7 +148,7 @@ func foo(res cmds.Response, req cmds.Request) {
148
149 err = config.WriteConfigFile(filename, cfg)
150 if err != nil {
158 - res.SetError(err, cmds.ErrNormal)
159 - return
151 + return err
152 }
153 + return nil
154 }