make add command use absolute paths and print properly, issue #151
Jeromy committed
Oct 10, 2014 at 13:46 UTC
bb84c232e1c7d631b1573dd66973fbc3320f7c6c
3 files changed
+32
-33
cmd/ipfs/add.go
+6
-4
@@ -2,6 +2,7 @@ package main
2
3
import (
4
"fmt"
5
+ "path/filepath"
6
7
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
8
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
@@ -30,8 +31,9 @@ func init() {
31
}
32
33
var addCmd = makeCommand(command{
33
- name: "add",
34
- args: 1,
35
- flags: []string{"r"},
36
- cmdFn: commands.Add,
34
+ name: "add",
35
+ args: 1,
36
+ flags: []string{"r"},
37
+ cmdFn: commands.Add,
38
+ argFilter: filepath.Abs,
39
})
cmd/ipfs/gen.go
+17
-6
@@ -13,11 +13,12 @@ import (
13
// command is the descriptor of an ipfs daemon command.
14
// Used with makeCommand to proxy over commands via the daemon.
15
type command struct {
16
- name string
17
- args int
18
- flags []string
19
- online bool
20
- cmdFn commands.CmdFunc
16
+ name string
17
+ args int
18
+ flags []string
19
+ online bool
20
+ cmdFn commands.CmdFunc
21
+ argFilter func(string) (string, error)
22
}
23
24
// commanderFunc is a function that can be passed into the Commander library as
@@ -39,7 +40,17 @@ func makeCommand(cmdDesc command) commanderFunc {
40
41
cmd := daemon.NewCommand()
42
cmd.Command = cmdDesc.name
42
- cmd.Args = inp
43
+ if cmdDesc.argFilter != nil {
44
+ for _, a := range inp {
45
+ s, err := cmdDesc.argFilter(a)
46
+ if err != nil {
47
+ return err
48
+ }
49
+ cmd.Args = append(cmd.Args, s)
50
+ }
51
+ } else {
52
+ cmd.Args = inp
53
+ }
54
55
for _, a := range cmdDesc.flags {
56
cmd.Opts[a] = c.Flag.Lookup(a).Value.Get()
core/commands/add.go
+9
-23
@@ -12,7 +12,6 @@ import (
12
"github.com/jbenet/go-ipfs/importer"
13
dag "github.com/jbenet/go-ipfs/merkledag"
14
ft "github.com/jbenet/go-ipfs/unixfs"
15
- u "github.com/jbenet/go-ipfs/util"
15
)
16
17
// 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
29
// add every path in args
30
for _, path := range args {
31
33
- // get absolute path, as incoming arg may be relative
34
- path, err := filepath.Abs(path)
35
- if err != nil {
36
- return fmt.Errorf("addFile error: %v", err)
37
- }
38
-
32
// Add the file
40
- _, err = AddPath(n, path, depth)
33
+ _, err := AddPath(n, path, depth, out)
34
if err != nil {
35
if err == ErrDepthLimitExceeded && depth == 1 {
36
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
51
}
52
53
// AddPath adds a particular path to ipfs.
61
-func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
54
+func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
55
if depth == 0 {
56
return nil, ErrDepthLimitExceeded
57
}
@@ -69,13 +62,13 @@ func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
62
}
63
64
if fi.IsDir() {
72
- return addDir(n, fpath, depth)
65
+ return addDir(n, fpath, depth, out)
66
}
67
75
- return addFile(n, fpath, depth)
68
+ return addFile(n, fpath, depth, out)
69
}
70
78
-func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
71
+func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
72
tree := &dag.Node{Data: ft.FolderPBData()}
73
74
files, err := ioutil.ReadDir(fpath)
@@ -86,7 +79,7 @@ func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
79
// construct nodes for containing files.
80
for _, f := range files {
81
fp := filepath.Join(fpath, f.Name())
89
- nd, err := AddPath(n, fp, depth-1)
82
+ nd, err := AddPath(n, fp, depth-1, out)
83
if err != nil {
84
return nil, err
85
}
@@ -99,7 +92,7 @@ func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
92
return tree, addNode(n, tree, fpath)
93
}
94
102
-func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
95
+func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
96
root, err := importer.NewDagFromFile(fpath)
97
if err != nil {
98
return nil, err
@@ -110,9 +103,9 @@ func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
103
return nil, err
104
}
105
113
- log.Info("Adding file: %s = %s\n", fpath, k)
106
+ fmt.Fprintf(out, "Adding file: %s = %s\n", fpath, k)
107
for _, l := range root.Links {
115
- log.Info("SubBlock: %s\n", l.Hash.B58String())
108
+ fmt.Fprintf(out, "SubBlock: %s\n", l.Hash.B58String())
109
}
110
111
return root, addNode(n, root, fpath)
@@ -126,13 +119,6 @@ func addNode(n *core.IpfsNode, nd *dag.Node, fpath string) error {
119
return err
120
}
121
129
- k, err := nd.Key()
130
- if err != nil {
131
- return err
132
- }
133
-
134
- u.POut("added %s %s\n", k, fpath)
135
-
122
// ensure we keep it. atm no-op
123
return n.PinDagNodeRecursively(nd, -1)
124
}