提交 24814a41 作者: Matt Bell 提交者: Juan Batiz-Benet

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

上级 6ae09b16
...@@ -50,65 +50,67 @@ Plumbing commands: ...@@ -50,65 +50,67 @@ Plumbing commands:
Use "ipfs help <command>" for more information about a command. Use "ipfs help <command>" for more information about a command.
`, `,
Subcommands: map[string]*cmds.Command{ }
"cat": catCmd,
"ls": lsCmd, var rootSubcommands = map[string]*cmds.Command{
"init": initCmd, "cat": catCmd,
"ls": lsCmd,
// test subcommands "commands": commandsCmd,
// TODO: remove these when we don't need them anymore "init": initCmd,
"beep": &cmds.Command{ "daemon": daemonCmd,
Run: func(res cmds.Response, req cmds.Request) {
v := &TestOutput{"hello, world", 1337} // test subcommands
res.SetValue(v) // TODO: remove these when we don't need them anymore
}, "beep": &cmds.Command{
Format: func(res cmds.Response) (string, error) { Run: func(res cmds.Response, req cmds.Request) {
v := res.Value().(*TestOutput) v := &TestOutput{"hello, world", 1337}
s := fmt.Sprintf("Foo: %s\n", v.Foo) res.SetValue(v)
s += fmt.Sprintf("Bar: %v\n", v.Bar) },
return s, nil Format: func(res cmds.Response) (string, error) {
}, v := res.Value().(*TestOutput)
Type: &TestOutput{}, s := fmt.Sprintf("Foo: %s\n", v.Foo)
s += fmt.Sprintf("Bar: %v\n", v.Bar)
return s, nil
},
Type: &TestOutput{},
},
"boop": &cmds.Command{
Run: func(res cmds.Response, req cmds.Request) {
v := strings.NewReader("hello, world")
res.SetValue(v)
}, },
"boop": &cmds.Command{ },
Run: func(res cmds.Response, req cmds.Request) { "warp": &cmds.Command{
v := strings.NewReader("hello, world") Options: []cmds.Option{
res.SetValue(v) cmds.Option{[]string{"power", "p"}, cmds.Float},
},
}, },
"warp": &cmds.Command{ Run: func(res cmds.Response, req cmds.Request) {
Options: []cmds.Option{ threshold := 1.21
cmds.Option{[]string{"power", "p"}, cmds.Float},
}, if power, found := req.Option("power"); found && power.(float64) >= threshold {
Run: func(res cmds.Response, req cmds.Request) { res.SetValue(struct {
threshold := 1.21 Status string
Power float64
if power, found := req.Option("power"); found && power.(float64) >= threshold { }{"Flux capacitor activated!", power.(float64)})
res.SetValue(struct {
Status string } else {
Power float64 err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold)
}{"Flux capacitor activated!", power.(float64)}) res.SetError(err, cmds.ErrClient)
}
} else {
err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold)
res.SetError(err, cmds.ErrClient)
}
},
}, },
"args": &cmds.Command{ },
Run: func(res cmds.Response, req cmds.Request) { "args": &cmds.Command{
res.SetValue(req.Arguments()) Run: func(res cmds.Response, req cmds.Request) {
}, res.SetValue(req.Arguments())
}, },
"echo": &cmds.Command{ },
Run: func(res cmds.Response, req cmds.Request) { "echo": &cmds.Command{
res.SetValue(req.Stream()) Run: func(res cmds.Response, req cmds.Request) {
}, res.SetValue(req.Stream())
}, },
}, },
} }
func init() { func init() {
Root.Subcommands["daemon"] = daemonCmd Root.Subcommands = rootSubcommands
Root.Subcommands["commands"] = commandsCmd
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论