main.go 3.09 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 24
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:]

25
	req, root, err := cmdsCli.Parse(args, Root, commands.Root)
26 27
	if err != nil {
		fmt.Println(err)
28
		fmt.Println(Root.Help)
29 30 31
		os.Exit(1)
	}

32
	options, err := getOptions(req, root)
33 34 35 36 37 38
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if help, found := options.Option("help"); found && help.(bool) {
39
		fmt.Println(req.Command().Help)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		os.Exit(0)
	}

	if debug, found := options.Option("debug"); found && debug.(bool) {
		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()
		}
	}

	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 {
		if req.Command().Format != nil {
			req.SetOption("encoding", cmds.Text)
		} else {
			req.SetOption("encoding", cmds.JSON)
		}
	}

	var res cmds.Response
84 85
	if root == Root {
		res = root.Call(req)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

	} else {
		local, found := options.Option("local")

		if (!found || !local.(bool)) && daemon.Locked(configPath) {
			res, err = cmdsHttp.Send(req)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

		} else {
			node, err := core.NewIpfsNode(conf, false)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			ctx.Node = node

105
			res = root.Call(req)
106 107 108 109 110 111
		}
	}

	if res.Error() != nil {
		fmt.Println(res.Error().Error())

112
		if req.Command().Help != "" && res.Error().Code == cmds.ErrClient {
113
			// TODO: convert from markdown to ANSI terminal format?
114
			fmt.Println(req.Command().Help)
115 116 117 118 119
		}

		os.Exit(1)
	}

120
	out, err := res.Reader()
121 122
	if err != nil {
		fmt.Println(err.Error())
123
		return
124
	}
125 126

	io.Copy(os.Stdout, out)
127 128 129
}

func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
130
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

	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 {
		return opt.(string), nil
	}

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