add files :)
Juan Batiz-Benet committed
Jul 5, 2014 at 17:16 UTC
6282214582768b7727099ce43f582421b6309299
2 files changed
+114
-1
cli/add.go
new
+113
@@ -0,0 +1,113 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+ "github.com/gonuts/flag"
6
+ "github.com/jbenet/commander"
7
+ core "github.com/jbenet/go-ipfs/core"
8
+ importer "github.com/jbenet/go-ipfs/importer"
9
+ u "github.com/jbenet/go-ipfs/util"
10
+ mh "github.com/jbenet/go-multihash"
11
+ "os"
12
+)
13
+
14
+var cmdIpfsAdd = &commander.Command{
15
+ UsageLine: "add",
16
+ Short: "Add an object to ipfs.",
17
+ Long: `ipfs add <path>... - Add objects to ipfs.
18
+
19
+ Adds contents of <path> to ipfs. Use -r to add directories.
20
+ Note that directories are added recursively, to form the ipfs
21
+ MerkleDAG. A smarter partial add with a staging area (like git)
22
+ remains to be implemented.
23
+`,
24
+ Run: addCmd,
25
+ Flag: *flag.NewFlagSet("ipfs-add", flag.ExitOnError),
26
+}
27
+
28
+func init() {
29
+ cmdIpfsAdd.Flag.Bool("r", false, "add objects recursively")
30
+}
31
+
32
+func addCmd(c *commander.Command, inp []string) error {
33
+ if len(inp) < 1 {
34
+ u.POut(c.Long)
35
+ return nil
36
+ }
37
+
38
+ n, err := localNode()
39
+ if err != nil {
40
+ return err
41
+ }
42
+
43
+ recursive := c.Flag.Lookup("r").Value.Get().(bool)
44
+ var depth int
45
+ if recursive {
46
+ depth = -1
47
+ } else {
48
+ depth = 1
49
+ }
50
+
51
+ for _, fpath := range inp {
52
+ err = addPath(n, fpath, depth)
53
+ if err != nil {
54
+ u.PErr("error adding %s: %v\n", fpath, err)
55
+ }
56
+ }
57
+ return err
58
+}
59
+
60
+func addPath(n *core.IpfsNode, fpath string, depth int) error {
61
+ fi, err := os.Stat(fpath)
62
+ if err != nil {
63
+ return err
64
+ }
65
+
66
+ if fi.IsDir() {
67
+ return addDir(n, fpath, depth)
68
+ } else {
69
+ return addFile(n, fpath, depth)
70
+ }
71
+}
72
+
73
+func addDir(n *core.IpfsNode, fpath string, depth int) error {
74
+ return u.NotImplementedError
75
+}
76
+
77
+func addFile(n *core.IpfsNode, fpath string, depth int) error {
78
+ stat, err := os.Stat(fpath)
79
+ if err != nil {
80
+ return err
81
+ }
82
+
83
+ if stat.IsDir() {
84
+ return fmt.Errorf("addFile: `fpath` is a directory")
85
+ }
86
+
87
+ f, err := os.Open(fpath)
88
+ if err != nil {
89
+ return err
90
+ }
91
+ defer f.Close()
92
+
93
+ root, err := importer.NewDagFromReader(f, stat.Size())
94
+ if err != nil {
95
+ return err
96
+ }
97
+
98
+ h, _ := root.Multihash()
99
+ u.POut("hash: %v\n", h)
100
+ u.POut("data: %v\n", root.Data)
101
+
102
+ // add the file to the graph + local storage
103
+ k, err := n.AddDagNode(root)
104
+ if err != nil {
105
+ return err
106
+ }
107
+
108
+ u.POut("added %s %s\n", fpath, mh.Multihash(k).B58String())
109
+ return nil
110
+
111
+ // ensure we keep it. atm no-op
112
+ // return n.PinDagNode(root)
113
+}
cli/ipfs.go
+1
-1
@@ -36,8 +36,8 @@ Use "ipfs help <command>" for more information about a command.
36
`,
37
Run: ipfsCmd,
38
Subcommands: []*commander.Command{
39
+ cmdIpfsAdd,
40
cmdIpfsVersion,
40
- // cmdIpfsConfig,
41
cmdIpfsCommands,
42
},
43
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),