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

import (
4
	"errors"
5 6 7
	"io"
	"strings"

Jan Winkelmann's avatar
Jan Winkelmann committed
8
	oldcmds "github.com/ipfs/go-ipfs/commands"
Jakub Sztandera's avatar
Jakub Sztandera committed
9
	lgc "github.com/ipfs/go-ipfs/commands/legacy"
10
	dag "github.com/ipfs/go-ipfs/core/commands/dag"
Jan Winkelmann's avatar
Jan Winkelmann committed
11
	e "github.com/ipfs/go-ipfs/core/commands/e"
12
	name "github.com/ipfs/go-ipfs/core/commands/name"
13
	ocmd "github.com/ipfs/go-ipfs/core/commands/object"
14
	unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs"
Jan Winkelmann's avatar
Jan Winkelmann committed
15

Steven Allen's avatar
Steven Allen committed
16 17
	cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
	logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
18
	"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
19 20
)

Jeromy's avatar
Jeromy committed
21
var log = logging.Logger("core/commands")
Matt Bell's avatar
Matt Bell committed
22

23 24
var ErrNotOnline = errors.New("this command must be run in online mode. Try running 'ipfs daemon' first")

25
const (
26 27 28 29
	ConfigOption = "config"
	DebugOption  = "debug"
	LocalOption  = "local"
	ApiOption    = "api"
30 31
)

