and special case that one thing because why not
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Jul 9, 2016 at 11:33 UTC
b034a0d8e8d1651649d0e8503a466c50c76ec2af
6 files changed
+46
-18
commands/cli/parse.go
+13
-10
@@ -61,6 +61,16 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
61
}
62
}
63
64
+ // This is an ugly hack to maintain our current CLI interface while fixing
65
+ // other stdin usage bugs. Let this serve as a warning, be careful about the
66
+ // choices you make, they will haunt you forever.
67
+ if len(path) == 2 && path[0] == "bootstrap" {
68
+ if (path[1] == "add" && opts["default"] == true) ||
69
+ (path[1] == "rm" && opts["all"] == true) {
70
+ stdin = nil
71
+ }
72
+ }
73
+
74
stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root)
75
if err != nil {
76
return req, cmd, path, err
@@ -301,17 +311,10 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
311
stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:]
312
} else {
313
if stdin != nil && argDef.SupportsStdin && !fillingVariadic {
304
- fname := ""
305
- istty, err := isTty(stdin)
306
- if err != nil {
307
- return nil, nil, err
308
- }
309
- if istty {
310
- fname = stdinSpecialName
314
+ if err := printReadInfo(stdin, msgStdinInfo); err == nil {
315
+ fileArgs[stdin.Name()] = files.NewReaderFile(stdinSpecialName, "", stdin, nil)
316
+ stdin = nil
317
}
312
-
313
- fileArgs[stdin.Name()] = files.NewReaderFile(fname, "", stdin, nil)
314
- stdin = nil
318
}
319
}
320
case cmds.ArgFile:
commands/http/client.go
+1
-1
@@ -128,7 +128,7 @@ func getQuery(req cmds.Request) (string, error) {
128
query.Set(k, str)
129
}
130
131
- args := req.Arguments()
131
+ args := req.StringArguments()
132
argDefs := req.Command().Arguments
133
134
argDefIndex := 0
commands/reqlog.go
+1
-1
@@ -56,7 +56,7 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry {
56
Active: true,
57
Command: strings.Join(req.Path(), "/"),
58
Options: req.Options(),
59
- Args: req.Arguments(),
59
+ Args: req.StringArguments(),
60
ID: rl.nextID,
61
req: req,
62
log: rl,
commands/request.go
+6
-5
@@ -71,6 +71,7 @@ type Request interface {
71
SetOption(name string, val interface{})
72
SetOptions(opts OptMap) error
73
Arguments() []string
74
+ StringArguments() []string
75
SetArguments([]string)
76
Files() files.File
77
SetFiles(files.File)
@@ -168,6 +169,10 @@ func (r *request) SetOptions(opts OptMap) error {
169
return r.ConvertOptions()
170
}
171
172
+func (r *request) StringArguments() []string {
173
+ return r.arguments
174
+}
175
+
176
// Arguments returns the arguments slice
177
func (r *request) Arguments() []string {
178
if r.haveVarArgsFromStdin() {
@@ -207,7 +212,7 @@ func (r *request) haveVarArgsFromStdin() bool {
212
}
213
214
last := r.cmd.Arguments[len(r.cmd.Arguments)-1]
210
- return last.SupportsStdin && last.Type == ArgString &&
215
+ return last.SupportsStdin && last.Type == ArgString && (last.Required || last.Variadic) &&
216
len(r.arguments) < len(r.cmd.Arguments)
217
}
218
@@ -234,10 +239,6 @@ func (r *request) VarArgs(f func(string) error) error {
239
return err
240
}
241
237
- if fi.FileName() == "*stdin*" {
238
- fmt.Fprintln(os.Stderr, "ipfs: Reading from stdin; send Ctrl-d to stop.")
239
- }
240
-
242
scan := bufio.NewScanner(fi)
243
for scan.Scan() {
244
err := f(scan.Text())
core/commands/id.go
+1
-1
@@ -58,7 +58,7 @@ EXAMPLE:
58
`,
59
},
60
Arguments: []cmds.Argument{
61
- cmds.StringArg("peerid", false, false, "Peer.ID of node to look up.").EnableStdin(),
61
+ cmds.StringArg("peerid", false, false, "Peer.ID of node to look up."),
62
},
63
Options: []cmds.Option{
64
cmds.StringOption("format", "f", "Optional output format."),
test/sharness/t0120-bootstrap.sh
+24
@@ -117,6 +117,30 @@ test_bootstrap_cmd() {
117
'
118
119
test_bootstrap_list_cmd
120
+
121
+ test_expect_success "'ipfs bootstrap add' accepts args from stdin" '
122
+ echo $BP1 > bpeers &&
123
+ echo $BP2 >> bpeers &&
124
+ echo $BP3 >> bpeers &&
125
+ echo $BP4 >> bpeers &&
126
+ cat bpeers | ipfs bootstrap add > add_stdin_actual
127
+ '
128
+
129
+ test_expect_success "output looks good" '
130
+ test_cmp add_stdin_actual bpeers
131
+ '
132
+
133
+ test_bootstrap_list_cmd $BP1 $BP2 $BP3 $BP4
134
+
135
+ test_expect_success "'ipfs bootstrap rm' accepts args from stdin" '
136
+ cat bpeers | ipfs bootstrap rm > rm_stdin_actual
137
+ '
138
+
139
+ test_expect_success "output looks good" '
140
+ test_cmp rm_stdin_actual bpeers
141
+ '
142
+
143
+ test_bootstrap_list_cmd
144
}
145
146
# should work offline