main.go 5.7 KB
Newer Older
1 2 3 4 5 6
package main

import (
	"fmt"
	"io"
	"os"
Matt Bell's avatar
Matt Bell committed
7
	"os/signal"
8 9
	"runtime/pprof"

10
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
11 12 13
	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"

14 15 16 17 18 19 20 21 22 23 24 25 26
	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")

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

32 33
var ofi io.WriteCloser

34
func main() {
Matt Bell's avatar
Matt Bell committed
35 36
	handleInterrupt()

37
	args := os.Args[1:]
38 39 40 41 42
	req, root, err := createRequest(args)
	if err != nil {
		fmt.Println(err)
		exit(1)
	}
43
	handleOptions(req, root)
44

45
	// if debugging, setup profiling.
46
	if u.Debug {
47 48
		var err error
		ofi, err = os.Create("cpu.prof")
49
		if err != nil {
50 51
			fmt.Println(err)
			return
52
		}
53 54

		pprof.StartCPUProfile(ofi)
55
	}
56 57 58 59 60

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

	exit(0)
61
}
62

63
func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
64 65 66 67
	req, root, cmd, path, err := cmdsCli.Parse(args, Root, commands.Root)

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

		// 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)
87 88 89
		helpText, htErr := cmdsCli.HelpText("ipfs", root, path)
		if htErr != nil {
			fmt.Println(htErr)
90 91 92
		} else {
			fmt.Println(helpText)
		}
93
		return nil, nil, err
94 95
	}

96
	configPath, err := getConfigRoot(req)
97
	if err != nil {
98
		return nil, nil, err
99 100 101 102
	}

	conf, err := getConfig(configPath)
	if err != nil {
103
		return nil, nil, err
104 105 106 107 108
	}
	ctx := req.Context()
	ctx.ConfigRoot = configPath
	ctx.Config = conf

109
	if !req.Option("encoding").Found() {
110
		if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
111 112 113 114 115 116
			req.SetOption("encoding", cmds.Text)
		} else {
			req.SetOption("encoding", cmds.JSON)
		}
	}

117
	return req, root, nil
118 119 120
}

func handleOptions(req cmds.Request, root *cmds.Command) {
121 122
	help, err := req.Option("help").Bool()
	if help && err == nil {
123 124 125 126 127
		helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
		if err != nil {
			fmt.Println(err.Error())
		} else {
			fmt.Println(helpText)
128
		}
129 130 131 132
		exit(0)
	} else if err != nil {
		fmt.Println(err)
		exit(1)
133 134
	}

135 136 137 138 139 140
	if debug, err := req.Option("debug").Bool(); debug && err == nil {
		u.Debug = true
		u.SetAllLoggers(logging.DEBUG)
	} else if err != nil {
		fmt.Println(err)
		exit(1)
141
	}
142
}
143

144
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
145
	var res cmds.Response
146

147 148
	if root == Root {
		res = root.Call(req)
149 150

	} else {
151 152 153 154
		local, err := req.Option("local").Bool()
		if err != nil {
			fmt.Println(err)
			exit(1)
155
		}
156

157
		if (!req.Option("local").Found() || !local) && daemon.Locked(req.Context().ConfigRoot) {
158 159 160
			addr, err := ma.NewMultiaddr(req.Context().Config.Addresses.API)
			if err != nil {
				fmt.Println(err)
161
				exit(1)
162 163 164 165 166
			}

			_, host, err := manet.DialArgs(addr)
			if err != nil {
				fmt.Println(err)
167
				exit(1)
168 169 170 171 172
			}

			client := cmdsHttp.NewClient(host)

			res, err = client.Send(req)
173 174
			if err != nil {
				fmt.Println(err)
175
				exit(1)
176 177 178
			}

		} else {
179
			node, err := core.NewIpfsNode(req.Context().Config, false)
180 181
			if err != nil {
				fmt.Println(err)
182
				exit(1)
183
			}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
184
			defer node.Close()
185
			req.Context().Node = node
186

187
			res = root.Call(req)
188 189 190
		}
	}

191 192 193
	return res
}

194
func outputResponse(res cmds.Response, root *cmds.Command) {
195
	if res.Error() != nil {
196
		fmt.Printf(errorFormat, res.Error().Error())
197

198 199 200 201 202 203 204
		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)
			}
205 206
		}

207
		exit(1)
208 209
	}

210
	out, err := res.Reader()
211 212
	if err != nil {
		fmt.Println(err.Error())
213
		return
214
	}
215 216

	io.Copy(os.Stdout, out)
217 218 219
}

func getConfigRoot(req cmds.Request) (string, error) {
220 221 222 223 224 225
	configOpt, err := req.Option("config").String()
	if err != nil {
		return "", err
	}
	if configOpt != "" {
		return configOpt, nil
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	}

	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)
}
243 244 245 246 247 248 249 250 251

func writeHeapProfileToFile() error {
	mprof, err := os.Create(heapProfile)
	if err != nil {
		log.Fatal(err)
	}
	defer mprof.Close()
	return pprof.WriteHeapProfile(mprof)
}
252

Matt Bell's avatar
Matt Bell committed
253 254 255 256 257 258 259 260 261 262 263 264 265
// listen for and handle SIGTERM
func handleInterrupt() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	go func() {
		for _ = range c {
			log.Info("Received interrupt signal, terminating...")
			exit(0)
		}
	}()
}

266 267 268 269 270 271 272 273 274 275 276 277 278
func exit(code int) {
	if u.Debug {
		pprof.StopCPUProfile()
		ofi.Close()

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

	os.Exit(code)
}