main.go 4.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package main

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

	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")

func main() {
	args := os.Args[1:]
24 25 26 27 28
	req, root := createRequest(args)
	handleOptions(req, root)
	res := callCommand(req, root)
	outputResponse(res)
}
29

30
func createRequest(args []string) (cmds.Request, *cmds.Command) {
31
	req, root, err := cmdsCli.Parse(args, Root, commands.Root)
32 33
	if err != nil {
		fmt.Println(err)
34
		fmt.Println(Root.Help)
35 36 37
		os.Exit(1)
	}

38
	options, err := getOptions(req, root)
39 40 41 42 43
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	configPath, err := getConfigRoot(options)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	conf, err := getConfig(configPath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

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

	if _, found := options.Option("encoding"); !found {
61
		if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			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)
		os.Exit(1)
	}

78 79 80 81 82 83 84 85
	if help, found := options.Option("help"); found {
		if helpBool, ok := help.(bool); helpBool && ok {
			fmt.Println(req.Command().Help)
			os.Exit(0)
		} else if !ok {
			fmt.Println("error: expected 'help' option to be a bool")
			os.Exit(1)
		}
86 87
	}

88 89 90 91 92 93 94 95 96 97 98 99 100 101
	if debug, found := options.Option("debug"); found {
		if debugBool, ok := debug.(bool); debugBool && ok {
			u.Debug = true

			// if debugging, setup profiling.
			if u.Debug {
				ofi, err := os.Create("cpu.prof")
				if err != nil {
					fmt.Println(err)
					return
				}
				pprof.StartCPUProfile(ofi)
				defer ofi.Close()
				defer pprof.StopCPUProfile()
102
			}
103 104 105
		} else if !ok {
			fmt.Println("error: expected 'debug' option to be a bool")
			os.Exit(1)
106 107
		}
	}
108
}
109

110
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
111
	var res cmds.Response
112

113 114
	if root == Root {
		res = root.Call(req)
115 116

	} else {
117 118 119 120 121 122
		options, err := getOptions(req, root)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

123
		var found bool
124
		var local interface{}
125
		localBool := false
126
		if local, found = options.Option("local"); found {
127 128 129 130 131 132 133
			var ok bool
			localBool, ok = local.(bool)
			if !ok {
				fmt.Println("error: expected 'local' option to be a bool")
				os.Exit(1)
			}
		}
134

135
		if (!found || !localBool) && daemon.Locked(req.Context().ConfigRoot) {
136 137 138 139 140 141 142
			res, err = cmdsHttp.Send(req)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

		} else {
143
			node, err := core.NewIpfsNode(req.Context().Config, false)
144 145 146 147
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
148
			req.Context().Node = node
149

150
			res = root.Call(req)
151 152 153
		}
	}

154 155 156 157
	return res
}

func outputResponse(res cmds.Response) {
158 159 160
	if res.Error() != nil {
		fmt.Println(res.Error().Error())

161
		if res.Request().Command().Help != "" && res.Error().Code == cmds.ErrClient {
162
			// TODO: convert from markdown to ANSI terminal format?
163
			fmt.Println(res.Request().Command().Help)
164 165 166 167 168
		}

		os.Exit(1)
	}

169
	out, err := res.Reader()
170 171
	if err != nil {
		fmt.Println(err.Error())
172
		return
173
	}
174 175

	io.Copy(os.Stdout, out)
176 177 178
}

func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
179
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	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 {
196 197 198 199 200
		if optStr, ok := opt.(string); ok {
			return optStr, nil
		} else {
			return "", fmt.Errorf("Expected 'config' option to be a string")
		}
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	}

	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)
}