| 1 | package assets |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | "fmt" |
| 6 | gopath "path" |
| 7 | |
| 8 | "github.com/ipfs/kubo/core" |
| 9 | "github.com/ipfs/kubo/core/coreapi" |
| 10 | |
| 11 | "github.com/ipfs/boxo/files" |
| 12 | cid "github.com/ipfs/go-cid" |
| 13 | ) |
| 14 | |
| 15 | //go:embed init-doc |
| 16 | var Asset embed.FS |
| 17 | |
| 18 | // initDocPaths lists the paths for the docs we want to seed during --init. |
| 19 | var initDocPaths = []string{ |
| 20 | gopath.Join("init-doc", "about"), |
| 21 | gopath.Join("init-doc", "readme"), |
| 22 | gopath.Join("init-doc", "help"), |
| 23 | gopath.Join("init-doc", "contact"), |
| 24 | gopath.Join("init-doc", "security-notes"), |
| 25 | gopath.Join("init-doc", "quick-start"), |
| 26 | gopath.Join("init-doc", "ping"), |
| 27 | } |
| 28 | |
| 29 | // SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key. |
| 30 | func SeedInitDocs(nd *core.IpfsNode) (cid.Cid, error) { |
| 31 | return addAssetList(nd, initDocPaths) |
| 32 | } |
| 33 | |
| 34 | func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) { |
| 35 | api, err := coreapi.NewCoreAPI(nd) |
| 36 | if err != nil { |
| 37 | return cid.Cid{}, err |
| 38 | } |
| 39 | |
| 40 | dirMap := map[string]files.Node{} |
| 41 | |
| 42 | for _, p := range l { |
| 43 | d, err := Asset.ReadFile(p) |
| 44 | if err != nil { |
| 45 | return cid.Cid{}, fmt.Errorf("assets: could load Asset '%s': %s", p, err) |
| 46 | } |
| 47 | |
| 48 | dirMap[gopath.Base(p)] = files.NewBytesFile(d) |
| 49 | } |
| 50 | |
| 51 | basePath, err := api.Unixfs().Add(nd.Context(), files.NewMapDirectory(dirMap)) |
| 52 | if err != nil { |
| 53 | return cid.Cid{}, err |
| 54 | } |
| 55 | |
| 56 | if err := api.Pin().Add(nd.Context(), basePath); err != nil { |
| 57 | return cid.Cid{}, err |
| 58 | } |
| 59 | |
| 60 | return basePath.RootCid(), nil |
| 61 | } |