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

import (
4
	"fmt"
5
	"strings"
6 7

	cmds "github.com/jbenet/go-ipfs/commands"
8 9 10 11 12 13 14 15 16 17 18
)

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},
19
		cmds.Option{[]string{"help", "h"}, cmds.Bool},
20
		cmds.Option{[]string{"local", "L"}, cmds.Bool},
21 22 23 24 25 26 27 28 29 30 31 32 33 34
	},
	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.
35
    update        Download and apply go-ipfs updates.
36 37 38 39 40 41 42
    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.
43 44 45 46 47 48 49
    net-diag      Print network diagnostic

Plumbing commands:

    block         Interact with raw blocks in the datastore
    object        Interact with raw dag nodes

50 51 52 53

Use "ipfs help <command>" for more information about a command.
`,
	Subcommands: map[string]*cmds.Command{
Matt Bell's avatar
Matt Bell committed
54 55 56 57
		"cat": cat,

		// test subcommands
		// TODO: remove these when we don't need them anymore
58 59
		"beep": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
60
				v := &TestOutput{"hello, world", 1337}
61 62
				res.SetValue(v)
			},
63
			Format: func(res cmds.Response) (string, error) {
64
				v := res.Value().(*TestOutput)
65 66 67 68
				s := fmt.Sprintf("Foo: %s\n", v.Foo)
				s += fmt.Sprintf("Bar: %v\n", v.Bar)
				return s, nil
			},
69
			Type: &TestOutput{},
70 71 72 73 74 75 76
		},
		"boop": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
				v := strings.NewReader("hello, world")
				res.SetValue(v)
			},
		},
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
		"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)
				}
			},
		},
96 97 98 99 100
		"args": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
				res.SetValue(req.Arguments())
			},
		},
101 102 103 104 105
		"echo": &cmds.Command{
			Run: func(req cmds.Request, res cmds.Response) {
				res.SetValue(req.Stream())
			},
		},
106 107
	},
}