@cryptotaxi247 / kubo / commits / 02377fabf

assets: remove seed logic from init

License: MIT Signed-off-by: Henry <cryptix@riseup.net>

Henry committed Jul 3, 2015 at 10:21 UTC 02377fabf842d35b60fb8e90edb740f2b253c9c7
2 files changed +59 -38
assets/assets.go
+57 -7
@@ -2,11 +2,61 @@
2
3 package assets
4
5 -var InitDir = map[string][]byte{
6 - "about": MustAsset("init-doc/about"),
7 - "readme": MustAsset("init-doc/readme"),
8 - "help": MustAsset("init-doc/help"),
9 - "contact": MustAsset("init-doc/contact"),
10 - "security-notes": MustAsset("init-doc/security-notes"),
11 - "quick-start": MustAsset("init-doc/quick-start"),
5 +import (
6 + "bytes"
7 + "fmt"
8 + "path/filepath"
9 +
10 + "github.com/ipfs/go-ipfs/blocks/key"
11 + "github.com/ipfs/go-ipfs/core"
12 + "github.com/ipfs/go-ipfs/core/coreunix"
13 + uio "github.com/ipfs/go-ipfs/unixfs/io"
14 +)
15 +
16 +// initDocPaths lists the paths for the docs we want to seed during --init
17 +var initDocPaths = []string{
18 + "init-doc/about",
19 + "init-doc/readme",
20 + "init-doc/help",
21 + "init-doc/contact",
22 + "init-doc/security-notes",
23 + "init-doc/quick-start",
24 +}
25 +
26 +func SeedInitDocs(nd *core.IpfsNode) (*key.Key, error) {
27 + dirb := uio.NewDirectory(nd.DAG)
28 +
29 + for _, p := range initDocPaths {
30 + d, err := Asset(p)
31 + if err != nil {
32 + return nil, fmt.Errorf("assets.AddDocuDir: could load Asset '%s': %s", p, err)
33 + }
34 +
35 + s, err := coreunix.Add(nd, bytes.NewBuffer(d))
36 + if err != nil {
37 + return nil, fmt.Errorf("assets.AddDocuDir: could not Add '%s': %s", p, err)
38 + }
39 +
40 + fname := filepath.Base(p)
41 + k := key.B58KeyDecode(s)
42 + if err := dirb.AddChild(fname, k); err != nil {
43 + return nil, fmt.Errorf("assets.AddDocuDir: could not add '%s' as a child: %s", fname, err)
44 + }
45 + }
46 +
47 + dir := dirb.GetNode()
48 + dkey, err := nd.DAG.Add(dir)
49 + if err != nil {
50 + return nil, fmt.Errorf("assets.AddDocuDir: DAG.Add(dir) failed: %s", err)
51 + }
52 +
53 + if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
54 + return nil, fmt.Errorf("assets.AddDocuDir: Pinning on init-docu failed: %s", err)
55 + }
56 +
57 + if err := nd.Pinning.Flush(); err != nil {
58 + return nil, fmt.Errorf("assets.AddDocuDir: Pinnig flush failed: %s", err)
59 + }
60 +
61 + return &dkey, nil
62 }
cmd/ipfs/init.go
+2 -31
@@ -1,7 +1,6 @@
1 package main
2
3 import (
4 - "bytes"
4 "errors"
5 "fmt"
6 "io"
@@ -10,14 +9,11 @@ import (
9
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11 assets "github.com/ipfs/go-ipfs/assets"
13 - key "github.com/ipfs/go-ipfs/blocks/key"
12 cmds "github.com/ipfs/go-ipfs/commands"
13 core "github.com/ipfs/go-ipfs/core"
16 - coreunix "github.com/ipfs/go-ipfs/core/coreunix"
14 namesys "github.com/ipfs/go-ipfs/namesys"
15 config "github.com/ipfs/go-ipfs/repo/config"
16 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
20 - uio "github.com/ipfs/go-ipfs/unixfs/io"
17 )
18
19 const nBitsForKeypairDefault = 2048
@@ -167,34 +163,9 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
163 }
164 defer nd.Close()
165
170 - dirb := uio.NewDirectory(nd.DAG)
171 -
172 - // add every file in the assets pkg
173 - for fname, file := range assets.InitDir {
174 - buf := bytes.NewBuffer(file)
175 - s, err := coreunix.Add(nd, buf)
176 - if err != nil {
177 - return err
178 - }
179 -
180 - k := key.B58KeyDecode(s)
181 - if err := dirb.AddChild(fname, k); err != nil {
182 - return err
183 - }
184 - }
185 -
186 - dir := dirb.GetNode()
187 - dkey, err := nd.DAG.Add(dir)
166 + dkey, err := assets.SeedInitDocs(nd)
167 if err != nil {
189 - return err
190 - }
191 -
192 - if err := nd.Pinning.Pin(ctx, dir, true); err != nil {
193 - return err
194 - }
195 -
196 - if err := nd.Pinning.Flush(); err != nil {
197 - return err
168 + return fmt.Errorf("init: seeding init docs failed: %s", err)
169 }
170
171 if _, err = fmt.Fprintf(out, "to get started, enter:\n"); err != nil {