From bb9904bde148eb9566d2a442f8f9fa1f2372ff9e Mon Sep 17 00:00:00 2001 From: Thomas Gardner Date: Wed, 3 Feb 2016 18:51:32 +1000 Subject: [PATCH] repo: add `ipfs repo stat` command License: MIT Signed-off-by: Thomas Gardner --- core/commands/repo.go | 69 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/core/commands/repo.go b/core/commands/repo.go index 3b634e631..7af24f8bf 100644 --- a/core/commands/repo.go +++ b/core/commands/repo.go @@ -4,12 +4,20 @@ import ( "bytes" "fmt" "io" + "strings" cmds "github.com/ipfs/go-ipfs/commands" corerepo "github.com/ipfs/go-ipfs/core/corerepo" + fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" ) +type RepoStat struct { + repoPath string + repoSize uint64 // size in bytes + numBlocks uint64 +} + var RepoCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Manipulate the IPFS repo.", @@ -19,7 +27,8 @@ var RepoCmd = &cmds.Command{ }, Subcommands: map[string]*cmds.Command{ - "gc": repoGcCmd, + "gc": repoGcCmd, + "stat": repoStatCmd, }, } @@ -95,3 +104,61 @@ order to reclaim hard disk space. }, }, } + +var repoStatCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Print status of the local repo.", + ShortDescription: ``, + }, + Run: func(req cmds.Request, res cmds.Response) { + ctx := req.Context() + n, err := req.InvocContext().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + usage, err := n.Repo.GetStorageUsage() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + allKeys, err := n.Blockstore.AllKeysChan(ctx) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + count := uint64(0) + for range allKeys { + count++ + } + + path, err := fsrepo.BestKnownPath() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + out := &RepoStat{ + repoPath: path, + repoSize: usage, + numBlocks: count, + } + res.SetOutput(out) + }, + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) (io.Reader, error) { + stat, ok := res.Output().(*RepoStat) + if !ok { + return nil, u.ErrCast() + } + + out := fmt.Sprintf("Path: %s\nSize: %d bytes\n"+ + "Blocks: %d\n", + stat.repoPath, stat.repoSize, stat.numBlocks) + return strings.NewReader(out), nil + }, + }, +}