files2.0: common single-file helper
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 14, 2018 at 12:05 UTC
351ed26bd813e9c7dfd02f6b9c3100df4c79ca7e
9 files changed
+76
-84
core/commands/block.go
+4
-8
@@ -11,7 +11,6 @@ import (
11
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
14
- files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
14
cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
15
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
16
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
@@ -154,12 +153,9 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
153
return err
154
}
155
157
- it := req.Files.Entries()
158
- if !it.Next() && it.Err() != nil {
159
- return it.Err()
160
- }
161
- if files.FileFromEntry(it) == nil {
162
- return fmt.Errorf("expected a regular file")
156
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
157
+ if err != nil {
158
+ return err
159
}
160
161
mhtype, _ := req.Options[mhtypeOptionName].(string)
@@ -182,7 +178,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
178
}
179
}
180
185
- p, err := api.Block().Put(req.Context, files.FileFromEntry(it), options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
181
+ p, err := api.Block().Put(req.Context, file, options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
182
if err != nil {
183
return err
184
}
core/commands/cmdenv/file.go
new
+22
@@ -0,0 +1,22 @@
1
+package cmdenv
2
+
3
+import (
4
+ "fmt"
5
+
6
+ files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
7
+)
8
+
9
+func GetFileArg(it files.DirIterator) (files.File, error) {
10
+ if !it.Next() {
11
+ err := it.Err()
12
+ if err == nil {
13
+ err = fmt.Errorf("expected a file argument")
14
+ }
15
+ return nil, err
16
+ }
17
+ file := files.FileFromEntry(it)
18
+ if file == nil {
19
+ return nil, fmt.Errorf("file argument was nil")
20
+ }
21
+ return file, nil
22
+}
core/commands/config.go
+9
-14
@@ -10,15 +10,14 @@ import (
10
"os/exec"
11
"strings"
12
13
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
14
- repo "github.com/ipfs/go-ipfs/repo"
15
- fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
13
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
14
+ "github.com/ipfs/go-ipfs/repo"
15
+ "github.com/ipfs/go-ipfs/repo/fsrepo"
16
17
"gx/ipfs/QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2/jsondiff"
18
- files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
19
- config "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
20
- cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
21
- cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
18
+ "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
19
+ "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
20
+ "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
21
)
22
23
// ConfigUpdateOutput is config profile apply command's output
@@ -281,14 +280,10 @@ can't be undone.
280
}
281
defer r.Close()
282
284
- it := req.Files.Entries()
285
- if !it.Next() && it.Err() != nil {
286
- return it.Err()
287
- }
288
- if files.FileFromEntry(it) == nil {
289
- return fmt.Errorf("expected a regular file")
283
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
284
+ if err != nil {
285
+ return err
286
}
291
- file := files.FileFromEntry(it)
287
defer file.Close()
288
289
return replaceConfig(r, file)
core/commands/dag/dag.go
+3
-2
@@ -95,10 +95,11 @@ into an object of the specified format.
95
96
it := req.Files.Entries()
97
for it.Next() {
98
- if files.FileFromEntry(it) == nil {
98
+ file := files.FileFromEntry(it)
99
+ if file == nil {
100
return fmt.Errorf("expected a regular file")
101
}
101
- nds, err := coredag.ParseInputs(ienc, format, files.FileFromEntry(it), mhType, -1)
102
+ nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1)
103
if err != nil {
104
return err
105
}
core/commands/files.go
+13
-18
@@ -10,22 +10,21 @@ import (
10
"sort"
11
"strings"
12
13
- core "github.com/ipfs/go-ipfs/core"
14
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
15
- iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13
+ "github.com/ipfs/go-ipfs/core"
14
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
15
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
16
17
- humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
17
+ "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
18
bservice "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
19
- cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
20
- mfs "gx/ipfs/QmU3iDRUrxyTYdV2j5MuWLFvP1k7w98vD66PLnNChgvUmZ/go-mfs"
21
- files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
22
- offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
23
- cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
19
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
20
+ "gx/ipfs/QmU3iDRUrxyTYdV2j5MuWLFvP1k7w98vD66PLnNChgvUmZ/go-mfs"
21
+ "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
22
+ "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
23
ft "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs"
24
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
25
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
26
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
28
- cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
27
+ "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
28
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
29
)
30
@@ -770,15 +769,11 @@ stat' on the file or any of its ancestors.
769
return err
770
}
771
773
- it := req.Files.Entries()
774
- if !it.Next() && it.Err() != nil {
775
- return it.Err()
776
- }
777
- if files.FileFromEntry(it) == nil {
778
- return fmt.Errorf("expected a regular file")
772
+ var r io.Reader
773
+ r, err = cmdenv.GetFileArg(req.Files.Entries())
774
+ if err != nil {
775
+ return err
776
}
780
-
781
- var r io.Reader = files.FileFromEntry(it)
777
if countfound {
778
r = io.LimitReader(r, int64(count))
779
}
core/commands/object/object.go
+8
-12
@@ -8,16 +8,15 @@ import (
8
"io/ioutil"
9
"text/tabwriter"
10
11
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
14
15
- cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
16
- files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
17
- cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
15
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
16
+ "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
17
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
18
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
20
- cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
19
+ "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
20
)
21
22
type Node struct {
@@ -392,12 +391,9 @@ And then run:
391
return err
392
}
393
395
- it := req.Files.Entries()
396
- if !it.Next() && it.Err() != nil {
397
- return it.Err()
398
- }
399
- if files.FileFromEntry(it) == nil {
400
- return fmt.Errorf("expected a regular file")
394
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
395
+ if err != nil {
396
+ return err
397
}
398
399
inputenc, _ := req.Options["inputenc"].(string)
@@ -415,7 +411,7 @@ And then run:
411
return err
412
}
413
418
- p, err := api.Object().Put(req.Context, files.FileFromEntry(it),
414
+ p, err := api.Object().Put(req.Context, file,
415
options.Object.DataType(datafieldenc),
416
options.Object.InputEnc(inputenc),
417
options.Object.Pin(dopin))
core/commands/object/patch.go
+8
-15
@@ -8,7 +8,6 @@ import (
8
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
11
- "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
11
"gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
12
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13
)
@@ -61,15 +60,12 @@ the limit will not be respected by the network.
60
return err
61
}
62
64
- it := req.Files.Entries()
65
- if !it.Next() && it.Err() != nil {
66
- return it.Err()
67
- }
68
- if files.FileFromEntry(it) == nil {
69
- return fmt.Errorf("expected a regular file")
63
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
64
+ if err != nil {
65
+ return err
66
}
67
72
- p, err := api.Object().AppendData(req.Context, root, files.FileFromEntry(it))
68
+ p, err := api.Object().AppendData(req.Context, root, file)
69
if err != nil {
70
return err
71
}
@@ -111,15 +107,12 @@ Example:
107
return err
108
}
109
114
- it := req.Files.Entries()
115
- if !it.Next() && it.Err() != nil {
116
- return it.Err()
117
- }
118
- if files.FileFromEntry(it) == nil {
119
- return fmt.Errorf("expected a regular file")
110
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
111
+ if err != nil {
112
+ return err
113
}
114
122
- p, err := api.Object().SetData(req.Context, root, files.FileFromEntry(it))
115
+ p, err := api.Object().SetData(req.Context, root, file)
116
if err != nil {
117
return err
118
}
core/commands/tar.go
+8
-11
@@ -4,16 +4,15 @@ import (
4
"fmt"
5
"io"
6
7
- core "github.com/ipfs/go-ipfs/core"
8
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
7
+ "github.com/ipfs/go-ipfs/core"
8
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
tar "github.com/ipfs/go-ipfs/tar"
11
12
- files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
12
"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
14
- cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
13
+ "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
14
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
16
- cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
15
+ "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
16
)
17
18
var TarCmd = &cmds.Command{
@@ -46,14 +45,12 @@ represent it.
45
}
46
47
it := req.Files.Entries()
49
- if !it.Next() && it.Err() != nil {
50
- return it.Err()
51
- }
52
- if files.FileFromEntry(it) == nil {
53
- return fmt.Errorf("expected a regular file")
48
+ file, err := cmdenv.GetFileArg(it)
49
+ if err != nil {
50
+ return err
51
}
52
56
- node, err := tar.ImportTar(req.Context, files.FileFromEntry(it), nd.DAG)
53
+ node, err := tar.ImportTar(req.Context, file, nd.DAG)
54
if err != nil {
55
return err
56
}
core/coreunix/add.go
+1
-4
@@ -547,11 +547,8 @@ func (adder *Adder) addDir(path string, dir files.Directory) error {
547
return err
548
}
549
}
550
- if it.Err() != nil {
551
- return it.Err()
552
- }
550
554
- return nil
551
+ return it.Err()
552
}
553
554
func (adder *Adder) maybePauseForGC() error {