@cryptotaxi247 / kubo / commits / 24814a411

core/commands2: Set root subcommands in init to prevent initialization loops

Matt Bell committed Oct 31, 2014 at 13:47 UTC 24814a411f294f25c8c6447135adf32997f64d1d
1 file changed +54 -52
core/commands2/root.go
+54 -52
@@ -50,65 +50,67 @@ Plumbing commands:
50
51 Use "ipfs help <command>" for more information about a command.
52 `,
53 - Subcommands: map[string]*cmds.Command{
54 - "cat": catCmd,
55 - "ls": lsCmd,
56 - "init": initCmd,
57 -
58 - // test subcommands
59 - // TODO: remove these when we don't need them anymore
60 - "beep": &cmds.Command{
61 - Run: func(res cmds.Response, req cmds.Request) {
62 - v := &TestOutput{"hello, world", 1337}
63 - res.SetValue(v)
64 - },
65 - Format: func(res cmds.Response) (string, error) {
66 - v := res.Value().(*TestOutput)
67 - s := fmt.Sprintf("Foo: %s\n", v.Foo)
68 - s += fmt.Sprintf("Bar: %v\n", v.Bar)
69 - return s, nil
70 - },
71 - Type: &TestOutput{},
53 +}
54 +
55 +var rootSubcommands = map[string]*cmds.Command{
56 + "cat": catCmd,
57 + "ls": lsCmd,
58 + "commands": commandsCmd,
59 + "init": initCmd,
60 + "daemon": daemonCmd,
61 +
62 + // test subcommands
63 + // TODO: remove these when we don't need them anymore
64 + "beep": &cmds.Command{
65 + Run: func(res cmds.Response, req cmds.Request) {
66 + v := &TestOutput{"hello, world", 1337}
67 + res.SetValue(v)
68 + },
69 + Format: func(res cmds.Response) (string, error) {
70 + v := res.Value().(*TestOutput)
71 + s := fmt.Sprintf("Foo: %s\n", v.Foo)
72 + s += fmt.Sprintf("Bar: %v\n", v.Bar)
73 + return s, nil
74 + },
75 + Type: &TestOutput{},
76 + },
77 + "boop": &cmds.Command{
78 + Run: func(res cmds.Response, req cmds.Request) {
79 + v := strings.NewReader("hello, world")
80 + res.SetValue(v)
81 },
73 - "boop": &cmds.Command{
74 - Run: func(res cmds.Response, req cmds.Request) {
75 - v := strings.NewReader("hello, world")
76 - res.SetValue(v)
77 - },
82 + },
83 + "warp": &cmds.Command{
84 + Options: []cmds.Option{
85 + cmds.Option{[]string{"power", "p"}, cmds.Float},
86 },
79 - "warp": &cmds.Command{
80 - Options: []cmds.Option{
81 - cmds.Option{[]string{"power", "p"}, cmds.Float},
82 - },
83 - Run: func(res cmds.Response, req cmds.Request) {
84 - threshold := 1.21
85 -
86 - if power, found := req.Option("power"); found && power.(float64) >= threshold {
87 - res.SetValue(struct {
88 - Status string
89 - Power float64
90 - }{"Flux capacitor activated!", power.(float64)})
91 -
92 - } else {
93 - err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold)
94 - res.SetError(err, cmds.ErrClient)
95 - }
96 - },
87 + Run: func(res cmds.Response, req cmds.Request) {
88 + threshold := 1.21
89 +
90 + if power, found := req.Option("power"); found && power.(float64) >= threshold {
91 + res.SetValue(struct {
92 + Status string
93 + Power float64
94 + }{"Flux capacitor activated!", power.(float64)})
95 +
96 + } else {
97 + err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold)
98 + res.SetError(err, cmds.ErrClient)
99 + }
100 },
98 - "args": &cmds.Command{
99 - Run: func(res cmds.Response, req cmds.Request) {
100 - res.SetValue(req.Arguments())
101 - },
101 + },
102 + "args": &cmds.Command{
103 + Run: func(res cmds.Response, req cmds.Request) {
104 + res.SetValue(req.Arguments())
105 },
103 - "echo": &cmds.Command{
104 - Run: func(res cmds.Response, req cmds.Request) {
105 - res.SetValue(req.Stream())
106 - },
106 + },
107 + "echo": &cmds.Command{
108 + Run: func(res cmds.Response, req cmds.Request) {
109 + res.SetValue(req.Stream())
110 },
111 },
112 }
113
114 func init() {
112 - Root.Subcommands["daemon"] = daemonCmd
113 - Root.Subcommands["commands"] = commandsCmd
115 + Root.Subcommands = rootSubcommands
116 }