@cryptotaxi247 / kubo / commits / 7596bcce2

cleaned up ipfs init

Juan Batiz-Benet committed Nov 15, 2014 at 22:50 UTC 7596bcce2d221d8f9eed1c47ca117c745f9a5ffc
1 file changed +82 -60
cmd/ipfs2/init.go
+82 -60
@@ -50,98 +50,71 @@ var initCmd = &cmds.Command{
50 nBitsForKeypair = 4096
51 }
52
53 - return nil, doInit(req.Context().ConfigRoot, dspathOverride, force, nBitsForKeypair)
53 + return doInit(req.Context().ConfigRoot, dspathOverride, force, nBitsForKeypair)
54 },
55 }
56
57 +var errCannotInitConfigExists = errors.New(`ipfs configuration file already exists!
58 +Reinitializing would overwrite your keys.
59 +(use -f to force overwrite)
60 +`)
61 +
62 +var welcomeMsg = `Hello and Welcome to IPFS!
63 +
64 +██╗██████╗ ███████╗███████╗
65 +██║██╔══██╗██╔════╝██╔════╝
66 +██║██████╔╝█████╗ ███████╗
67 +██║██╔═══╝ ██╔══╝ ╚════██║
68 +██║██║ ██║ ███████║
69 +╚═╝╚═╝ ╚═╝ ╚══════╝
70 +
71 +If you're seeing this, you have successfully installed
72 +IPFS and are now interfacing with the ipfs merkledag!
73 +
74 +For a short demo of what you can do, enter 'ipfs tour'
75 +`
76 +
77 // TODO add default welcome hash: eaa68bedae247ed1e5bd0eb4385a3c0959b976e4
78 // NB: if dspath is not provided, it will be retrieved from the config
59 -func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypair int) error {
79 +func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypair int) (interface{}, error) {
80
81 u.POut("initializing ipfs node at %s\n", configRoot)
82
83 configFilename, err := config.Filename(configRoot)
84 if err != nil {
65 - return errors.New("Couldn't get home directory path")
85 + return nil, errors.New("Couldn't get home directory path")
86 }
87
88 fi, err := os.Lstat(configFilename)
89 if fi != nil || (err != nil && !os.IsNotExist(err)) {
90 if !force {
91 // TODO multi-line string
72 - return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
92 + return nil, errCannotInitConfigExists
93 }
94 }
95
76 - ds, err := datastoreConfig(dspathOverride)
77 - if err != nil {
78 - return err
79 - }
80 -
81 - identity, err := identityConfig(nBitsForKeypair)
82 - if err != nil {
83 - return err
84 - }
85 -
86 - conf := config.Config{
87 -
88 - // setup the node addresses.
89 - Addresses: config.Addresses{
90 - Swarm: "/ip4/0.0.0.0/tcp/4001",
91 - API: "/ip4/127.0.0.1/tcp/5001",
92 - },
93 -
94 - Bootstrap: []*config.BootstrapPeer{
95 - &config.BootstrapPeer{ // Use these hardcoded bootstrap peers for now.
96 - // mars.i.ipfs.io
97 - PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
98 - Address: "/ip4/104.131.131.82/tcp/4001",
99 - },
100 - },
101 -
102 - Datastore: ds,
103 -
104 - Identity: identity,
105 -
106 - // setup the node mount points.
107 - Mounts: config.Mounts{
108 - IPFS: "/ipfs",
109 - IPNS: "/ipns",
110 - },
111 -
112 - // tracking ipfs version used to generate the init folder and adding
113 - // update checker default setting.
114 - Version: config.VersionDefaultValue(),
115 - }
116 -
117 - err = config.WriteConfigFile(configFilename, conf)
96 + conf, err := initConfig(configFilename, dspathOverride, nBitsForKeypair)
97 if err != nil {
119 - return err
98 + return nil, err
99 }
100
122 - nd, err := core.NewIpfsNode(&conf, false)
101 + nd, err := core.NewIpfsNode(conf, false)
102 if err != nil {
124 - return err
103 + return nil, err
104 }
105 defer nd.Close()
106
107 // Set up default file
129 - msg := `Hello and Welcome to IPFS!
130 -If you're seeing this, that means that you have successfully
131 -installed ipfs and are now interfacing with the wonderful
132 -world of DAGs and hashes!
133 -`
134 - reader := bytes.NewBufferString(msg)
108 + reader := bytes.NewBufferString(welcomeMsg)
109
110 defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
111 if err != nil {
138 - return err
112 + return nil, err
113 }
114
115 k, _ := defnd.Key()
142 - fmt.Printf("Default file key: %s\n", k)
143 -
144 - return nil
116 + fmt.Printf("done.\nto test, enter: ipfs cat %s\n", k)
117 + return nil, nil
118 }
119
120 func datastoreConfig(dspath string) (config.Datastore, error) {
@@ -171,6 +144,55 @@ func datastoreConfig(dspath string) (config.Datastore, error) {
144 return ds, nil
145 }
146
147 +func initConfig(configFilename string, dspathOverride string, nBitsForKeypair int) (*config.Config, error) {
148 + ds, err := datastoreConfig(dspathOverride)
149 + if err != nil {
150 + return nil, err
151 + }
152 +
153 + identity, err := identityConfig(nBitsForKeypair)
154 + if err != nil {
155 + return nil, err
156 + }
157 +
158 + conf := &config.Config{
159 +
160 + // setup the node addresses.
161 + Addresses: config.Addresses{
162 + Swarm: "/ip4/0.0.0.0/tcp/4001",
163 + API: "/ip4/127.0.0.1/tcp/5001",
164 + },
165 +
166 + Bootstrap: []*config.BootstrapPeer{
167 + &config.BootstrapPeer{ // Use these hardcoded bootstrap peers for now.
168 + // mars.i.ipfs.io
169 + PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
170 + Address: "/ip4/104.131.131.82/tcp/4001",
171 + },
172 + },
173 +
174 + Datastore: ds,
175 +
176 + Identity: identity,
177 +
178 + // setup the node mount points.
179 + Mounts: config.Mounts{
180 + IPFS: "/ipfs",
181 + IPNS: "/ipns",
182 + },
183 +
184 + // tracking ipfs version used to generate the init folder and adding
185 + // update checker default setting.
186 + Version: config.VersionDefaultValue(),
187 + }
188 +
189 + if err := config.WriteConfigFile(configFilename, conf); err != nil {
190 + return nil, err
191 + }
192 +
193 + return conf, nil
194 +}
195 +
196 func identityConfig(nbits int) (config.Identity, error) {
197 // TODO guard higher up
198 ident := config.Identity{}
@@ -178,7 +200,7 @@ func identityConfig(nbits int) (config.Identity, error) {
200 return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
201 }
202
181 - fmt.Println("generating key pair...")
203 + fmt.Printf("generating key pair...")
204 sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
205 if err != nil {
206 return ident, err