@cryptotaxi247 / kubo / commits / bb9904bde

repo: add `ipfs repo stat` command

License: MIT Signed-off-by: Thomas Gardner <tmg@fastmail.com>

Thomas Gardner committed Feb 3, 2016 at 18:51 UTC bb9904bde148eb9566d2a442f8f9fa1f2372ff9e
1 file changed +68 -1
core/commands/repo.go
+68 -1
@@ -4,12 +4,20 @@ import (
4 "bytes"
5 "fmt"
6 "io"
7 + "strings"
8
9 cmds "github.com/ipfs/go-ipfs/commands"
10 corerepo "github.com/ipfs/go-ipfs/core/corerepo"
11 + fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
12 u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
13 )
14
15 +type RepoStat struct {
16 + repoPath string
17 + repoSize uint64 // size in bytes
18 + numBlocks uint64
19 +}
20 +
21 var RepoCmd = &cmds.Command{
22 Helptext: cmds.HelpText{
23 Tagline: "Manipulate the IPFS repo.",
@@ -19,7 +27,8 @@ var RepoCmd = &cmds.Command{
27 },
28
29 Subcommands: map[string]*cmds.Command{
22 - "gc": repoGcCmd,
30 + "gc": repoGcCmd,
31 + "stat": repoStatCmd,
32 },
33 }
34
@@ -95,3 +104,61 @@ order to reclaim hard disk space.
104 },
105 },
106 }
107 +
108 +var repoStatCmd = &cmds.Command{
109 + Helptext: cmds.HelpText{
110 + Tagline: "Print status of the local repo.",
111 + ShortDescription: ``,
112 + },
113 + Run: func(req cmds.Request, res cmds.Response) {
114 + ctx := req.Context()
115 + n, err := req.InvocContext().GetNode()
116 + if err != nil {
117 + res.SetError(err, cmds.ErrNormal)
118 + return
119 + }
120 +
121 + usage, err := n.Repo.GetStorageUsage()
122 + if err != nil {
123 + res.SetError(err, cmds.ErrNormal)
124 + return
125 + }
126 +
127 + allKeys, err := n.Blockstore.AllKeysChan(ctx)
128 + if err != nil {
129 + res.SetError(err, cmds.ErrNormal)
130 + return
131 + }
132 +
133 + count := uint64(0)
134 + for range allKeys {
135 + count++
136 + }
137 +
138 + path, err := fsrepo.BestKnownPath()
139 + if err != nil {
140 + res.SetError(err, cmds.ErrNormal)
141 + return
142 + }
143 +
144 + out := &RepoStat{
145 + repoPath: path,
146 + repoSize: usage,
147 + numBlocks: count,
148 + }
149 + res.SetOutput(out)
150 + },
151 + Marshalers: cmds.MarshalerMap{
152 + cmds.Text: func(res cmds.Response) (io.Reader, error) {
153 + stat, ok := res.Output().(*RepoStat)
154 + if !ok {
155 + return nil, u.ErrCast()
156 + }
157 +
158 + out := fmt.Sprintf("Path: %s\nSize: %d bytes\n"+
159 + "Blocks: %d\n",
160 + stat.repoPath, stat.repoSize, stat.numBlocks)
161 + return strings.NewReader(out), nil
162 + },
163 + },
164 +}