add.go 1.44 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4
package main

import (
	"fmt"
5 6
	"os"

7 8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
9
	"github.com/jbenet/go-ipfs/core/commands"
10
	"github.com/jbenet/go-ipfs/daemon"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13
	u "github.com/jbenet/go-ipfs/util"
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
14 15
// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
16

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
var cmdIpfsAdd = &commander.Command{
	UsageLine: "add",
	Short:     "Add an object to ipfs.",
	Long: `ipfs add <path>... - Add objects to ipfs.

    Adds contents of <path> to ipfs. Use -r to add directories.
    Note that directories are added recursively, to form the ipfs
    MerkleDAG. A smarter partial add with a staging area (like git)
    remains to be implemented.
`,
	Run:  addCmd,
	Flag: *flag.NewFlagSet("ipfs-add", flag.ExitOnError),
}

func init() {
	cmdIpfsAdd.Flag.Bool("r", false, "add objects recursively")
}

func addCmd(c *commander.Command, inp []string) error {
	if len(inp) < 1 {
		u.POut(c.Long)
		return nil
	}

41 42 43 44 45
	cmd := daemon.NewCommand()
	cmd.Command = "add"
	cmd.Args = inp
	cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get()
	err := daemon.SendCommand(cmd, "localhost:12345")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	if err != nil {
47
		// Do locally
48 49 50 51
		conf, err := getConfigDir(c.Parent)
		if err != nil {
			return err
		}
Jeromy's avatar
Jeromy committed
52
		n, err := localNode(conf, false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
		if err != nil {
54
			return err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
		}
56

57
		return commands.Add(n, cmd.Args, cmd.Opts, os.Stdout)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	}
59
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
}