ipfs.go 2.66 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.
verokarhu's avatar
verokarhu committed
38
    serve         Serve an http gateway into 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,
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) {
Jeromy's avatar
Jeromy committed
108
	conf := c.Flag.Lookup("c").Value.Get()
109
	if conf == nil {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
110
		return config.PathRoot()
111
	}
Jeromy's avatar
Jeromy committed
112
	confStr, ok := conf.(string)
113 114
	if !ok {
		return "", errors.New("failed to retrieve config flag value.")
Jeromy's avatar
Jeromy committed
115
	}
116
	if len(confStr) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
117
		return config.PathRoot()
118
	}
119 120

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