ipfs.go 2.46 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 57 58 59 60
	config, err := config.GetDefaultPathRoot()
	if err != nil {
		config = ""
	}
	CmdIpfs.Flag.String("c", config, "specify config directory")
Jeromy's avatar
Jeromy committed
61 62
}

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

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

80
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
81 82 83 84 85 86
	filename, err := config.GetConfigFilePath(confdir)
	if err != nil {
		return nil, err
	}

	cfg, err := config.Load(filename)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90
	if err != nil {
		return nil, err
	}

91
	return core.NewIpfsNode(cfg, online)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
}
Jeromy's avatar
Jeromy committed
93 94

// Gets the config "-c" flag from the command, or returns
95
// the default configuration root directory
96
func getConfigDir(c *commander.Command) (string, error) {
Jeromy's avatar
Jeromy committed
97
	conf := c.Flag.Lookup("c").Value.Get()
98
	if conf == nil {
99
		return config.GetDefaultPathRoot()
100
	}
Jeromy's avatar
Jeromy committed
101
	confStr, ok := conf.(string)
102 103
	if !ok {
		return "", errors.New("failed to retrieve config flag value.")
Jeromy's avatar
Jeromy committed
104
	}
105 106 107
	if len(confStr) == 0 {
		return config.GetDefaultPathRoot()
	}
108 109

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