refactor(init) extract bits flag
Brian Tiger Chow committed
Nov 4, 2014 at 03:54 UTC
f3048ab712e335b07169d14dce91fe1ca24a20bf
1 file changed
+14
-11
cmd/ipfs2/init.go
+14
-11
@@ -42,7 +42,16 @@ var initCmd = &cmds.Command{
42
return
43
}
44
45
- err := foo(res, req, dspath, force)
45
+ arg, found = req.Option("b")
46
+ nBitsForKeypair, ok := arg.(int) // TODO param
47
+ if found && !ok {
48
+ res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
49
+ return
50
+ } else if !found {
51
+ nBitsForKeypair = 4096
52
+ }
53
+
54
+ err := foo(res, req, dspath, force, nBitsForKeypair)
55
if err != nil {
56
res.SetError(err, cmds.ErrNormal)
57
return
@@ -50,7 +59,7 @@ var initCmd = &cmds.Command{
59
},
60
}
61
53
-func foo(res cmds.Response, req cmds.Request, dspath string, force bool) error {
62
+func foo(res cmds.Response, req cmds.Request, dspath string, force bool, nBitsForKeypair int) error {
63
ctx := req.Context()
64
65
u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
@@ -104,19 +113,13 @@ func foo(res cmds.Response, req cmds.Request, dspath string, force bool) error {
113
IPNS: "/ipns",
114
}
115
107
- arg, found = req.Option("b")
108
- nbits, ok := arg.(int) // TODO param
109
- if found && !ok {
110
- return errors.New("failed to get bits flag")
111
- } else if !found {
112
- nbits = 4096
113
- }
114
- if nbits < 1024 {
116
+ // TODO guard
117
+ if nBitsForKeypair < 1024 {
118
return errors.New("Bitsize less than 1024 is considered unsafe.")
119
}
120
121
u.POut("generating key pair\n")
119
- sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
122
+ sk, pk, err := ci.GenerateKeyPair(ci.RSA, nBitsForKeypair)
123
if err != nil {
124
return err
125
}