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

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

9 10
	"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
11
	"github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
	core "github.com/jbenet/go-ipfs/core"
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
13
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
16
// The IPFS command tree. It is an instance of `commander.Command`.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30
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
31 32 33
    config        Manage configuration.
    version       Show ipfs version information.
    commands      List all available commands.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
34 35 36

Advanced Commands:

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
func main() {
73
	u.Debug = true
74 75 76 77 78 79 80 81 82
	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
83 84
	if err != nil {
		if len(err.Error()) > 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
			fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
86 87 88 89
		}
		os.Exit(1)
	}
	return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91

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

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

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

// Gets the config "-c" flag from the command, or returns
107
// the default configuration root directory
108
func getConfigDir(c *commander.Command) (string, error) {
Jeromy's avatar
Jeromy committed
109
	conf := c.Flag.Lookup("c").Value.Get()
110
	if conf == nil {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
111
		return config.PathRoot()
112
	}
Jeromy's avatar
Jeromy committed
113
	confStr, ok := conf.(string)
114 115
	if !ok {
		return "", errors.New("failed to retrieve config flag value.")
Jeromy's avatar
Jeromy committed
116
	}
117
	if len(confStr) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
118
		return config.PathRoot()
119
	}
120 121

	return u.TildeExpansion(confStr)
Jeromy's avatar
Jeromy committed
122
}