root.go 6.65 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
	logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
17
	"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
keks's avatar
keks committed
18
	"gx/ipfs/QmdsFzGmSLMQQaaPhcgGkpDjPocqBWLFA829u6iMv5huPw/go-ipfs-cmds"
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 26 27 28
const (
	ApiOption = "api"
)

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

42
DATA STRUCTURE COMMANDS
43 44 45
  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
46
  dag           Interact with IPLD documents (experimental)
47

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

61
NETWORK COMMANDS
62
  id            Show info about IPFS peers
63 64 65 66 67
  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
68

69
TOOL COMMANDS
70 71 72 73
  config        Manage configuration
  version       Show ipfs version information
  update        Download and apply go-ipfs updates
  commands      List all available commands
74 75

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

77 78 79
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:
80

81
  export IPFS_PATH=/path/to/ipfsrepo
82 83 84

EXIT STATUS

Richard Littauer's avatar
Richard Littauer committed
85
The CLI will exit with one of the following values:
86 87 88

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

		// global options, added to every command
		cmds.OptionEncodingType,
		cmds.OptionStreamChannels,
		cmds.OptionTimeout,
103
	},
104 105
}

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

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

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

var CommandsDaemonROCmd = CommandsCmd(RootRO)

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

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

190
func init() {
191
	Root.ProcessHelp()
rht's avatar
rht committed
192
	*RootRO = *Root
rht's avatar
rht committed
193 194 195

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

keks's avatar
keks committed
198 199 200 201 202
	// 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)

203
	Root.Subcommands = rootSubcommands
Jan Winkelmann's avatar
Jan Winkelmann committed
204

rht's avatar
rht committed
205
	RootRO.Subcommands = rootROSubcommands
206
}
207 208 209 210 211

type MessageOutput struct {
	Message string
}

Jan Winkelmann's avatar
Jan Winkelmann committed
212 213 214 215 216 217 218 219 220 221 222 223
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
224
}