Skip to content

Commit

Permalink
make add command use absolute paths and print properly, issue #151
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Oct 10, 2014
1 parent 26574b5 commit bb84c23
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
10 changes: 6 additions & 4 deletions cmd/ipfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"path/filepath"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
Expand Down Expand Up @@ -30,8 +31,9 @@ func init() {
}

var addCmd = makeCommand(command{
name: "add",
args: 1,
flags: []string{"r"},
cmdFn: commands.Add,
name: "add",
args: 1,
flags: []string{"r"},
cmdFn: commands.Add,
argFilter: filepath.Abs,
})
23 changes: 17 additions & 6 deletions cmd/ipfs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
// command is the descriptor of an ipfs daemon command.
// Used with makeCommand to proxy over commands via the daemon.
type command struct {
name string
args int
flags []string
online bool
cmdFn commands.CmdFunc
name string
args int
flags []string
online bool
cmdFn commands.CmdFunc
argFilter func(string) (string, error)
}

// commanderFunc is a function that can be passed into the Commander library as
Expand All @@ -39,7 +40,17 @@ func makeCommand(cmdDesc command) commanderFunc {

cmd := daemon.NewCommand()
cmd.Command = cmdDesc.name
cmd.Args = inp
if cmdDesc.argFilter != nil {
for _, a := range inp {
s, err := cmdDesc.argFilter(a)
if err != nil {
return err
}
cmd.Args = append(cmd.Args, s)
}
} else {
cmd.Args = inp
}

for _, a := range cmdDesc.flags {
cmd.Opts[a] = c.Flag.Lookup(a).Value.Get()
Expand Down
32 changes: 9 additions & 23 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/jbenet/go-ipfs/importer"
dag "github.com/jbenet/go-ipfs/merkledag"
ft "github.com/jbenet/go-ipfs/unixfs"
u "github.com/jbenet/go-ipfs/util"
)

// Error indicating the max depth has been exceded.
Expand All @@ -30,14 +29,8 @@ func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Wr
// add every path in args
for _, path := range args {

// get absolute path, as incoming arg may be relative
path, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}

// Add the file
_, err = AddPath(n, path, depth)
_, err := AddPath(n, path, depth, out)
if err != nil {
if err == ErrDepthLimitExceeded && depth == 1 {
err = errors.New("use -r to recursively add directories")
Expand All @@ -58,7 +51,7 @@ func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Wr
}

// AddPath adds a particular path to ipfs.
func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
if depth == 0 {
return nil, ErrDepthLimitExceeded
}
Expand All @@ -69,13 +62,13 @@ func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
}

if fi.IsDir() {
return addDir(n, fpath, depth)
return addDir(n, fpath, depth, out)
}

return addFile(n, fpath, depth)
return addFile(n, fpath, depth, out)
}

func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
tree := &dag.Node{Data: ft.FolderPBData()}

files, err := ioutil.ReadDir(fpath)
Expand All @@ -86,7 +79,7 @@ func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
// construct nodes for containing files.
for _, f := range files {
fp := filepath.Join(fpath, f.Name())
nd, err := AddPath(n, fp, depth-1)
nd, err := AddPath(n, fp, depth-1, out)
if err != nil {
return nil, err
}
Expand All @@ -99,7 +92,7 @@ func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
return tree, addNode(n, tree, fpath)
}

func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
root, err := importer.NewDagFromFile(fpath)
if err != nil {
return nil, err
Expand All @@ -110,9 +103,9 @@ func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
return nil, err
}

log.Info("Adding file: %s = %s\n", fpath, k)
fmt.Fprintf(out, "Adding file: %s = %s\n", fpath, k)
for _, l := range root.Links {
log.Info("SubBlock: %s\n", l.Hash.B58String())
fmt.Fprintf(out, "SubBlock: %s\n", l.Hash.B58String())
}

return root, addNode(n, root, fpath)
Expand All @@ -126,13 +119,6 @@ func addNode(n *core.IpfsNode, nd *dag.Node, fpath string) error {
return err
}

k, err := nd.Key()
if err != nil {
return err
}

u.POut("added %s %s\n", k, fpath)

// ensure we keep it. atm no-op
return n.PinDagNodeRecursively(nd, -1)
}

0 comments on commit bb84c23

Please sign in to comment.