@cryptotaxi247 / kubo / commits / faf6454df

refactor(init) extract method

Brian Tiger Chow committed Nov 4, 2014 at 03:33 UTC faf6454df6691b81156486727e3f8818ef8c9884
1 file changed +126 -122
cmd/ipfs2/init.go
+126 -122
@@ -23,135 +23,139 @@ var initCmd = &cmds.Command{
23 },
24 Help: `ipfs init
25
26 - Initializes ipfs configuration files and generates a
27 - new keypair.
28 -`,
26 + Initializes ipfs configuration files and generates a
27 + new keypair.
28 + `,
29 Run: func(res cmds.Response, req cmds.Request) {
30 - ctx := req.Context()
31 -
32 - u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
33 - filename, err := config.Filename(ctx.ConfigRoot)
34 - if err != nil {
35 - res.SetError(errors.New("Couldn't get home directory path"), cmds.ErrNormal)
36 - return
37 - }
38 -
39 - arg, found := req.Option("d")
40 - dspath, ok := arg.(string)
41 - if found && !ok {
42 - res.SetError(errors.New("failed to parse datastore flag"), cmds.ErrNormal)
43 - return
44 - }
45 -
46 - fi, err := os.Lstat(filename)
47 - arg, found = req.Option("f")
48 - force, ok := arg.(bool)
49 - if found && !ok {
50 - res.SetError(errors.New("failed to parse force flag"), cmds.ErrNormal)
51 - return
52 - }
53 - if fi != nil || (err != nil && !os.IsNotExist(err)) {
54 - if !force {
55 - res.SetError(errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)"), cmds.ErrNormal)
56 - return
57 - }
58 - }
59 - cfg := new(config.Config)
60 -
61 - cfg.Datastore = config.Datastore{}
62 - if len(dspath) == 0 {
63 - dspath, err = config.DataStorePath("")
64 - if err != nil {
65 - res.SetError(err, cmds.ErrNormal)
66 - return
67 - }
68 - }
69 - cfg.Datastore.Path = dspath
70 - cfg.Datastore.Type = "leveldb"
71 -
72 - // Construct the data store if missing
73 - if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
74 - res.SetError(err, cmds.ErrNormal)
75 - return
76 - }
77 -
78 - // Check the directory is writeable
79 - if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
80 - os.Remove(f.Name())
81 - } else {
82 - res.SetError(errors.New("Datastore '"+dspath+"' is not writeable"), cmds.ErrNormal)
83 - return
84 - }
85 -
86 - cfg.Identity = config.Identity{}
87 -
88 - // setup the node addresses.
89 - cfg.Addresses = config.Addresses{
90 - Swarm: "/ip4/0.0.0.0/tcp/4001",
91 - API: "/ip4/127.0.0.1/tcp/5001",
92 - }
93 -
94 - // setup the node mount points.
95 - cfg.Mounts = config.Mounts{
96 - IPFS: "/ipfs",
97 - IPNS: "/ipns",
98 - }
99 -
100 - arg, found = req.Option("b")
101 - nbits, ok := arg.(int)
102 - if found && !ok {
103 - res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
104 - return
105 - } else if !found {
106 - nbits = 4096
107 - }
108 - if nbits < 1024 {
109 - res.SetError(errors.New("Bitsize less than 1024 is considered unsafe."), cmds.ErrNormal)
110 - return
111 - }
112 -
113 - u.POut("generating key pair\n")
114 - sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
115 - if err != nil {
116 - res.SetError(err, cmds.ErrNormal)
117 - return
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 {
124 - res.SetError(err, cmds.ErrNormal)
125 - return
126 - }
127 - cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
30 + foo(res, req)
31 + },
32 +}
33
129 - id, err := peer.IDFromPubKey(pk)
130 - if err != nil {
131 - res.SetError(err, cmds.ErrNormal)
34 +func foo(res cmds.Response, req cmds.Request) {
35 + ctx := req.Context()
36 +
37 + u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
38 + filename, err := config.Filename(ctx.ConfigRoot)
39 + if err != nil {
40 + res.SetError(errors.New("Couldn't get home directory path"), cmds.ErrNormal)
41 + return
42 + }
43 +
44 + arg, found := req.Option("d")
45 + dspath, ok := arg.(string)
46 + if found && !ok {
47 + res.SetError(errors.New("failed to parse datastore flag"), cmds.ErrNormal)
48 + return
49 + }
50 +
51 + fi, err := os.Lstat(filename)
52 + arg, found = req.Option("f")
53 + force, ok := arg.(bool)
54 + if found && !ok {
55 + res.SetError(errors.New("failed to parse force flag"), cmds.ErrNormal)
56 + return
57 + }
58 + if fi != nil || (err != nil && !os.IsNotExist(err)) {
59 + 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
62 }
134 - cfg.Identity.PeerID = id.Pretty()
135 -
136 - // Use these hardcoded bootstrap peers for now.
137 - cfg.Bootstrap = []*config.BootstrapPeer{
138 - &config.BootstrapPeer{
139 - // mars.i.ipfs.io
140 - PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
141 - Address: "/ip4/104.131.131.82/tcp/4001",
142 - },
143 - }
63 + }
64 + cfg := new(config.Config)
65
145 - // tracking ipfs version used to generate the init folder and adding update checker default setting.
146 - cfg.Version = config.Version{
147 - Check: "error",
148 - Current: updates.Version,
149 - }
150 -
151 - err = config.WriteConfigFile(filename, cfg)
66 + cfg.Datastore = config.Datastore{}
67 + if len(dspath) == 0 {
68 + dspath, err = config.DataStorePath("")
69 if err != nil {
70 res.SetError(err, cmds.ErrNormal)
71 return
72 }
156 - },
73 + }
74 + cfg.Datastore.Path = dspath
75 + cfg.Datastore.Type = "leveldb"
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
81 + }
82 +
83 + // Check the directory is writeable
84 + if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
85 + os.Remove(f.Name())
86 + } else {
87 + res.SetError(errors.New("Datastore '"+dspath+"' is not writeable"), cmds.ErrNormal)
88 + return
89 + }
90 +
91 + cfg.Identity = config.Identity{}
92 +
93 + // setup the node addresses.
94 + cfg.Addresses = config.Addresses{
95 + Swarm: "/ip4/0.0.0.0/tcp/4001",
96 + API: "/ip4/127.0.0.1/tcp/5001",
97 + }
98 +
99 + // setup the node mount points.
100 + cfg.Mounts = config.Mounts{
101 + IPFS: "/ipfs",
102 + IPNS: "/ipns",
103 + }
104 +
105 + arg, found = req.Option("b")
106 + nbits, ok := arg.(int)
107 + if found && !ok {
108 + res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
109 + return
110 + } else if !found {
111 + nbits = 4096
112 + }
113 + if nbits < 1024 {
114 + res.SetError(errors.New("Bitsize less than 1024 is considered unsafe."), cmds.ErrNormal)
115 + return
116 + }
117 +
118 + u.POut("generating key pair\n")
119 + sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
120 + if err != nil {
121 + res.SetError(err, cmds.ErrNormal)
122 + return
123 + }
124 +
125 + // currently storing key unencrypted. in the future we need to encrypt it.
126 + // TODO(security)
127 + skbytes, err := sk.Bytes()
128 + if err != nil {
129 + res.SetError(err, cmds.ErrNormal)
130 + return
131 + }
132 + cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
133 +
134 + id, err := peer.IDFromPubKey(pk)
135 + if err != nil {
136 + res.SetError(err, cmds.ErrNormal)
137 + return
138 + }
139 + cfg.Identity.PeerID = id.Pretty()
140 +
141 + // Use these hardcoded bootstrap peers for now.
142 + cfg.Bootstrap = []*config.BootstrapPeer{
143 + &config.BootstrapPeer{
144 + // mars.i.ipfs.io
145 + PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
146 + Address: "/ip4/104.131.131.82/tcp/4001",
147 + },
148 + }
149 +
150 + // tracking ipfs version used to generate the init folder and adding update checker default setting.
151 + cfg.Version = config.Version{
152 + Check: "error",
153 + Current: updates.Version,
154 + }
155 +
156 + err = config.WriteConfigFile(filename, cfg)
157 + if err != nil {
158 + res.SetError(err, cmds.ErrNormal)
159 + return
160 + }
161 }