-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobject.go
68 lines (57 loc) · 1.58 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package embeddedShell
import (
"context"
"fmt"
"github.com/ipfs/go-ipfs/core/coreapi"
dag "github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
)
func (s *Shell) NewObject(template string) (string, error) {
node := new(dag.ProtoNode)
switch template {
case "":
break
case "unixfs-dir":
node.SetData(unixfs.FolderPBData())
default:
return "", fmt.Errorf("unknown template %s", template)
}
err := s.node.DAG.Add(s.ctx, node)
if err != nil {
return "", err
}
return node.Cid().String(), nil
}
// TODO: extract all this logic from the core/commands/object.go to avoid dupe code
func (s *Shell) Patch(root, action string, args ...string) (string, error) {
api, err := coreapi.NewCoreAPI(s.node)
if err != nil {
return "", err
}
insertpath := args[0]
childhash := path.New(args[1])
switch action {
case "add-link":
h, err := api.Object().AddLink(context.Background(), path.New(root), insertpath, childhash)
if err != nil {
return "", err
}
return h.Cid().String(), nil
default:
return "", fmt.Errorf("unsupported action (impl not complete)")
}
}
//TODO: hrm, maybe this interface could be better
func (s *Shell) PatchLink(root, npath, childhash string, create bool) (string, error) {
api, err := coreapi.NewCoreAPI(s.node)
if err != nil {
return "", err
}
h, err := api.Object().AddLink(context.Background(), path.New(root), npath, path.New(childhash), options.Object.Create(create))
if err != nil {
return "", err
}
return h.Cid().String(), nil
}