main.go 6.51 KB
Newer Older
1 2 3 4 5 6 7 8
package main

import (
	"fmt"
	"io"
	"os"
	"runtime/pprof"

9
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
10 11 12
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"

13 14 15 16 17 18 19 20 21 22 23 24 25
	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
	"github.com/jbenet/go-ipfs/config"
	"github.com/jbenet/go-ipfs/core"
	commands "github.com/jbenet/go-ipfs/core/commands2"
	daemon "github.com/jbenet/go-ipfs/daemon2"
	u "github.com/jbenet/go-ipfs/util"
)

// log is the command logger
var log = u.Logger("cmd/ipfs")

26 27 28 29
const (
	heapProfile = "ipfs.mprof"
	errorFormat = "ERROR: %v\n\n"
)
30

31 32
var ofi io.WriteCloser

33 34
func main() {
	args := os.Args[1:]
35 36
	req, root := createRequest(args)
	handleOptions(req, root)
37

38
	// if debugging, setup profiling.
39
	if u.Debug {
40 41
		var err error
		ofi, err = os.Create("cpu.prof")
42
		if err != nil {
43 44
			fmt.Println(err)
			return
45
		}
46 47

		pprof.StartCPUProfile(ofi)
48
	}
49 50 51 52 53

	res := callCommand(req, root)
	outputResponse(res, root)

	exit(0)
54
}
55

56
func createRequest(args []string) (cmds.Request, *cmds.Command) {
57 58 59 60 61 62 63 64
	req, root, cmd, path, err := cmdsCli.Parse(args, Root, commands.Root)

	var options cmds.Request
	if req != nil && root != nil {
		var err2 error
		options, err2 = getOptions(req, root)
		if err2 != nil {
			fmt.Println(err2)
65
			exit(1)
66
		}
67 68
	}

69 70
	// handle parse error (which means the commandline input was wrong,
	// e.g. incorrect number of args, or nonexistent subcommand)
71
	if err != nil {
72
		// if the -help flag wasn't specified, show the error message
73 74 75 76 77 78 79
		// or if a path was returned (user specified a valid subcommand), show the error message
		// (this means there was an option or argument error)
		if options != nil || path != nil && len(path) > 0 {
			help := false
			if options != nil {
				opt, _ := options.Option("help")
				help, _ = opt.(bool)
80 81
			}

82 83 84
			if !help {
				fmt.Printf(errorFormat, err)
			}
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		}

		// when generating help for the root command, we don't want the autogenerated subcommand text
		// (since we have better hand-made subcommand list in the root Help field)
		if cmd == nil {
			root = &*commands.Root
			root.Subcommands = nil
		}

		// generate the help text for the command the user was trying to call (or root)
		helpText, err := cmdsCli.HelpText("ipfs", root, path)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println(helpText)
		}
101
		exit(1)
102 103
	}

104 105 106
	configPath, err := getConfigRoot(options)
	if err != nil {
		fmt.Println(err)
107
		exit(1)
108 109 110 111 112
	}

	conf, err := getConfig(configPath)
	if err != nil {
		fmt.Println(err)
113
		exit(1)
114 115 116 117 118 119 120
	}

	ctx := req.Context()
	ctx.ConfigRoot = configPath
	ctx.Config = conf

	if _, found := options.Option("encoding"); !found {
121
		if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
122 123 124 125 126 127 128 129 130 131 132 133 134
			req.SetOption("encoding", cmds.Text)
		} else {
			req.SetOption("encoding", cmds.JSON)
		}
	}

	return req, root
}

func handleOptions(req cmds.Request, root *cmds.Command) {
	options, err := getOptions(req, root)
	if err != nil {
		fmt.Println(err)
135
		exit(1)
136 137
	}

138 139
	if help, found := options.Option("help"); found {
		if helpBool, ok := help.(bool); helpBool && ok {
140 141 142 143 144 145
			helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
			if err != nil {
				fmt.Println(err.Error())
			} else {
				fmt.Println(helpText)
			}
146
			exit(0)
147 148
		} else if !ok {
			fmt.Println("error: expected 'help' option to be a bool")
149
			exit(1)
150
		}
151 152
	}

153 154 155 156
	if debug, found := options.Option("debug"); found {
		if debugBool, ok := debug.(bool); debugBool && ok {
			u.Debug = true

157
			u.SetAllLoggers(logging.DEBUG)
158 159
		} else if !ok {
			fmt.Println("error: expected 'debug' option to be a bool")
160
			exit(1)
161 162
		}
	}
163
}
164

