repo: add ipfs repo stat command

License: MIT
Signed-off-by: Thomas Gardner <tmg@fastmail.com>
This commit is contained in:
Thomas Gardner 2016-02-03 18:51:32 +10:00 committed by David Dias
parent 22fcd18ad6
commit bb9904bde1

View File

@ -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
},
},
}