@cryptotaxi247 / kubo / commits / 201ad30a7

cmd/ipfs: Run CLI-specific subcommands locally in CLI entry point

Matt Bell committed Oct 23, 2014 at 19:26 UTC 201ad30a7b947078646c030ebd65b2eb215b097f
1 file changed +40 -10
cmd/ipfs/ipfs.go
+40 -10
@@ -21,26 +21,46 @@ var log = u.Logger("cmd/ipfs")
21 const API_PATH = "/api/v0"
22
23 func main() {
24 - req, err := cli.Parse(os.Args[1:], commands.Root)
24 + args := os.Args[1:]
25 +
26 + req, err := cli.Parse(args, Root)
27 if err != nil {
28 fmt.Println(err)
29 os.Exit(1)
30 }
31
30 - // TODO: call command locally if option tells us to, or if command is CLI-only (e.g. ipfs init)
31 -
32 - cmd, err := commands.Root.Get(req.Path())
33 - if err != nil {
34 - fmt.Println(err)
35 - os.Exit(1)
32 + if len(req.Path()) == 0 {
33 + req, err = cli.Parse(args, commands.Root)
34 + if err != nil {
35 + fmt.Println(err)
36 + os.Exit(1)
37 + }
38 }
39
38 - res, err := sendRequest(req)
39 - if err != nil {
40 + var local bool // TODO: option to force local
41 + var root *cmds.Command
42 + cmd, err := Root.Get(req.Path())
43 + if err == nil {
44 + local = true
45 + root = Root
46 +
47 + } else if local {
48 fmt.Println(err)
49 os.Exit(1)
50 +
51 + } else {
52 + cmd, err = commands.Root.Get(req.Path())
53 + if err != nil {
54 + fmt.Println(err)
55 + os.Exit(1)
56 + }
57 +
58 + local = false
59 + root = commands.Root
60 }
61
62 + // TODO: get converted options so we can use them here (e.g. --debug, --config)
63 +
64 if debug, ok := req.Option("debug"); ok && debug.(bool) {
65 u.Debug = true
66
@@ -57,7 +77,17 @@ func main() {
77 }
78 }
79
60 - //res := commands.Root.Call(req)
80 + var res cmds.Response
81 + if local {
82 + // TODO: spin up node
83 + res = root.Call(req)
84 + } else {
85 + res, err = sendRequest(req)
86 + if err != nil {
87 + fmt.Println(err)
88 + os.Exit(1)
89 + }
90 + }
91
92 if res.Error() != nil {
93 fmt.Println(res.Error().Error())