root.go 6.05 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"
8
	dag "github.com/ipfs/go-ipfs/core/commands/dag"
Jan Winkelmann's avatar
Jan Winkelmann committed
9
	e "github.com/ipfs/go-ipfs/core/commands/e"
Jeromy's avatar
Jeromy committed
10
	files "github.com/ipfs/go-ipfs/core/commands/files"
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 14

	"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
Jeromy's avatar
Jeromy committed
15
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
keks's avatar
keks committed
16
	"gx/ipfs/QmUgr8HrEkQqXfBPtj1A2UEg1V7cvhUhDsmL44wFPCJk5k/go-ipfs-cmds"
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 89 90 91 92 93
	Options: []cmdkit.Option{
		cmdkit.StringOption("config", "c", "Path to the configuration file to use."),
		cmdkit.BoolOption("debug", "D", "Operate in debug mode.").Default(false),
		cmdkit.BoolOption("help", "Show the full command help text.").Default(false),
		cmdkit.BoolOption("h", "Show a short version of the command help text.").Default(false),
		cmdkit.BoolOption("local", "L", "Run the command locally, instead of using the daemon.").Default(false),
		cmdkit.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"),
94
	},
95 96
}

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

100
var rootSubcommands = map[string]*cmds.Command{
101
	"add":       AddCmd,
Jan Winkelmann's avatar
Jan Winkelmann committed
102
	"bitswap":   BitswapCmd,
103 104
	"block":     BlockCmd,
	"cat":       CatCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
	"commands":  CommandsDaemonCmd,
Jan Winkelmann's avatar
Jan Winkelmann committed
106 107 108 109 110 111 112 113 114
	"filestore": FileStoreCmd,
	"get":       GetCmd,
	"pubsub":    PubsubCmd,
	"repo":      RepoCmd,
	"stats":     StatsCmd,
}

var rootOldSubcommands = map[string]*oldcmds.Command{
	"bootstrap": BootstrapCmd,
115
	"config":    ConfigCmd,
116
	"dag":       dag.DagCmd,
117
	"dht":       DhtCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118
	"diag":      DiagCmd,
119
	"dns":       DNSCmd,
Jeromy's avatar
Jeromy committed
120
	"files":     files.FilesCmd,
121
	"id":        IDCmd,
Jeromy's avatar
Jeromy committed
122
	"key":       KeyCmd,
123 124 125 126
	"log":       LogCmd,
	"ls":        LsCmd,
	"mount":     MountCmd,
	"name":      NameCmd,
127
	"object":    ocmd.ObjectCmd,
128
	"pin":       PinCmd,
129
	"ping":      PingCmd,
Łukasz Magiera's avatar
Łukasz Magiera committed
130
	"p2p":       P2PCmd,
131
	"refs":      RefsCmd,
132
	"resolve":   ResolveCmd,
133
	"swarm":     SwarmCmd,
Jeromy's avatar
Jeromy committed
134
	"tar":       TarCmd,
135
	"file":      unixfs.UnixFSCmd,
Jeromy's avatar
Jeromy committed
136
	"update":    ExternalBinary(),
137
	"version":   VersionCmd,
Jeromy's avatar
Jeromy committed
138
	"shutdown":  daemonShutdownCmd,
139
}
140 141 142 143 144 145

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

var CommandsDaemonROCmd = CommandsCmd(RootRO)

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

rht's avatar
rht committed
148
var rootROSubcommands = map[string]*cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
149 150
	"commands": CommandsDaemonROCmd,
	"cat":      CatCmd,
rht's avatar
rht committed
151 152 153 154 155 156
	"block": &cmds.Command{
		Subcommands: map[string]*cmds.Command{
			"stat": blockStatCmd,
			"get":  blockGetCmd,
		},
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
157 158 159 160 161 162 163 164
	"get": GetCmd,
}

var rootROOldSubcommands = map[string]*oldcmds.Command{
	"dns": DNSCmd,
	"ls":  LsCmd,
	"name": &oldcmds.Command{
		Subcommands: map[string]*oldcmds.Command{
rht's avatar
rht committed
165 166 167
			"resolve": IpnsCmd,
		},
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
168 169
	"object": &oldcmds.Command{
		Subcommands: map[string]*oldcmds.Command{
170 171 172 173 174
			"data":  ocmd.ObjectDataCmd,
			"links": ocmd.ObjectLinksCmd,
			"get":   ocmd.ObjectGetCmd,
			"stat":  ocmd.ObjectStatCmd,
			"patch": ocmd.ObjectPatchCmd,
rht's avatar
rht committed
175 176
		},
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
177 178
	"dag": &oldcmds.Command{
		Subcommands: map[string]*oldcmds.Command{
Łukasz Magiera's avatar
Łukasz Magiera committed
179 180
			"get":     dag.DagGetCmd,
			"resolve": dag.DagResolveCmd,
jbenet's avatar
jbenet committed
181 182
		},
	},
183 184
	"refs":    RefsROCmd,
	"resolve": ResolveCmd,
Lars Gierth's avatar
Lars Gierth committed
185
	"version": VersionCmd,
rht's avatar
rht committed
186 187
}

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

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

Jan Winkelmann's avatar
Jan Winkelmann committed
196
	Root.OldSubcommands = rootOldSubcommands
197
	Root.Subcommands = rootSubcommands
Jan Winkelmann's avatar
Jan Winkelmann committed
198 199

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

type MessageOutput struct {
	Message string
}

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