ipfs.go 2.89 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package main

import (
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
4
	"fmt"
5
	"os"
6
	"runtime/pprof"
7

8 9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
Jeromy's avatar
Jeromy committed
10
	"github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	core "github.com/jbenet/go-ipfs/core"
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
12
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15
// The IPFS command tree. It is an instance of `commander.Command`.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
var CmdIpfs = &commander.Command{
	UsageLine: "ipfs [<flags>] <command> [<args>]",
	Short:     "global versioned p2p merkledag file system",
	Long: `ipfs - global versioned p2p merkledag file system

Basic commands:

    add <path>    Add an object to ipfs.
    cat <ref>     Show ipfs object data.
    ls <ref>      List links from an object.
    refs <ref>    List link hashes from an object.

Tool commands:

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32
    config        Manage configuration.
    version       Show ipfs version information.
    commands      List all available commands.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
33 34 35

Advanced Commands:

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
    mount         Mount an ipfs read-only mountpoint.
37
    serve         Serve an interface to ipfs.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
38 39 40 41 42

Use "ipfs help <command>" for more information about a command.
`,
	Run: ipfsCmd,
	Subcommands: []*commander.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
		cmdIpfsAdd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
		cmdIpfsCat,
45
		cmdIpfsLs,
Juan Batiz-Benet's avatar
refs  
Juan Batiz-Benet committed
46
		cmdIpfsRefs,
47
		cmdIpfsConfig,
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
48 49
		cmdIpfsVersion,
		cmdIpfsCommands,
50
		cmdIpfsMount,
51
		cmdIpfsInit,
verokarhu's avatar
verokarhu committed
52
		cmdIpfsServe,
53
		cmdIpfsBootstrap,
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
54 55 56 57
	},
	Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}

Jeromy's avatar
Jeromy committed
58
func init() {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
59
	config, err := config.PathRoot()
60 61 62 63
	if err != nil {
		config = ""
	}
	CmdIpfs.Flag.String("c", config, "specify config directory")
Jeromy's avatar
Jeromy committed
64 65
}

Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
66 67 68 69 70
func ipfsCmd(c *commander.Command, args []string) error {
	u.POut(c.Long)
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
func main() {
72
	u.Debug = true
73 74 75 76 77 78 79 80 81
	ofi, err := os.Create("cpu.prof")
	if err != nil {
		fmt.Println(err)
		return
	}
	pprof.StartCPUProfile(ofi)
	defer ofi.Close()
	defer pprof.StopCPUProfile()
	err = CmdIpfs.Dispatch(os.Args[1:])
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
82 83
	if err != nil {
		if len(err.Error()) > 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
			fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
85 86 87 88
		}
		os.Exit(1)
	}
	return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90

91
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
92
	filename, err := config.Filename(confdir)
93 94 95 96 97
	if err != nil {
		return nil, err
	}

	cfg, err := config.Load(filename)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99 100 101
	if err != nil {
		return nil, err
	}

102
	return core.NewIpfsNode(cfg, online)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
}
Jeromy's avatar
Jeromy committed
104 105

// Gets the config "-c" flag from the command, or returns
106
// the default configuration root directory
107
func getConfigDir(c *commander.Command) (string, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112 113 114 115 116

	// use the root cmd (that's where config is specified)
	for ; c.Parent != nil; c = c.Parent {
	}

	// flag should be defined on root.
	param := c.Flag.Lookup("c").Value.Get().(string)
	if param != "" {
		return u.TildeExpansion(param)
117
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118 119 120 121 122 123 124 125

	return config.PathRoot()
}

func getConfig(c *commander.Command) (*config.Config, error) {
	confdir, err := getConfigDir(c)
	if err != nil {
		return nil, err
Jeromy's avatar
Jeromy committed
126
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128 129 130

	filename, err := config.Filename(confdir)
	if err != nil {
		return nil, err
131
	}
132

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
	return config.Load(filename)
Jeromy's avatar
Jeromy committed
134
}