add.go 1.38 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/daemon"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12
	u "github.com/jbenet/go-ipfs/util"
)

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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
	}

40 41 42 43 44 45
	cmd := daemon.NewCommand()
	cmd.Command = "add"
	fmt.Println(inp)
	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 48
		// Do locally
		n, err := localNode(false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
		if err != nil {
50
			return err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
		}
52

53 54 55 56
		err := daemon.ExecuteCommand(cmd, n, os.Stdout)
		if err != nil {
			fmt.Println(err)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	}
58
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
}