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

import (
4 5 6
	"io"
	"strings"

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

Steven Allen's avatar
Steven Allen committed
14 15
	"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
	logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
16
	"gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
17 18
)

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

21 22 23 24
const (
	ApiOption = "api"
)

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

38
DATA STRUCTURE COMMANDS
39 40 41
  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
42
  dag           Interact with IPLD documents (experimental)
43

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

57
NETWORK COMMANDS
58
  id            Show info about IPFS peers
59 60 61 62 63
  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
64

65
TOOL COMMANDS
66 67 68 69
  config        Manage configuration
  version       Show ipfs version information
  update        Download and apply go-ipfs updates
  commands      List all available commands
70 71

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

73 74 75
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:
76

77
  export IPFS_PATH=/path/to/ipfsrepo
78 79 80

EXIT STATUS

Richard Littauer's avatar
Richard Littauer committed
81
The CLI will exit with one of the following values:
82 83 84

0     Successful execution.
1     Failed executions.
85
`,
86
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
87 88
	Options: []cmdkit.Option{
		cmdkit.StringOption("config", "c", "Path to the configuration file to use."),
89 90 91 92
		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
93
		cmdkit.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"),
94 95 96 97 98

		// global options, added to every command
		cmds.OptionEncodingType,
		cmds.OptionStreamChannels,
		cmds.OptionTimeout,
99
	},
100 101
}

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

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

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

var CommandsDaemonROCmd = CommandsCmd(RootRO)

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

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

186
func init() {
187
	Root.ProcessHelp()
rht's avatar
rht committed
188
	*RootRO = *Root
rht's avatar
rht committed
189 190 191

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

keks's avatar
keks committed
194 195 196 197 198
	// 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)

199
	Root.Subcommands = rootSubcommands
Jan Winkelmann's avatar
Jan Winkelmann committed
200

rht's avatar
rht committed
201
	RootRO.Subcommands = rootROSubcommands
202
}
203 204 205 206 207

type MessageOutput struct {
	Message string
}

Jan Winkelmann's avatar
Jan Winkelmann committed
208 209 210 211 212 213 214 215 216 217 218 219
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
220
}