pin.go 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package commands

import (
	"fmt"
	"io"

	"github.com/jbenet/go-ipfs/core"
)

func Pin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
11

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14 15 16 17 18 19 20 21
	// set recursive flag
	recursive, _ := opts["r"].(bool) // false if cast fails.

	// if recursive, set depth flag
	depth := 1 // default (non recursive)
	if d, ok := opts["d"].(int); recursive && ok {
		depth = d
	}
	if depth < -1 {
		return fmt.Errorf("ipfs pin: called with invalid depth: %v", depth)
22 23
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25
	fmt.Printf("recursive, depth: %v, %v\n", recursive, depth)

26 27 28 29 30 31
	for _, fn := range args {
		dagnode, err := n.Resolver.ResolvePath(fn)
		if err != nil {
			return fmt.Errorf("pin error: %v", err)
		}

Jeromy's avatar
Jeromy committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		err = n.Pinning.Pin(dagnode, recursive)
		if err != nil {
			return fmt.Errorf("pin: %v", err)
		}
	}
	return nil
}

func Unpin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {

	// set recursive flag
	recursive, _ := opts["r"].(bool) // false if cast fails.

	for _, fn := range args {
		dagnode, err := n.Resolver.ResolvePath(fn)
		if err != nil {
			return fmt.Errorf("pin error: %v", err)
		}

		k, _ := dagnode.Key()
		err = n.Pinning.Unpin(k, recursive)
53 54 55 56 57 58
		if err != nil {
			return fmt.Errorf("pin: %v", err)
		}
	}
	return nil
}