ipfs files: use go-ipfs-cmds
License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>
Michael Muré committed
Jan 16, 2018 at 18:25 UTC
53c42fc4d3111bdfcb56b3afac95c367479c35c3
2 files changed
+47
-45
core/commands/files/files.go
+46
-44
@@ -10,7 +10,8 @@ import (
10
gopath "path"
11
"strings"
12
13
- cmds "github.com/ipfs/go-ipfs/commands"
13
+ oldcmds "github.com/ipfs/go-ipfs/commands"
14
+ lgc "github.com/ipfs/go-ipfs/commands/legacy"
15
core "github.com/ipfs/go-ipfs/core"
16
e "github.com/ipfs/go-ipfs/core/commands/e"
17
dag "github.com/ipfs/go-ipfs/merkledag"
@@ -19,6 +20,7 @@ import (
20
ft "github.com/ipfs/go-ipfs/unixfs"
21
uio "github.com/ipfs/go-ipfs/unixfs/io"
22
23
+ cmds "gx/ipfs/QmZ9hww8R3FKrDRCYPxhN13m6XgjPDpaSvdUfisPvERzXz/go-ipfs-cmds"
24
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
25
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
26
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
@@ -46,19 +48,19 @@ operations.
48
`,
49
},
50
Options: []cmdkit.Option{
49
- cmdkit.BoolOption("f", "flush", "Flush target and ancestors after write.").WithDefault(true),
51
+ cmdkit.BoolOption( "f", "flush", "Flush target and ancestors after write.").WithDefault(true),
52
},
53
Subcommands: map[string]*cmds.Command{
52
- "read": FilesReadCmd,
53
- "write": FilesWriteCmd,
54
- "mv": FilesMvCmd,
55
- "cp": FilesCpCmd,
56
- "ls": FilesLsCmd,
57
- "mkdir": FilesMkdirCmd,
58
- "stat": FilesStatCmd,
59
- "rm": FilesRmCmd,
60
- "flush": FilesFlushCmd,
61
- "chcid": FilesChcidCmd,
54
+ "read": lgc.NewCommand(filesReadCmd),
55
+ "write": lgc.NewCommand(filesWriteCmd),
56
+ "mv": lgc.NewCommand(filesMvCmd),
57
+ "cp": lgc.NewCommand(filesCpCmd),
58
+ "ls": lgc.NewCommand(filesLsCmd),
59
+ "mkdir": lgc.NewCommand(filesMkdirCmd),
60
+ "stat": lgc.NewCommand(filesStatCmd),
61
+ "rm": lgc.NewCommand(filesRmCmd),
62
+ "flush": lgc.NewCommand(filesFlushCmd),
63
+ "chcid": lgc.NewCommand(filesChcidCmd),
64
},
65
}
66
@@ -73,7 +75,7 @@ CumulativeSize: <cumulsize>
75
ChildBlocks: <childs>
76
Type: <type>`
77
76
-var FilesStatCmd = &cmds.Command{
78
+var filesStatCmd = &oldcmds.Command{
79
Helptext: cmdkit.HelpText{
80
Tagline: "Display file status.",
81
},
@@ -87,7 +89,7 @@ var FilesStatCmd = &cmds.Command{
89
cmdkit.BoolOption("hash", "Print only hash. Implies '--format=<hash>'. Conflicts with other format options."),
90
cmdkit.BoolOption("size", "Print only size. Implies '--format=<cumulsize>'. Conflicts with other format options."),
91
},
90
- Run: func(req cmds.Request, res cmds.Response) {
92
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
93
94
_, err := statGetFormatOptions(req)
95
if err != nil {
@@ -120,8 +122,8 @@ var FilesStatCmd = &cmds.Command{
122
123
res.SetOutput(o)
124
},
123
- Marshalers: cmds.MarshalerMap{
124
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
125
+ Marshalers: oldcmds.MarshalerMap{
126
+ oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
127
v, err := unwrapOutput(res.Output())
128
if err != nil {
129
return nil, err
@@ -151,7 +153,7 @@ func moreThanOne(a, b, c bool) bool {
153
return a && b || b && c || a && c
154
}
155
154
-func statGetFormatOptions(req cmds.Request) (string, error) {
156
+func statGetFormatOptions(req oldcmds.Request) (string, error) {
157
158
hash, _, _ := req.Option("hash").Bool()
159
size, _, _ := req.Option("size").Bool()
@@ -220,7 +222,7 @@ func statNode(ds ipld.DAGService, fsn mfs.FSNode) (*Object, error) {
222
}
223
}
224
223
-var FilesCpCmd = &cmds.Command{
225
+var filesCpCmd = &oldcmds.Command{
226
Helptext: cmdkit.HelpText{
227
Tagline: "Copy files into mfs.",
228
},
@@ -228,7 +230,7 @@ var FilesCpCmd = &cmds.Command{
230
cmdkit.StringArg("source", true, false, "Source object to copy."),
231
cmdkit.StringArg("dest", true, false, "Destination to copy object to."),
232
},
231
- Run: func(req cmds.Request, res cmds.Response) {
233
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
234
node, err := req.InvocContext().GetNode()
235
if err != nil {
236
res.SetError(err, cmdkit.ErrNormal)
@@ -310,11 +312,11 @@ type Object struct {
312
Type string
313
}
314
313
-type FilesLsOutput struct {
315
+type filesLsOutput struct {
316
Entries []mfs.NodeListing
317
}
318
317
-var FilesLsCmd = &cmds.Command{
319
+var filesLsCmd = &oldcmds.Command{
320
Helptext: cmdkit.HelpText{
321
Tagline: "List directories in the local mutable namespace.",
322
ShortDescription: `
@@ -341,7 +343,7 @@ Examples:
343
Options: []cmdkit.Option{
344
cmdkit.BoolOption("l", "Use long listing format."),
345
},
344
- Run: func(req cmds.Request, res cmds.Response) {
346
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
347
var arg string
348
349
if len(req.Arguments()) == 0 {
@@ -385,33 +387,33 @@ Examples:
387
Name: name,
388
})
389
}
388
- res.SetOutput(&FilesLsOutput{output})
390
+ res.SetOutput(&filesLsOutput{output})
391
} else {
392
listing, err := fsn.List(req.Context())
393
if err != nil {
394
res.SetError(err, cmdkit.ErrNormal)
395
return
396
}
395
- res.SetOutput(&FilesLsOutput{listing})
397
+ res.SetOutput(&filesLsOutput{listing})
398
}
399
return
400
case *mfs.File:
401
_, name := gopath.Split(path)
400
- out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
402
+ out := &filesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
403
res.SetOutput(out)
404
return
405
default:
406
res.SetError(errors.New("unrecognized type"), cmdkit.ErrNormal)
407
}
408
},
407
- Marshalers: cmds.MarshalerMap{
408
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
409
+ Marshalers: oldcmds.MarshalerMap{
410
+ oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
411
v, err := unwrapOutput(res.Output())
412
if err != nil {
413
return nil, err
414
}
415
414
- out, ok := v.(*FilesLsOutput)
416
+ out, ok := v.(*filesLsOutput)
417
if !ok {
418
return nil, e.TypeErr(out, v)
419
}
@@ -429,10 +431,10 @@ Examples:
431
return buf, nil
432
},
433
},
432
- Type: FilesLsOutput{},
434
+ Type: filesLsOutput{},
435
}
436
435
-var FilesReadCmd = &cmds.Command{
437
+var filesReadCmd = &oldcmds.Command{
438
Helptext: cmdkit.HelpText{
439
Tagline: "Read a file in a given mfs.",
440
ShortDescription: `
@@ -453,7 +455,7 @@ Examples:
455
cmdkit.IntOption("offset", "o", "Byte offset to begin reading from."),
456
cmdkit.IntOption("count", "n", "Maximum number of bytes to read."),
457
},
456
- Run: func(req cmds.Request, res cmds.Response) {
458
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
459
n, err := req.InvocContext().GetNode()
460
if err != nil {
461
res.SetError(err, cmdkit.ErrNormal)
@@ -544,7 +546,7 @@ func (crw *contextReaderWrapper) Read(b []byte) (int, error) {
546
return crw.R.CtxReadFull(crw.ctx, b)
547
}
548
547
-var FilesMvCmd = &cmds.Command{
549
+var filesMvCmd = &oldcmds.Command{
550
Helptext: cmdkit.HelpText{
551
Tagline: "Move files.",
552
ShortDescription: `
@@ -561,7 +563,7 @@ Example:
563
cmdkit.StringArg("source", true, false, "Source file to move."),
564
cmdkit.StringArg("dest", true, false, "Destination path for file to be moved to."),
565
},
564
- Run: func(req cmds.Request, res cmds.Response) {
566
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
567
n, err := req.InvocContext().GetNode()
568
if err != nil {
569
res.SetError(err, cmdkit.ErrNormal)
@@ -589,7 +591,7 @@ Example:
591
},
592
}
593
592
-var FilesWriteCmd = &cmds.Command{
594
+var filesWriteCmd = &oldcmds.Command{
595
Helptext: cmdkit.HelpText{
596
Tagline: "Write to a mutable file in a given filesystem.",
597
ShortDescription: `
@@ -636,7 +638,7 @@ stat' on the file or any of its ancestors.
638
cidVersionOption,
639
hashOption,
640
},
639
- Run: func(req cmds.Request, res cmds.Response) {
641
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
642
path, err := checkPath(req.StringArguments()[0])
643
if err != nil {
644
res.SetError(err, cmdkit.ErrNormal)
@@ -737,7 +739,7 @@ stat' on the file or any of its ancestors.
739
},
740
}
741
740
-var FilesMkdirCmd = &cmds.Command{
742
+var filesMkdirCmd = &oldcmds.Command{
743
Helptext: cmdkit.HelpText{
744
Tagline: "Make directories.",
745
ShortDescription: `
@@ -763,7 +765,7 @@ Examples:
765
cidVersionOption,
766
hashOption,
767
},
766
- Run: func(req cmds.Request, res cmds.Response) {
768
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
769
n, err := req.InvocContext().GetNode()
770
if err != nil {
771
res.SetError(err, cmdkit.ErrNormal)
@@ -800,7 +802,7 @@ Examples:
802
},
803
}
804
803
-var FilesFlushCmd = &cmds.Command{
805
+var filesFlushCmd = &oldcmds.Command{
806
Helptext: cmdkit.HelpText{
807
Tagline: "Flush a given path's data to disk.",
808
ShortDescription: `
@@ -811,7 +813,7 @@ are run with the '--flush=false'.
813
Arguments: []cmdkit.Argument{
814
cmdkit.StringArg("path", false, false, "Path to flush. Default: '/'."),
815
},
814
- Run: func(req cmds.Request, res cmds.Response) {
816
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
817
nd, err := req.InvocContext().GetNode()
818
if err != nil {
819
res.SetError(err, cmdkit.ErrNormal)
@@ -833,7 +835,7 @@ are run with the '--flush=false'.
835
},
836
}
837
836
-var FilesChcidCmd = &cmds.Command{
838
+var filesChcidCmd = &oldcmds.Command{
839
Helptext: cmdkit.HelpText{
840
Tagline: "Change the cid version or hash function of the root node of a given path.",
841
ShortDescription: `
@@ -847,7 +849,7 @@ Change the cid version or hash function of the root node of a given path.
849
cidVersionOption,
850
hashOption,
851
},
850
- Run: func(req cmds.Request, res cmds.Response) {
852
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
853
nd, err := req.InvocContext().GetNode()
854
if err != nil {
855
res.SetError(err, cmdkit.ErrNormal)
@@ -901,7 +903,7 @@ func updatePath(rt *mfs.Root, pth string, prefix *cid.Prefix, flush bool) error
903
return nil
904
}
905
904
-var FilesRmCmd = &cmds.Command{
906
+var filesRmCmd = &oldcmds.Command{
907
Helptext: cmdkit.HelpText{
908
Tagline: "Remove a file.",
909
ShortDescription: `
@@ -922,7 +924,7 @@ Remove files or directories.
924
Options: []cmdkit.Option{
925
cmdkit.BoolOption("recursive", "r", "Recursively remove directories."),
926
},
925
- Run: func(req cmds.Request, res cmds.Response) {
927
+ Run: func(req oldcmds.Request, res oldcmds.Response) {
928
defer res.SetOutput(nil)
929
930
nd, err := req.InvocContext().GetNode()
@@ -1007,7 +1009,7 @@ Remove files or directories.
1009
},
1010
}
1011
1010
-func getPrefix(req cmds.Request) (*cid.Prefix, error) {
1012
+func getPrefix(req oldcmds.Request) (*cid.Prefix, error) {
1013
cidVer, cidVerSet, _ := req.Option("cid-version").Int()
1014
hashFunStr, hashFunSet, _ := req.Option("hash").String()
1015
core/commands/root.go
+1
-1
@@ -109,6 +109,7 @@ var rootSubcommands = map[string]*cmds.Command{
109
"block": BlockCmd,
110
"cat": CatCmd,
111
"commands": CommandsDaemonCmd,
112
+ "files": files.FilesCmd,
113
"filestore": FileStoreCmd,
114
"get": GetCmd,
115
"pubsub": PubsubCmd,
@@ -120,7 +121,6 @@ var rootSubcommands = map[string]*cmds.Command{
121
"dht": lgc.NewCommand(DhtCmd),
122
"diag": lgc.NewCommand(DiagCmd),
123
"dns": lgc.NewCommand(DNSCmd),
123
- "files": lgc.NewCommand(files.FilesCmd),
124
"id": lgc.NewCommand(IDCmd),
125
"key": lgc.NewCommand(KeyCmd),
126
"log": lgc.NewCommand(LogCmd),