ipfs.go 2.82 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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10 11 12
	flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"

	config "github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
	core "github.com/jbenet/go-ipfs/core"
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
14
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
)

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

Advanced Commands:

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75
func main() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	u.Debug = false

	// setup logging
	u.SetupLogging()

	// if debugging, setup profiling.
	if u.Debug {
		ofi, err := os.Create("cpu.prof")
		if err != nil {
			fmt.Println(err)
			return
		}
		pprof.StartCPUProfile(ofi)
		defer ofi.Close()
		defer pprof.StopCPUProfile()
91
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93

	err := CmdIpfs.Dispatch(os.Args[1:])
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
94 95
	if err != nil {
		if len(err.Error()) > 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
			fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
97 98 99 100
		}
		os.Exit(1)
	}
	return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102

103
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
104
	filename, err := config.Filename(confdir)
105 106 107 108 109
	if err != nil {
		return nil, err
	}

	cfg, err := config.Load(filename)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113
	if err != nil {
		return nil, err
	}

114
	return core.NewIpfsNode(cfg, online)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
115
}
Jeromy's avatar
Jeromy committed
116 117

// Gets the config "-c" flag from the command, or returns
118
// the default configuration root directory
119
func getConfigDir(c *commander.Command) (string, error) {
Jeromy's avatar
Jeromy committed
120
	conf := c.Flag.Lookup("c").Value.Get()
121
	if conf == nil {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
122
		return config.PathRoot()
123
	}
Jeromy's avatar
Jeromy committed
124
	confStr, ok := conf.(string)
125
	if !ok {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
		return "", errors.New("failed to retrieve config flag value")
Jeromy's avatar
Jeromy committed
127
	}
128
	if len(confStr) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
129
		return config.PathRoot()
130
	}
131 132

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