Skip to content

Instantly share code, notes, and snippets.

@mittenchops
Forked from leerspace/add_pin_unpin.go
Created June 11, 2018 06:07
Show Gist options
  • Save mittenchops/b2bb8fb4493eee373a8d77a3515c321e to your computer and use it in GitHub Desktop.
Save mittenchops/b2bb8fb4493eee373a8d77a3515c321e to your computer and use it in GitHub Desktop.
example of adding, pinning, and unpinning using IPFS as library
package main
import (
"context"
"log"
"net"
core "github.com/ipfs/go-ipfs/core"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd, err := core.NewNode(ctx, &core.BuildCfg{})
if err != nil {
log.Fatal(err)
}
list, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
}
log.Println("listening on: ", list.Addr())
// add directory
hash, err := coreunix.AddR(nd, "test_directory")
if err != nil {
log.Fatal(err)
}
log.Println("added directory with multihash: ", hash)
// pin a hash
pinlist, err := corerepo.Pin(nd, ctx, []string{hash}, true)
if err != nil {
log.Fatal(err)
}
log.Println("pinned cid: ", pinlist)
// unpin a hash
unpinlist, err := corerepo.Unpin(nd, ctx, []string{hash}, true)
if err != nil {
log.Fatal(err)
}
log.Println("unpinned cid: ", unpinlist)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment