@cryptotaxi247 / kubo / commits / 1ad5b345b

core/commands: add: Added -w/--wrap-with-directory option

Matt Bell committed Jan 31, 2015 at 19:05 UTC 1ad5b345b8142e2f08d4cca6aab34a9a6267a12e
1 file changed +22 -4
core/commands/add.go
+22 -4
@@ -10,6 +10,7 @@ import (
10 cmds "github.com/jbenet/go-ipfs/commands"
11 files "github.com/jbenet/go-ipfs/commands/files"
12 core "github.com/jbenet/go-ipfs/core"
13 + coreunix "github.com/jbenet/go-ipfs/core/coreunix"
14 importer "github.com/jbenet/go-ipfs/importer"
15 "github.com/jbenet/go-ipfs/importer/chunk"
16 dag "github.com/jbenet/go-ipfs/merkledag"
@@ -26,7 +27,10 @@ var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
27 // how many bytes of progress to wait before sending a progress update message
28 const progressReaderIncrement = 1024 * 256
29
29 -const progressOptionName = "progress"
30 +const (
31 + progressOptionName = "progress"
32 + wrapOptionName = "wrap-with-directory"
33 +)
34
35 type AddedObject struct {
36 Name string
@@ -52,6 +56,7 @@ remains to be implemented.
56 cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
57 cmds.BoolOption("quiet", "q", "Write minimal output"),
58 cmds.BoolOption(progressOptionName, "p", "Stream progress data"),
59 + cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
60 },
61 PreRun: func(req cmds.Request) error {
62 if quiet, _, _ := req.Option("quiet").Bool(); quiet {
@@ -84,6 +89,7 @@ remains to be implemented.
89 }
90
91 progress, _, _ := req.Option(progressOptionName).Bool()
92 + wrap, _, _ := req.Option(wrapOptionName).Bool()
93
94 outChan := make(chan interface{})
95 res.SetOutput((<-chan interface{})(outChan))
@@ -97,7 +103,7 @@ remains to be implemented.
103 return
104 }
105
100 - _, err = addFile(n, file, outChan, progress)
106 + _, err = addFile(n, file, outChan, progress, wrap)
107 if err != nil {
108 return
109 }
@@ -225,7 +231,7 @@ func addNode(n *core.IpfsNode, node *dag.Node) error {
231 return nil
232 }
233
228 -func addFile(n *core.IpfsNode, file files.File, out chan interface{}, progress bool) (*dag.Node, error) {
234 +func addFile(n *core.IpfsNode, file files.File, out chan interface{}, progress bool, wrap bool) (*dag.Node, error) {
235 if file.IsDirectory() {
236 return addDir(n, file, out, progress)
237 }
@@ -237,6 +243,18 @@ func addFile(n *core.IpfsNode, file files.File, out chan interface{}, progress b
243 reader = &progressReader{file: file, out: out}
244 }
245
246 + if wrap {
247 + p, dagnode, err := coreunix.AddWrapped(n, reader, path.Base(file.FileName()))
248 + if err != nil {
249 + return nil, err
250 + }
251 + out <- &AddedObject{
252 + Hash: p,
253 + Name: file.FileName(),
254 + }
255 + return dagnode, nil
256 + }
257 +
258 dns, err := add(n, []io.Reader{reader})
259 if err != nil {
260 return nil, err
@@ -263,7 +281,7 @@ func addDir(n *core.IpfsNode, dir files.File, out chan interface{}, progress boo
281 break
282 }
283
266 - node, err := addFile(n, file, out, progress)
284 + node, err := addFile(n, file, out, progress, false)
285 if err != nil {
286 return nil, err
287 }