0

I am a Newbie of ipfs and go, trying to get the ipfs information using go-ipfs-api. Following is my code:

package main

import (
         "fmt"
         "context"
         "os"
         "net/http"
         "github.com/prometheus/client_golang/prometheus"
         "github.com/prometheus/client_golang/prometheus/promhttp"
         "github.com/ipfs/go-ipfs-api"
)
 
 var sh *shell.Shell
 
 func main() {
         sh := shell.NewShell("localhost:5001")
         cid, err := sh.SwarmPeers()
         if err != nil {
                 fmt.Fprintf(os.Stderr, "error: %s", err)
                 os.Exit(1)
         }
         fmt.Printf("added %s", cid)
 }

This sh.SwarmPeers need some context as a parameter. I am not getting what parameter do I need to pass to get the peers list. Also, which API functions should I use to get the total number of files and the total size of files that exist on ipfs? Please help. If any tutorials are present please share. It will help me to understand using other functions. Any help would be appreciated.

Varsh
  • 413
  • 9
  • 26

1 Answers1

1

The context is a Golang context which helps track things like deadlines or cancellations of operations. In this case because you're making an HTTP call to your local IPFS daemon it's possible you might want to cancel the function because it's taking too long or your application no longer cares and the context lets you handle that use case.

You can get a background context (i.e. lives forever and is never cancelled) via context.Background(). If you want to set a timeout on it you can pass that background context to context.WithTimeout. Checkout the Golang docs on context for more information.

Adin Schmahmann
  • 263
  • 1
  • 7
  • Thank you for your reply. It helps me. can you help in getting the total file count with their size in the same program? – Varsh Mar 02 '21 at 09:32
  • You'll have to be more precise about what you're looking for. Basically every CLI command has a corresponding HTTP equivalent, and of those the vast majority are exposed by go-ipfs-api. For example, you can use the equivalent of `ipfs ls` to inspect directories under a given root, `ipfs pin ls` for the list of pinned CIDs, or `ipfs repo stat` for information on the repository size and the number of blocks. – Adin Schmahmann Mar 02 '21 at 15:02
  • I am finding an issue using these commands as `go-ipfs-api` functions. This question's answer is acceptable. For more queries, I will post a new question. – Varsh Mar 03 '21 at 09:30