github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands/pin.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/jbenet/go-ipfs/core"
     8  )
     9  
    10  func Pin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
    11  
    12  	// set recursive flag
    13  	recursive, _ := opts["r"].(bool) // false if cast fails.
    14  
    15  	// if recursive, set depth flag
    16  	depth := 1 // default (non recursive)
    17  	if d, ok := opts["d"].(int); recursive && ok {
    18  		depth = d
    19  	}
    20  	if depth < -1 {
    21  		return fmt.Errorf("ipfs pin: called with invalid depth: %v", depth)
    22  	}
    23  
    24  	fmt.Printf("recursive, depth: %v, %v\n", recursive, depth)
    25  
    26  	for _, fn := range args {
    27  		dagnode, err := n.Resolver.ResolvePath(fn)
    28  		if err != nil {
    29  			return fmt.Errorf("pin error: %v", err)
    30  		}
    31  
    32  		err = n.Pinning.Pin(dagnode, recursive)
    33  		if err != nil {
    34  			return fmt.Errorf("pin: %v", err)
    35  		}
    36  	}
    37  	return n.Pinning.Flush()
    38  }
    39  
    40  func Unpin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
    41  
    42  	// set recursive flag
    43  	recursive, _ := opts["r"].(bool) // false if cast fails.
    44  
    45  	for _, fn := range args {
    46  		dagnode, err := n.Resolver.ResolvePath(fn)
    47  		if err != nil {
    48  			return fmt.Errorf("pin error: %v", err)
    49  		}
    50  
    51  		k, _ := dagnode.Key()
    52  		err = n.Pinning.Unpin(k, recursive)
    53  		if err != nil {
    54  			return fmt.Errorf("pin: %v", err)
    55  		}
    56  	}
    57  	return n.Pinning.Flush()
    58  }