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

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

18
const API_PATH = "/api/v0"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
19 20

func main() {
21
	req, err := cli.Parse(os.Args[1:], commands.Root)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
22
	if err != nil {
23
		fmt.Println(err)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24 25 26
		os.Exit(1)
	}

27
	cmd, err := commands.Root.Get(req.Path())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28
	if err != nil {
29 30
		fmt.Println(err)
		os.Exit(1)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31 32
	}

33
	// TODO: send request to daemon via HTTP API
Brian Tiger Chow's avatar
Brian Tiger Chow committed
34

35 36
	if debug, ok := req.Option("debug"); ok && debug.(bool) {
		u.Debug = true
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37

38 39 40 41 42 43 44 45 46 47 48
		// 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
49 50
	}

51
	res := commands.Root.Call(req)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
52

53 54
	if res.Error() != nil {
		fmt.Println(res.Error().Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
55

56 57 58 59
		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
60

61
		os.Exit(1)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
62 63
	}

64
	_, err = io.Copy(os.Stdout, res)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65
	if err != nil {
66
		fmt.Println(err.Error())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
67 68
	}
}