@cryptotaxi247 / kubo / commits / 3dd7a9a5c

cmd/ipfs2: Broke up main into subfunctions

Matt Bell committed Nov 3, 2014 at 22:34 UTC 3dd7a9a5c8d261e19565c925e20178f0ddffefaa
1 file changed +60 -30
cmd/ipfs2/main.go
+60 -30
@@ -21,7 +21,13 @@ var log = u.Logger("cmd/ipfs")
21
22 func main() {
23 args := os.Args[1:]
24 + req, root := createRequest(args)
25 + handleOptions(req, root)
26 + res := callCommand(req, root)
27 + outputResponse(res)
28 +}
29
30 +func createRequest(args []string) (cmds.Request, *cmds.Command) {
31 req, root, err := cmdsCli.Parse(args, Root, commands.Root)
32 if err != nil {
33 fmt.Println(err)
@@ -35,6 +41,40 @@ func main() {
41 os.Exit(1)
42 }
43
44 + configPath, err := getConfigRoot(options)
45 + if err != nil {
46 + fmt.Println(err)
47 + os.Exit(1)
48 + }
49 +
50 + conf, err := getConfig(configPath)
51 + if err != nil {
52 + fmt.Println(err)
53 + os.Exit(1)
54 + }
55 +
56 + ctx := req.Context()
57 + ctx.ConfigRoot = configPath
58 + ctx.Config = conf
59 +
60 + if _, found := options.Option("encoding"); !found {
61 + if req.Command().Format != nil {
62 + req.SetOption("encoding", cmds.Text)
63 + } else {
64 + req.SetOption("encoding", cmds.JSON)
65 + }
66 + }
67 +
68 + return req, root
69 +}
70 +
71 +func handleOptions(req cmds.Request, root *cmds.Command) {
72 + options, err := getOptions(req, root)
73 + if err != nil {
74 + fmt.Println(err)
75 + os.Exit(1)
76 + }
77 +
78 if help, found := options.Option("help"); found {
79 if helpBool, ok := help.(bool); helpBool && ok {
80 fmt.Println(req.Command().Help)
@@ -65,39 +105,25 @@ func main() {
105 os.Exit(1)
106 }
107 }
108 +}
109
69 - configPath, err := getConfigRoot(options)
70 - if err != nil {
71 - fmt.Println(err)
72 - os.Exit(1)
73 - }
74 -
75 - conf, err := getConfig(configPath)
76 - if err != nil {
77 - fmt.Println(err)
78 - os.Exit(1)
79 - }
80 -
81 - ctx := req.Context()
82 - ctx.ConfigRoot = configPath
83 - ctx.Config = conf
84 -
85 - if _, found := options.Option("encoding"); !found {
86 - if req.Command().Format != nil {
87 - req.SetOption("encoding", cmds.Text)
88 - } else {
89 - req.SetOption("encoding", cmds.JSON)
90 - }
91 - }
92 -
110 +func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
111 var res cmds.Response
112 +
113 if root == Root {
114 res = root.Call(req)
115
116 } else {
117 + options, err := getOptions(req, root)
118 + if err != nil {
119 + fmt.Println(err)
120 + os.Exit(1)
121 + }
122 +
123 var found bool
124 + var local interface{}
125 localBool := false
100 - if local, found := options.Option("local"); found {
126 + if local, found = options.Option("local"); found {
127 var ok bool
128 localBool, ok = local.(bool)
129 if !ok {
@@ -106,7 +132,7 @@ func main() {
132 }
133 }
134
109 - if (!found || !localBool) && daemon.Locked(configPath) {
135 + if (!found || !localBool) && daemon.Locked(req.Context().ConfigRoot) {
136 res, err = cmdsHttp.Send(req)
137 if err != nil {
138 fmt.Println(err)
@@ -114,23 +140,27 @@ func main() {
140 }
141
142 } else {
117 - node, err := core.NewIpfsNode(conf, false)
143 + node, err := core.NewIpfsNode(req.Context().Config, false)
144 if err != nil {
145 fmt.Println(err)
146 os.Exit(1)
147 }
122 - ctx.Node = node
148 + req.Context().Node = node
149
150 res = root.Call(req)
151 }
152 }
153
154 + return res
155 +}
156 +
157 +func outputResponse(res cmds.Response) {
158 if res.Error() != nil {
159 fmt.Println(res.Error().Error())
160
131 - if req.Command().Help != "" && res.Error().Code == cmds.ErrClient {
161 + if res.Request().Command().Help != "" && res.Error().Code == cmds.ErrClient {
162 // TODO: convert from markdown to ANSI terminal format?
133 - fmt.Println(req.Command().Help)
163 + fmt.Println(res.Request().Command().Help)
164 }
165
166 os.Exit(1)