32
var Root = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
33
	Helptext: cmdkit.HelpText{
Jakub Sztandera's avatar
Jakub Sztandera committed
34 35
		Tagline:  "Global p2p merkle-dag filesystem.",
		Synopsis: "ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...",
36
		Subcommands: `
37
BASIC COMMANDS
38
  init          Initialize ipfs local configuration
39
  add <path>    Add a file to IPFS
40 41
  cat <ref>     Show IPFS object data
  get <ref>     Download IPFS objects
42 43
  ls <ref>      List links from an object
  refs <ref>    List hashes of links from an object
44

45
DATA STRUCTURE COMMANDS
46 47 48
  block         Interact with raw blocks in the datastore
  object        Interact with raw dag nodes
  files         Interact with objects as if they were a unix filesystem
slothbag's avatar
slothbag committed
49
  dag           Interact with IPLD documents (experimental)
50

51
ADVANCED COMMANDS
52
  daemon        Start a long-running daemon process
53
  mount         Mount an IPFS read-only mountpoint
54
  resolve       Resolve any type of name
55 56
  name          Publish and resolve IPNS names
  key           Create and list IPNS name keypairs
57 58 59
  dns           Resolve DNS links
  pin           Pin objects to local storage
  repo          Manipulate the IPFS repository
slothbag's avatar
slothbag committed
60
  stats         Various operational stats
Łukasz Magiera's avatar
Łukasz Magiera committed
61
  p2p           Libp2p stream mounting
62
  filestore     Manage the filestore (experimental)
63

64
NETWORK COMMANDS
65
  id            Show info about IPFS peers
66 67 68 69 70
  bootstrap     Add or remove bootstrap peers
  swarm         Manage connections to the p2p network
  dht           Query the DHT for values or peers
  ping          Measure the latency of a connection
  diag          Print diagnostics
71

72
TOOL COMMANDS
73 74 75 76
  config        Manage configuration
  version       Show ipfs version information
  update        Download and apply go-ipfs updates
  commands      List all available commands
77
  cid           Convert and discover properties of CIDs
78 79

Use 'ipfs <command> --help' to learn more about each command.
80

81 82 83
ipfs uses a repository in the local file system. By default, the repo is
located at ~/.ipfs. To change the repo location, set the $IPFS_PATH
environment variable:
84

85
  export IPFS_PATH=/path/to/ipfsrepo
86 87 88

EXIT STATUS

Richard Littauer's avatar
Richard Littauer committed
89
The CLI will exit with one of the following values:
90 91 92

0     Successful execution.
1     Failed executions.
93
`,
94
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
95
	Options: []cmdkit.Option{
96 97 98 99 100
		cmdkit.StringOption(ConfigOption, "c", "Path to the configuration file to use."),
		cmdkit.BoolOption(DebugOption, "D", "Operate in debug mode."),
		cmdkit.BoolOption(cmds.OptLongHelp, "Show the full command help text."),
		cmdkit.BoolOption(cmds.OptShortHelp, "Show a short version of the command help text."),
		cmdkit.BoolOption(LocalOption, "L", "Run the command locally, instead of using the daemon."),
Jan Winkelmann's avatar
Jan Winkelmann committed
101
		cmdkit.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"),
102 103 104 105 106

		// global options, added to every command
		cmds.OptionEncodingType,
		cmds.OptionStreamChannels,
		cmds.OptionTimeout,
107
	},
108 109
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112
// commandsDaemonCmd is the "ipfs commands" command for daemon
var CommandsDaemonCmd = CommandsCmd(Root)

113
var rootSubcommands = map[string]*cmds.Command{
114
	"add":       AddCmd,
Jan Winkelmann's avatar
Jan Winkelmann committed
115
	"bitswap":   BitswapCmd,
116 117
	"block":     BlockCmd,
	"cat":       CatCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118
	"commands":  CommandsDaemonCmd,
119
	"files":     FilesCmd,
Jan Winkelmann's avatar
Jan Winkelmann committed
120 121 122 123 124
	"filestore": FileStoreCmd,
	"get":       GetCmd,
	"pubsub":    PubsubCmd,
	"repo":      RepoCmd,
	"stats":     StatsCmd,
125
	"bootstrap": lgc.NewCommand(BootstrapCmd),
126
	"config":    ConfigCmd,
Overbool's avatar
Overbool committed
127
	"dag":       dag.DagCmd,
Overbool's avatar
Overbool committed
128
	"dht":       DhtCmd,
129
	"diag":      DiagCmd,
130
	"dns":       DNSCmd,
131
	"id":        IDCmd,
132
	"key":       KeyCmd,
133 134 135
	"log":       lgc.NewCommand(LogCmd),
	"ls":        lgc.NewCommand(LsCmd),
	"mount":     lgc.NewCommand(MountCmd),
136
	"name":      name.NameCmd,
137
	"object":    ocmd.ObjectCmd,
138
	"pin":       lgc.NewCommand(PinCmd),
Overbool's avatar
Overbool committed
139
	"ping":      PingCmd,
Overbool's avatar
Overbool committed
140
	"p2p":       P2PCmd,
141
	"refs":      lgc.NewCommand(RefsCmd),
142
	"resolve":   ResolveCmd,
143
	"swarm":     SwarmCmd,
144
	"tar":       TarCmd,
145 146
	"file":      lgc.NewCommand(unixfs.UnixFSCmd),
	"update":    lgc.NewCommand(ExternalBinary()),
147
	"urlstore":  urlStoreCmd,
148
	"version":   VersionCmd,
149
	"shutdown":  daemonShutdownCmd,
150
	"cid":       CidCmd,
151
}
152 153 154 155 156 157

// RootRO is the readonly version of Root
var RootRO = &cmds.Command{}

var CommandsDaemonROCmd = CommandsCmd(RootRO)

Jan Winkelmann's avatar
Jan Winkelmann committed
158
var RefsROCmd = &oldcmds.Command{}
rht's avatar
rht committed
159

rht's avatar
rht committed
160
var rootROSubcommands = map[string]*cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
161 162
	"commands": CommandsDaemonROCmd,
	"cat":      CatCmd,
rht's avatar
rht committed
163 164 165 166 167 168
	"block": &cmds.Command{
		Subcommands: map[string]*cmds.Command{
			"stat": blockStatCmd,
			"get":  blockGetCmd,
		},
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
169
	"get": GetCmd,
170
	"dns": DNSCmd,
171
	"ls":  lgc.NewCommand(LsCmd),
Overbool's avatar
Overbool committed
172
	"name": {
173 174
		Subcommands: map[string]*cmds.Command{
			"resolve": name.IpnsCmd,
rht's avatar
rht committed
175
		},
176
	},
177 178
	"object": {
		Subcommands: map[string]*cmds.Command{
179 180 181 182
			"data":  ocmd.ObjectDataCmd,
			"links": ocmd.ObjectLinksCmd,
			"get":   ocmd.ObjectGetCmd,
			"stat":  ocmd.ObjectStatCmd,
rht's avatar
rht committed
183
		},
Overbool's avatar
Overbool committed
184
	},
Overbool's avatar
Overbool committed
185 186
	"dag": {
		Subcommands: map[string]*cmds.Command{
Łukasz Magiera's avatar
Łukasz Magiera committed
187 188
			"get":     dag.DagGetCmd,
			"resolve": dag.DagResolveCmd,
jbenet's avatar
jbenet committed
189
		},
Overbool's avatar
Overbool committed
190
	},
191
	"resolve": ResolveCmd,
192
	"version": VersionCmd,
rht's avatar
rht committed
193 194
}

195
func init() {
196
	Root.ProcessHelp()
rht's avatar
rht committed
197
	*RootRO = *Root
rht's avatar
rht committed
198 199 200

	// sanitize readonly refs command
	*RefsROCmd = *RefsCmd
Jan Winkelmann's avatar
Jan Winkelmann committed
201
	RefsROCmd.Subcommands = map[string]*oldcmds.Command{}
rht's avatar
rht committed
202

keks's avatar
keks committed
203 204 205 206 207
	// this was in the big map definition above before,
	// but if we leave it there lgc.NewCommand will be executed
	// before the value is updated (:/sanitize readonly refs command/)
	rootROSubcommands["refs"] = lgc.NewCommand(RefsROCmd)

208
	Root.Subcommands = rootSubcommands
Jan Winkelmann's avatar
Jan Winkelmann committed
209

rht's avatar
rht committed
210
	RootRO.Subcommands = rootROSubcommands
211
}
212 213 214 215 216

type MessageOutput struct {
	Message string
}

Jan Winkelmann's avatar
Jan Winkelmann committed
217 218 219 220 221 222 223 224 225 226 227 228
func MessageTextMarshaler(res oldcmds.Response) (io.Reader, error) {
	v, err := unwrapOutput(res.Output())
	if err != nil {
		return nil, err
	}

	out, ok := v.(*MessageOutput)
	if !ok {
		return nil, e.TypeErr(out, v)
	}

	return strings.NewReader(out.Message), nil
229
}