package commands import ( "fmt" cmds "github.com/jbenet/go-ipfs/commands" "strings" ) type TestOutput struct { Foo string Bar int } var Root = &cmds.Command{ Options: []cmds.Option{ cmds.Option{[]string{"config", "c"}, cmds.String}, cmds.Option{[]string{"debug", "D"}, cmds.Bool}, cmds.Option{[]string{"help", "h"}, cmds.Bool}, cmds.Option{[]string{"local", "L"}, cmds.Bool}, }, Help: `ipfs - global versioned p2p merkledag file system Basic commands: init Initialize ipfs local configuration. add Add an object to ipfs. cat Show ipfs object data. ls List links from an object. refs List link hashes from an object. Tool commands: config Manage configuration. version Show ipfs version information. commands List all available commands. Advanced Commands: mount Mount an ipfs read-only mountpoint. serve Serve an interface to ipfs. net-diag Print network diagnostic. Use "ipfs help " for more information about a command. `, Subcommands: map[string]*cmds.Command{ "cat": cat, // test subcommands // TODO: remove these when we don't need them anymore "beep": &cmds.Command{ Run: func(req cmds.Request, res cmds.Response) { v := &TestOutput{"hello, world", 1337} res.SetValue(v) }, Format: func(res cmds.Response) (string, error) { v := res.Value().(*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(req cmds.Request, res cmds.Response) { v := strings.NewReader("hello, world") res.SetValue(v) }, }, "warp": &cmds.Command{ Options: []cmds.Option{ cmds.Option{[]string{"power", "p"}, cmds.Float}, }, Run: func(req cmds.Request, res cmds.Response) { threshold := 1.21 if power, found := req.Option("power"); found && power.(float64) >= threshold { res.SetValue(struct { Status string Power float64 }{"Flux capacitor activated!", power.(float64)}) } else { err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold) res.SetError(err, cmds.ErrClient) } }, }, "args": &cmds.Command{ Run: func(req cmds.Request, res cmds.Response) { res.SetValue(req.Arguments()) }, }, "echo": &cmds.Command{ Run: func(req cmds.Request, res cmds.Response) { res.SetValue(req.Stream()) }, }, }, }