root.go 1.84 KB
Newer Older
1 2 3
package commands

import (
4
	"fmt"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	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},
	},
	Help: `ipfs - global versioned p2p merkledag file system

Basic commands:

    init          Initialize ipfs local configuration.
    add <path>    Add an object to ipfs.
    cat <ref>     Show ipfs object data.
    ls <ref>      List links from an object.
    refs <ref>    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 <command>" for more information about a command.
`,
	Subcommands: map[string]*cmds.Command{
		"beep": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
				v := TestOutput{"hello, world", 1337}
				res.SetValue(v)
			},
		},
		"boop": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
				v := strings.NewReader("hello, world")
				res.SetValue(v)
			},
		},
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		"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)
				}
			},
		},
75 76
	},
}