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

import (
	"fmt"
5 6
	"os"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8
	"github.com/gonuts/flag"
	"github.com/jbenet/commander"
9
	daemon "github.com/jbenet/go-ipfs/daemon"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	u "github.com/jbenet/go-ipfs/util"
)

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
	}

37 38 39 40 41 42
	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
43
	if err != nil {
44 45
		// Do locally
		n, err := localNode(false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
		if err != nil {
47
			return err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
		}
49

50
		daemon.ExecuteCommand(cmd, n, os.Stdout)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
	}
52
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
}