165
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
166
	var res cmds.Response
167

168 169
	if root == Root {
		res = root.Call(req)
170 171

	} else {
172 173 174
		options, err := getOptions(req, root)
		if err != nil {
			fmt.Println(err)
175
			exit(1)
176 177
		}

178
		var found bool
179
		var local interface{}
180
		localBool := false
181
		if local, found = options.Option("local"); found {
182 183 184 185
			var ok bool
			localBool, ok = local.(bool)
			if !ok {
				fmt.Println("error: expected 'local' option to be a bool")
186
				exit(1)
187 188
			}
		}
189

190
		if (!found || !localBool) && daemon.Locked(req.Context().ConfigRoot) {
191 192 193
			addr, err := ma.NewMultiaddr(req.Context().Config.Addresses.API)
			if err != nil {
				fmt.Println(err)
194
				exit(1)
195 196 197 198 199
			}

			_, host, err := manet.DialArgs(addr)
			if err != nil {
				fmt.Println(err)
200
				exit(1)
201 202 203 204 205
			}

			client := cmdsHttp.NewClient(host)

			res, err = client.Send(req)
206 207
			if err != nil {
				fmt.Println(err)
208
				exit(1)
209 210 211
			}

		} else {
212
			node, err := core.NewIpfsNode(req.Context().Config, false)
213 214
			if err != nil {
				fmt.Println(err)
215
				exit(1)
216
			}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
217
			defer node.Close()
218
			req.Context().Node = node
219

220
			res = root.Call(req)
221 222 223
		}
	}

224 225 226
	return res
}

227
func outputResponse(res cmds.Response, root *cmds.Command) {
228
	if res.Error() != nil {
229
		fmt.Printf(errorFormat, res.Error().Error())
230

231 232 233 234 235 236 237
		if res.Error().Code == cmds.ErrClient {
			helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path())
			if err != nil {
				fmt.Println(err.Error())
			} else {
				fmt.Println(helpText)
			}
238 239
		}

240
		exit(1)
241 242
	}

243
	out, err := res.Reader()
244 245
	if err != nil {
		fmt.Println(err.Error())
246
		return
247
	}
248 249

	io.Copy(os.Stdout, out)
250 251 252
}

func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
253
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	options, err := root.GetOptions(tempReq.Path())
	if err != nil {
		return nil, err
	}

	err = tempReq.ConvertOptions(options)
	if err != nil {
		return nil, err
	}

	return tempReq, nil
}

func getConfigRoot(req cmds.Request) (string, error) {
	if opt, found := req.Option("config"); found {
270 271 272 273 274
		if optStr, ok := opt.(string); ok {
			return optStr, nil
		} else {
			return "", fmt.Errorf("Expected 'config' option to be a string")
		}
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	}

	configPath, err := config.PathRoot()
	if err != nil {
		return "", err
	}
	return configPath, nil
}

func getConfig(path string) (*config.Config, error) {
	configFile, err := config.Filename(path)
	if err != nil {
		return nil, err
	}

	return config.Load(configFile)
}
292 293 294 295 296 297 298 299 300

func writeHeapProfileToFile() error {
	mprof, err := os.Create(heapProfile)
	if err != nil {
		log.Fatal(err)
	}
	defer mprof.Close()
	return pprof.WriteHeapProfile(mprof)
}
301 302 303 304 305 306 307 308 309 310 311 312 313 314

func exit(code int) {
	if u.Debug {
		pprof.StopCPUProfile()
		ofi.Close()

		err := writeHeapProfileToFile()
		if err != nil {
			log.Critical(err)
		}
	}

	os.Exit(code)
}