ipfs.go 2.59 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.
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,
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
52 53 54 55
	},
	Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}

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

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

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

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

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

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

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

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