Commit bb84c232 authored by Jeromy's avatar Jeromy

make add command use absolute paths and print properly, issue #151

parent 26574b53
......@@ -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"
......@@ -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,
})
......@@ -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
......@@ -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()
......
......@@ -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.
......@@ -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")
......@@ -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
}
......@@ -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)
......@@ -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
}
......@@ -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
......@@ -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)
......@@ -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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment