Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial implementation of save #1187

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var rootSubcommands = map[string]*cmds.Command{
"ping": PingCmd,
"refs": RefsCmd,
"repo": RepoCmd,
"save": SaveCmd,
"stats": StatsCmd,
"swarm": SwarmCmd,
"update": UpdateCmd,
Expand Down
116 changes: 116 additions & 0 deletions core/commands/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package commands

import (
"bytes"
"errors"
"fmt"
"io"
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
nsfs "github.com/ipfs/go-ipfs/ipnsfs"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
)

var SaveCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Save a hash to a location within your ipns keyspace",
ShortDescription: ``,
},
Arguments: []cmds.Argument{
cmds.StringArg("object", true, false, "The object to save"),
cmds.StringArg("path", true, false, "The path within ipns/local to save the object"),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

p, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

obj, err := core.Resolve(nd, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

err = saveNodeTo(nd, obj, req.Arguments()[1])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

k, err := obj.Key()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

res.SetOutput(&SavedEntry{
Entry: k.Pretty(),
Path: req.Arguments()[1],
})
},
Type: SavedEntry{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
se, ok := res.Output().(*SavedEntry)
if !ok {
return nil, cmds.ErrIncorrectType
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "save %s to %s\n", se.Entry, se.Path)
return buf, nil
},
},
}

type SavedEntry struct {
Entry string
Path string
}

func saveNodeTo(nd *core.IpfsNode, obj *dag.Node, target string) error {
root, err := nd.IpnsFs.GetRoot(nd.Identity.Pretty())
if err != nil {
return err
}

rdir, ok := root.GetValue().(*nsfs.Directory)
if !ok {
return errors.New("your ipns entry doesnt point to a directory")
}

parts := strings.Split(target, "/")

cur := rdir
for i := 0; i < len(parts)-1; i++ {
child, err := cur.Child(parts[i])
if err != nil {
return err
}

switch child := child.(type) {
case *nsfs.Directory:
cur = child
case *nsfs.File:
return fmt.Errorf("%s is a file", strings.Join(parts[:i+1], "/"))
}
}

err = cur.AddChild(parts[len(parts)-1], obj)
if err != nil {
return err
}

return nil
}