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

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

	cmds "github.com/jbenet/go-ipfs/commands"
Matt Bell's avatar
Matt Bell committed
8
	u "github.com/jbenet/go-ipfs/util"
9 10
)

Matt Bell's avatar
Matt Bell committed
11 12
var log = u.Logger("core/commands")

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

Plumbing commands:

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

53 54 55

Use "ipfs help <command>" for more information about a command.
`,
56 57 58
}

var rootSubcommands = map[string]*cmds.Command{
59 60 61 62 63 64
	"cat":       catCmd,
	"ls":        lsCmd,
	"commands":  commandsCmd,
	"name":      nameCmd,
	"add":       addCmd,
	"log":       logCmd,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65
	"diag":      diagCmd,
66 67 68 69
	"pin":       pinCmd,
	"version":   versionCmd,
	"config":    configCmd,
	"bootstrap": bootstrapCmd,
70
	"mount":     mountCmd,
71
	"block":     blockCmd,
72
	"update":    updateCmd,
73 74 75 76 77 78

	// test subcommands
	// TODO: remove these when we don't need them anymore
	"beep": &cmds.Command{
		Run: func(res cmds.Response, req cmds.Request) {
			v := &TestOutput{"hello, world", 1337}
Matt Bell's avatar
Matt Bell committed
79
			log.Info("beep")
80
			res.SetOutput(v)
81
		},
82 83 84 85 86 87 88
		Marshallers: map[cmds.EncodingType]cmds.Marshaller{
			cmds.Text: func(res cmds.Response) ([]byte, error) {
				v := res.Output().(*TestOutput)
				s := fmt.Sprintf("Foo: %s\n", v.Foo)
				s += fmt.Sprintf("Bar: %v\n", v.Bar)
				return []byte(s), nil
			},
89 90 91
		},
		Type: &TestOutput{},
	},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
92
	// TODO rm
93 94 95
	"boop": &cmds.Command{
		Run: func(res cmds.Response, req cmds.Request) {
			v := strings.NewReader("hello, world")
96
			res.SetOutput(v)
97
		},
98
	},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
99
	// TODO rm
100 101 102
	"warp": &cmds.Command{
		Options: []cmds.Option{
			cmds.Option{[]string{"power", "p"}, cmds.Float},
103
		},
104 105 106 107
		Run: func(res cmds.Response, req cmds.Request) {
			threshold := 1.21

			if power, found := req.Option("power"); found && power.(float64) >= threshold {
108
				res.SetOutput(struct {
109 110 111 112 113 114 115 116
					Status string
					Power  float64
				}{"Flux capacitor activated!", power.(float64)})

			} else {
				err := fmt.Errorf("Insufficient power (%v jiggawatts required)", threshold)
				res.SetError(err, cmds.ErrClient)
			}
117
		},
118
	},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
119
	// TODO rm
120 121
	"args": &cmds.Command{
		Run: func(res cmds.Response, req cmds.Request) {
122
			res.SetOutput(req.Arguments())
123
		},
124
	},
125
}
126 127

func init() {
128
	Root.Subcommands = rootSubcommands
Matt Bell's avatar
Matt Bell committed
129
	u.SetLogLevel("core/commands", "info")
130
}
131 132 133 134 135

type MessageOutput struct {
	Message string
}

136
func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
137
	return []byte(res.Output().(*MessageOutput).Message), nil
138
}