ipfs.go 2.01 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
	cmds "github.com/jbenet/go-ipfs/commands"
10 11
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12
	"github.com/jbenet/go-ipfs/core/commands"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13 14 15 16 17 18 19
	u "github.com/jbenet/go-ipfs/util"
)

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

func main() {
20
	args := os.Args[1:]
21
	root := Root
22

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

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

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

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

51
	if debug, ok := options["debug"]; ok && debug.(bool) {
52
		u.Debug = true
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53

54 55 56 57 58 59 60 61 62 63 64
		// 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
65 66
	}

67
	var res cmds.Response
68
	if root == Root {
69 70 71
		// TODO: spin up node
		res = root.Call(req)
	} else {
72
		res, err = cmdsHttp.Send(req)
73 74 75 76 77
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
78

79 80
	if res.Error() != nil {
		fmt.Println(res.Error().Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
81

82 83 84 85
		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
86

87
		os.Exit(1)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
88 89
	}

90
	_, err = io.Copy(os.Stdout, res)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
91
	if err != nil {
92
		fmt.Println(err.Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
93 94
	}
}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

func getOptions(req cmds.Request, root *cmds.Command) (map[string]interface{}, error) {
	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
	}

	return tempReq.Options(), nil
}