ipfs.go 2.68 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,
Jeromy's avatar
Jeromy committed
54
		cmdIpfsRun,
55
		cmdIpfsPub,
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
56 57 58 59
	},
	Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}

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

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

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

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

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

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

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

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