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

import (
4 5 6
	"io"
	"strings"

7
	cmds "github.com/ipfs/go-ipfs/commands"
Jeromy's avatar
Jeromy committed
8
	files "github.com/ipfs/go-ipfs/core/commands/files"
9
	ocmd "github.com/ipfs/go-ipfs/core/commands/object"
10
	unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs"
Jeromy's avatar
Jeromy committed
11
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
12 13
)

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

16 17 18 19
const (
	ApiOption = "api"
)

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

33
DATA STRUCTURE COMMANDS
34 35 36
  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
37

38
ADVANCED COMMANDS
39 40 41 42 43 44 45
  daemon        Start a long-running daemon process
  mount         Mount an ipfs read-only mountpoint
  resolve       Resolve any type of name
  name          Publish or resolve IPNS names
  dns           Resolve DNS links
  pin           Pin objects to local storage
  repo          Manipulate the IPFS repository
46

47
NETWORK COMMANDS
48 49 50 51 52 53
  id            Show info about ipfs peers
  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
54

55
TOOL COMMANDS
56 57 58 59
  config        Manage configuration
  version       Show ipfs version information
  update        Download and apply go-ipfs updates
  commands      List all available commands
60 61

Use 'ipfs <command> --help' to learn more about each command.
62 63 64 65

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:

66
  export IPFS_PATH=/path/to/ipfsrepo
67
`,
68
	},
69
	Options: []cmds.Option{
70
		cmds.StringOption("config", "c", "Path to the configuration file to use."),
Richard Littauer's avatar
Richard Littauer committed
71 72 73 74
		cmds.BoolOption("debug", "D", "Operate in debug mode.").Default(false),
		cmds.BoolOption("help", "Show the full command help text.").Default(false),
		cmds.BoolOption("h", "Show a short version of the command help text.").Default(false),
		cmds.BoolOption("local", "L", "Run the command locally, instead of using the daemon.").Default(false),
75
		cmds.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"),
76
	},
77 78
}

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

82
var rootSubcommands = map[string]*cmds.Command{
83 84 85 86
	"add":       AddCmd,
	"block":     BlockCmd,
	"bootstrap": BootstrapCmd,
	"cat":       CatCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
	"commands":  CommandsDaemonCmd,
88
	"config":    ConfigCmd,
89
	"dht":       DhtCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
	"diag":      DiagCmd,
91
	"dns":       DNSCmd,
Jeromy's avatar
Jeromy committed
92
	"files":     files.FilesCmd,
Matt Bell's avatar
Matt Bell committed
93
	"get":       GetCmd,
94 95 96 97 98
	"id":        IDCmd,
	"log":       LogCmd,
	"ls":        LsCmd,
	"mount":     MountCmd,
	"name":      NameCmd,
99
	"object":    ocmd.ObjectCmd,
100
	"pin":       PinCmd,
101
	"ping":      PingCmd,
102
	"refs":      RefsCmd,
Jeromy's avatar
Jeromy committed
103
	"repo":      RepoCmd,
104
	"resolve":   ResolveCmd,
Jeromy's avatar
Jeromy committed
105
	"stats":     StatsCmd,
106
	"swarm":     SwarmCmd,
Jeromy's avatar
Jeromy committed
107
	"tar":       TarCmd,
Jeromy's avatar
Jeromy committed
108
	"pubsub":    PubsubCmd,
rht's avatar
rht committed
109
	"tour":      tourCmd,
110
	"file":      unixfs.UnixFSCmd,
Jeromy's avatar
Jeromy committed
111
	"update":    ExternalBinary(),
112
	"version":   VersionCmd,
113
	"bitswap":   BitswapCmd,
114
}
115 116 117 118 119 120

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

var CommandsDaemonROCmd = CommandsCmd(RootRO)

rht's avatar
rht committed
121 122
var RefsROCmd = &cmds.Command{}

rht's avatar
rht committed
123 124 125 126 127 128 129 130
var rootROSubcommands = map[string]*cmds.Command{
	"block": &cmds.Command{
		Subcommands: map[string]*cmds.Command{
			"stat": blockStatCmd,
			"get":  blockGetCmd,
		},
	},
	"cat":      CatCmd,
131
	"commands": CommandsDaemonROCmd,
132
	"dns":      DNSCmd,
133
	"get":      GetCmd,
rht's avatar
rht committed
134 135 136 137 138 139 140 141
	"ls":       LsCmd,
	"name": &cmds.Command{
		Subcommands: map[string]*cmds.Command{
			"resolve": IpnsCmd,
		},
	},
	"object": &cmds.Command{
		Subcommands: map[string]*cmds.Command{
142 143 144 145 146
			"data":  ocmd.ObjectDataCmd,
			"links": ocmd.ObjectLinksCmd,
			"get":   ocmd.ObjectGetCmd,
			"stat":  ocmd.ObjectStatCmd,
			"patch": ocmd.ObjectPatchCmd,
rht's avatar
rht committed
147 148
		},
	},
149 150
	"refs":    RefsROCmd,
	"resolve": ResolveCmd,
Lars Gierth's avatar
Lars Gierth committed
151
	"version": VersionCmd,
rht's avatar
rht committed
152 153
}

154
func init() {
155
	Root.ProcessHelp()
rht's avatar
rht committed
156
	*RootRO = *Root
rht's avatar
rht committed
157 158 159 160 161

	// sanitize readonly refs command
	*RefsROCmd = *RefsCmd
	RefsROCmd.Subcommands = map[string]*cmds.Command{}

162
	Root.Subcommands = rootSubcommands
rht's avatar
rht committed
163
	RootRO.Subcommands = rootROSubcommands
164
}
165 166 167 168 169

type MessageOutput struct {
	Message string
}

170 171
func MessageTextMarshaler(res cmds.Response) (io.Reader, error) {
	return strings.NewReader(res.Output().(*MessageOutput).Message), nil
172
}