Skip to content

Instantly share code, notes, and snippets.

@leerspace
Created January 9, 2018 13:30
Show Gist options
  • Save leerspace/663828ee1d5f9b174c1b5a5688de190b to your computer and use it in GitHub Desktop.
Save leerspace/663828ee1d5f9b174c1b5a5688de190b 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