@cryptotaxi247 / kubo / commits / d700a2ce8

cmd/ipfs2: Cleaned up main option checking (no longer need temporary Request)

Matt Bell committed Nov 10, 2014 at 19:23 UTC d700a2ce879b5f0e65ba5fb2e4845d8e9d7b0d4a
1 file changed +8 -48
cmd/ipfs2/main.go
+8 -48
@@ -59,28 +59,16 @@ func main() {
59 func createRequest(args []string) (cmds.Request, *cmds.Command) {
60 req, root, cmd, path, err := cmdsCli.Parse(args, Root, commands.Root)
61
62 - var options cmds.Request
63 - if req != nil && root != nil {
64 - var err2 error
65 - options, err2 = getOptions(req, root)
66 - if err2 != nil {
67 - fmt.Println(err2)
68 - exit(1)
69 - }
70 - }
71 -
62 // handle parse error (which means the commandline input was wrong,
63 // e.g. incorrect number of args, or nonexistent subcommand)
64 if err != nil {
65 // if the -help flag wasn't specified, show the error message
66 // or if a path was returned (user specified a valid subcommand), show the error message
67 // (this means there was an option or argument error)
78 - if options != nil || path != nil && len(path) > 0 {
68 + if path != nil && len(path) > 0 {
69 help := false
80 - if options != nil {
81 - opt, _ := options.Option("help")
82 - help, _ = opt.(bool)
83 - }
70 + opt, _ := req.Option("help")
71 + help, _ = opt.(bool)
72
73 if !help {
74 fmt.Printf(errorFormat, err)
@@ -104,7 +92,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
92 exit(1)
93 }
94
107 - configPath, err := getConfigRoot(options)
95 + configPath, err := getConfigRoot(req)
96 if err != nil {
97 fmt.Println(err)
98 exit(1)
@@ -120,7 +108,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
108 ctx.ConfigRoot = configPath
109 ctx.Config = conf
110
123 - if _, found := options.Option("encoding"); !found {
111 + if _, found := req.Option("encoding"); !found {
112 if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
113 req.SetOption("encoding", cmds.Text)
114 } else {
@@ -132,13 +120,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
120 }
121
122 func handleOptions(req cmds.Request, root *cmds.Command) {
135 - options, err := getOptions(req, root)
136 - if err != nil {
137 - fmt.Println(err)
138 - exit(1)
139 - }
140 -
141 - if help, found := options.Option("help"); found {
123 + if help, found := req.Option("help"); found {
124 if helpBool, ok := help.(bool); helpBool && ok {
125 helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
126 if err != nil {
@@ -153,7 +135,7 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
135 }
136 }
137
156 - if debug, found := options.Option("debug"); found {
138 + if debug, found := req.Option("debug"); found {
139 if debugBool, ok := debug.(bool); debugBool && ok {
140 u.Debug = true
141
@@ -172,16 +154,10 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
154 res = root.Call(req)
155
156 } else {
175 - options, err := getOptions(req, root)
176 - if err != nil {
177 - fmt.Println(err)
178 - exit(1)
179 - }
180 -
157 var found bool
158 var local interface{}
159 localBool := false
184 - if local, found = options.Option("local"); found {
160 + if local, found = req.Option("local"); found {
161 var ok bool
162 localBool, ok = local.(bool)
163 if !ok {
@@ -252,22 +228,6 @@ func outputResponse(res cmds.Response, root *cmds.Command) {
228 io.Copy(os.Stdout, out)
229 }
230
255 -func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
256 - tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
257 -
258 - options, err := root.GetOptions(tempReq.Path())
259 - if err != nil {
260 - return nil, err
261 - }
262 -
263 - err = tempReq.ConvertOptions(options)
264 - if err != nil {
265 - return nil, err
266 - }
267 -
268 - return tempReq, nil
269 -}
270 -
231 func getConfigRoot(req cmds.Request) (string, error) {
232 if opt, found := req.Option("config"); found {
233 if optStr, ok := opt.(string); ok {