ipfs.go 2.2 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 7
	"os"

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

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

Advanced Commands:

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
    mount         Mount an ipfs read-only mountpoint.
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
37 38 39 40 41

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

Jeromy's avatar
Jeromy committed
55
func init() {
56
	CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
Jeromy's avatar
Jeromy committed
57 58
}

Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
59 60 61 62 63
func ipfsCmd(c *commander.Command, args []string) error {
	u.POut(c.Long)
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64
func main() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
	// u.Debug = true
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
66 67 68
	err := CmdIpfs.Dispatch(os.Args[1:])
	if err != nil {
		if len(err.Error()) > 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
			fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
Juan Batiz-Benet's avatar
refmt  
Juan Batiz-Benet committed
70 71 72 73
		}
		os.Exit(1)
	}
	return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75

76 77
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
	cfg, err := config.Load(confdir + "/config")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79 80 81
	if err != nil {
		return nil, err
	}

82
	return core.NewIpfsNode(cfg, online)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
}
Jeromy's avatar
Jeromy committed
84 85 86

// Gets the config "-c" flag from the command, or returns
// the empty string
87
func getConfigDir(c *commander.Command) (string, error) {
Jeromy's avatar
Jeromy committed
88
	conf := c.Flag.Lookup("c").Value.Get()
89 90 91
	if conf == nil {
		return "", nil
	}
Jeromy's avatar
Jeromy committed
92
	confStr, ok := conf.(string)
93 94
	if !ok {
		return "", errors.New("failed to retrieve config flag value.")
Jeromy's avatar
Jeromy committed
95
	}
96
	return confStr, nil
Jeromy's avatar
Jeromy committed
97
}