@cryptotaxi247 / kubo / commits / 9333c504c

commands/cli,http: Make sure required file arguments are provided

Matt Bell committed Nov 16, 2014 at 23:56 UTC 9333c504c12b76d1577e478e5e013b37e211ae7f
2 files changed +16
commands/cli/parse.go
+7
@@ -248,6 +248,13 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
248 argDefIndex++
249 }
250
251 + // check to make sure we didn't miss any required arguments
252 + for _, argDef := range argDefs[argDefIndex:] {
253 + if argDef.Required {
254 + return nil, nil, fmt.Errorf("Argument '%s' is required", argDef.Name)
255 + }
256 + }
257 +
258 return stringArgs, fileArgs, nil
259 }
260
commands/http/parse.go
+9
@@ -2,6 +2,7 @@ package http
2
3 import (
4 "errors"
5 + "fmt"
6 "mime"
7 "net/http"
8 "strings"
@@ -54,6 +55,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
55 args := make([]string, valCount)
56
57 valIndex := 0
58 + requiredFile := ""
59 for _, argDef := range cmd.Arguments {
60 // skip optional argument definitions if there aren't sufficient remaining values
61 if valCount-valIndex <= numRequired && !argDef.Required {
@@ -78,6 +80,8 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
80 } else {
81 break
82 }
83 + } else if argDef.Type == cmds.ArgFile && argDef.Required && len(requiredFile) == 0 {
84 + requiredFile = argDef.Name
85 }
86 }
87
@@ -101,6 +105,11 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
105 }
106 }
107
108 + // if there is a required filearg, error if no files were provided
109 + if len(requiredFile) > 0 && f == nil {
110 + return nil, fmt.Errorf("File argument '%s' is required", requiredFile)
111 + }
112 +
113 req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs)
114 if err != nil {
115 return nil, err