@cryptotaxi247 / kubo / commits / 7c1e45786

cmd/ipfs2: Stricter option type coercion in main

Matt Bell committed Nov 3, 2014 at 22:09 UTC 7c1e45786a1fc36fc8bc2e595310c4ee4e62fec1
1 file changed +41 -18
cmd/ipfs2/main.go
+41 -18
@@ -35,24 +35,34 @@ func main() {
35 os.Exit(1)
36 }
37
38 - if help, found := options.Option("help"); found && help.(bool) {
39 - fmt.Println(req.Command().Help)
40 - os.Exit(0)
38 + if help, found := options.Option("help"); found {
39 + if helpBool, ok := help.(bool); helpBool && ok {
40 + fmt.Println(req.Command().Help)
41 + os.Exit(0)
42 + } else if !ok {
43 + fmt.Println("error: expected 'help' option to be a bool")
44 + os.Exit(1)
45 + }
46 }
47
43 - if debug, found := options.Option("debug"); found && debug.(bool) {
44 - u.Debug = true
45 -
46 - // if debugging, setup profiling.
47 - if u.Debug {
48 - ofi, err := os.Create("cpu.prof")
49 - if err != nil {
50 - fmt.Println(err)
51 - return
48 + if debug, found := options.Option("debug"); found {
49 + if debugBool, ok := debug.(bool); debugBool && ok {
50 + u.Debug = true
51 +
52 + // if debugging, setup profiling.
53 + if u.Debug {
54 + ofi, err := os.Create("cpu.prof")
55 + if err != nil {
56 + fmt.Println(err)
57 + return
58 + }
59 + pprof.StartCPUProfile(ofi)
60 + defer ofi.Close()
61 + defer pprof.StopCPUProfile()
62 }
53 - pprof.StartCPUProfile(ofi)
54 - defer ofi.Close()
55 - defer pprof.StopCPUProfile()
63 + } else if !ok {
64 + fmt.Println("error: expected 'debug' option to be a bool")
65 + os.Exit(1)
66 }
67 }
68
@@ -85,9 +95,18 @@ func main() {
95 res = root.Call(req)
96
97 } else {
88 - local, found := options.Option("local")
98 + var found bool
99 + localBool := false
100 + if local, found := options.Option("local"); found {
101 + var ok bool
102 + localBool, ok = local.(bool)
103 + if !ok {
104 + fmt.Println("error: expected 'local' option to be a bool")
105 + os.Exit(1)
106 + }
107 + }
108
90 - if (!found || !local.(bool)) && daemon.Locked(configPath) {
109 + if (!found || !localBool) && daemon.Locked(configPath) {
110 res, err = cmdsHttp.Send(req)
111 if err != nil {
112 fmt.Println(err)
@@ -144,7 +163,11 @@ func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
163
164 func getConfigRoot(req cmds.Request) (string, error) {
165 if opt, found := req.Option("config"); found {
147 - return opt.(string), nil
166 + if optStr, ok := opt.(string); ok {
167 + return optStr, nil
168 + } else {
169 + return "", fmt.Errorf("Expected 'config' option to be a string")
170 + }
171 }
172
173 configPath, err := config.PathRoot()