ipfs.go 3.06 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4
package main

import (
	"fmt"
5
	"io"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6 7 8
	"os"
	"runtime/pprof"

9 10
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"

11
	cmds "github.com/jbenet/go-ipfs/commands"
12 13
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
14
	"github.com/jbenet/go-ipfs/config"
15
	"github.com/jbenet/go-ipfs/core/commands"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18 19 20 21 22
	u "github.com/jbenet/go-ipfs/util"
)

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

func main() {
23
	args := os.Args[1:]
24
	root := Root
25

26
	req, err := cmdsCli.Parse(args, root)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
	if err != nil {
28
		fmt.Println(err)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31
		os.Exit(1)
	}

32
	// if the CLI-specific root doesn't contain the command, use the general root
33
	if len(req.Path()) == 0 {
34 35
		root = commands.Root
		req, err = cmdsCli.Parse(args, root)
36 37 38 39
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
40 41
	}

42 43
	cmd, err := root.Get(req.Path())
	if err != nil {
44 45 46
		fmt.Println(err)
		os.Exit(1)
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47

48 49 50 51 52
	options, err := getOptions(req, root)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
53

54
	if debug, found := options.Option("debug"); found && debug.(bool) {
55
		u.Debug = true
Brian Tiger Chow's avatar
Brian Tiger Chow committed
56

57 58 59 60 61 62 63 64 65 66 67
		// 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()
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
68 69
	}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	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

86
	var res cmds.Response
87
	if root == Root {
88
		res = root.Call(req)
89

90
	} else {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		local := true

		lockFilePath, err := config.Path(configPath, DaemonLockFile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		if lk, err := lock.Lock(lockFilePath); err != nil {
			local = false
		} else {
			lk.Close()
		}

		if !local {
			res, err = cmdsHttp.Send(req)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

		} else {
			// TODO: spin up node
			res = root.Call(req)
		}
116
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
117

118 119
	if res.Error() != nil {
		fmt.Println(res.Error().Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
120

121 122 123 124
		if cmd.Help != "" && res.Error().Code == cmds.ErrClient {
			// TODO: convert from markdown to ANSI terminal format?
			fmt.Println(cmd.Help)
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
125

126
		os.Exit(1)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
127 128
	}

129
	_, err = io.Copy(os.Stdout, res)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
130
	if err != nil {
131
		fmt.Println(err.Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
132 133
	}
}
134

135
func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
136 137 138 139 140 141 142 143 144 145 146 147
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)

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

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

148 149 150
	return tempReq, nil
}

151
func getConfigRoot(req cmds.Request) (string, error) {
152 153 154 155 156 157 158 159 160
	if opt, found := req.Option("config"); found {
		return opt.(string), nil
	}

	configPath, err := config.PathRoot()
	if err != nil {
		return "", err
	}
	return configPath, nil
161
}
Matt Bell's avatar
Matt Bell committed
162 163 164 165 166 167 168 169 170

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

	return config.Load(configFile)
}