@cryptotaxi247 / kubo / commits / d87aad1e3

cmd/ipfs: Do command option conversion in CLI entry point

Matt Bell committed Oct 27, 2014 at 15:30 UTC d87aad1e3af5142692292cec78f6a833523f2aee
1 file changed +30 -23
cmd/ipfs/ipfs.go
+30 -23
@@ -18,46 +18,37 @@ var log = u.Logger("cmd/ipfs")
18
19 func main() {
20 args := os.Args[1:]
21 + root := Root
22
22 - req, err := cmdsCli.Parse(args, Root)
23 + req, err := cmdsCli.Parse(args, root)
24 if err != nil {
25 fmt.Println(err)
26 os.Exit(1)
27 }
28
29 + // if the CLI-specific root doesn't contain the command, use the general root
30 if len(req.Path()) == 0 {
29 - req, err = cmdsCli.Parse(args, commands.Root)
31 + root = commands.Root
32 + req, err = cmdsCli.Parse(args, root)
33 if err != nil {
34 fmt.Println(err)
35 os.Exit(1)
36 }
37 }
38
36 - var local bool // TODO: option to force local
37 - var root *cmds.Command
38 - cmd, err := Root.Get(req.Path())
39 - if err == nil {
40 - local = true
41 - root = Root
42 -
43 - } else if local {
39 + cmd, err := root.Get(req.Path())
40 + if err != nil {
41 fmt.Println(err)
42 os.Exit(1)
46 -
47 - } else {
48 - cmd, err = commands.Root.Get(req.Path())
49 - if err != nil {
50 - fmt.Println(err)
51 - os.Exit(1)
52 - }
53 -
54 - local = false
55 - root = commands.Root
43 }
44
58 - // TODO: get converted options so we can use them here (e.g. --debug, --config)
45 + options, err := getOptions(req, root)
46 + if err != nil {
47 + fmt.Println(err)
48 + os.Exit(1)
49 + }
50
60 - if debug, ok := req.Option("debug"); ok && debug.(bool) {
51 + if debug, ok := options["debug"]; ok && debug.(bool) {
52 u.Debug = true
53
54 // if debugging, setup profiling.
@@ -74,7 +65,7 @@ func main() {
65 }
66
67 var res cmds.Response
77 - if local {
68 + if root == Root {
69 // TODO: spin up node
70 res = root.Call(req)
71 } else {
@@ -101,3 +92,19 @@ func main() {
92 fmt.Println(err.Error())
93 }
94 }
95 +
96 +func getOptions(req cmds.Request, root *cmds.Command) (map[string]interface{}, error) {
97 + tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
98 +
99 + options, err := root.GetOptions(tempReq.Path())
100 + if err != nil {
101 + return nil, err
102 + }
103 +
104 + err = tempReq.ConvertOptions(options)
105 + if err != nil {
106 + return nil, err
107 + }
108 +
109 + return tempReq.Options(), nil
110 +}