coreapi unixfs: stdin-name option
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 3, 2018 at 23:17 UTC
f9cf84965dc88eaed9fc09d92e36f72d214e2994
3 files changed
+43
-5
core/coreapi/interface/options/unixfs.go
+15
-4
@@ -32,8 +32,9 @@ type UnixfsAddSettings struct {
32
OnlyHash bool
33
Local bool
34
35
- Wrap bool
36
- Hidden bool
35
+ Wrap bool
36
+ Hidden bool
37
+ StdinName string
38
}
39
40
type UnixfsAddOption func(*UnixfsAddSettings) error
@@ -55,8 +56,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
56
OnlyHash: false,
57
Local: false,
58
58
- Wrap: false,
59
- Hidden: false,
59
+ Wrap: false,
60
+ Hidden: false,
61
+ StdinName: "",
62
}
63
64
for _, opt := range opts {
@@ -225,3 +227,12 @@ func (unixfsOpts) Hidden(hidden bool) UnixfsAddOption {
227
return nil
228
}
229
}
230
+
231
+// StdinName is the name set for files which don specify FilePath as
232
+// os.Stdin.Name()
233
+func (unixfsOpts) StdinName(name string) UnixfsAddOption {
234
+ return func(settings *UnixfsAddSettings) error {
235
+ settings.StdinName = name
236
+ return nil
237
+ }
238
+}
core/coreapi/unixfs.go
+1
-1
@@ -71,7 +71,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
71
fileAdder.Silent = true
72
fileAdder.RawLeaves = settings.RawLeaves
73
//fileAdder.NoCopy = nocopy
74
- //fileAdder.Name = pathName
74
+ fileAdder.Name = settings.StdinName
75
fileAdder.CidBuilder = prefix
76
77
switch settings.Layout {
core/coreapi/unixfs_test.go
+27
@@ -8,6 +8,7 @@ import (
8
"io"
9
"io/ioutil"
10
"math"
11
+ "os"
12
"strings"
13
"testing"
14
@@ -288,6 +289,32 @@ func TestAdd(t *testing.T) {
289
expect: wrapped,
290
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
291
},
292
+ {
293
+ name: "stdinWrapped",
294
+ path: "/ipfs/QmU3r81oZycjHS9oaSHw37ootMFuFUw1DvMLKXPsezdtqU",
295
+ data: func() files.File {
296
+ return files.NewReaderFile("", os.Stdin.Name(), ioutil.NopCloser(strings.NewReader(helloStr)), nil)
297
+ },
298
+ expect: func(files.File) files.File {
299
+ return files.NewSliceFile("", "", []files.File{
300
+ files.NewReaderFile("QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk", "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk", ioutil.NopCloser(strings.NewReader(helloStr)), nil),
301
+ })
302
+ },
303
+ opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
304
+ },
305
+ {
306
+ name: "stdinNamed",
307
+ path: "/ipfs/QmQ6cGBmb3ZbdrQW1MRm1RJnYnaxCqfssz7CrTa9NEhQyS",
308
+ data: func() files.File {
309
+ return files.NewReaderFile("", os.Stdin.Name(), ioutil.NopCloser(strings.NewReader(helloStr)), nil)
310
+ },
311
+ expect: func(files.File) files.File {
312
+ return files.NewSliceFile("", "", []files.File{
313
+ files.NewReaderFile("test", "test", ioutil.NopCloser(strings.NewReader(helloStr)), nil),
314
+ })
315
+ },
316
+ opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.StdinName("test")},
317
+ },
318
{
319
name: "twoLevelDirWrapped",
320
data: twoLevelDir(),