RepoStat: address review comments
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Jun 14, 2018 at 13:12 UTC
9d88d2cde87f10f5d14a8f11c78e4d3d4f5623fe
2 files changed
+49
-39
core/commands/repo.go
+28
-25
@@ -150,7 +150,7 @@ var repoStatCmd = &cmds.Command{
150
Helptext: cmdkit.HelpText{
151
Tagline: "Get stats for the currently used repo.",
152
ShortDescription: `
153
-'ipfs repo stat' provides information about the local set of
153
+'ipfs repo stat' provides information about the local set of
154
stored objects. It outputs:
155
156
RepoSize int Size in bytes that the repo is currently taking.
@@ -171,26 +171,33 @@ Version string The repo version.
171
return
172
}
173
174
- statF := corerepo.RepoStat
175
-
174
sizeOnly, _ := req.Options["size-only"].(bool)
175
if sizeOnly {
178
- statF = corerepo.RepoSize
176
+ sizeStat, err := corerepo.RepoSize(req.Context, n)
177
+ if err != nil {
178
+ res.SetError(err, cmdkit.ErrNormal)
179
+ return
180
+ }
181
+ cmds.EmitOnce(res, &corerepo.Stat{
182
+ SizeStat: sizeStat,
183
+ })
184
+ return
185
}
186
181
- stat, err := statF(req.Context, n)
187
+ stat, err := corerepo.RepoStat(req.Context, n)
188
if err != nil {
189
res.SetError(err, cmdkit.ErrNormal)
190
return
191
}
192
187
- cmds.EmitOnce(res, stat)
193
+ cmds.EmitOnce(res, &stat)
194
},
189
- Type: corerepo.Stat{},
195
+ Type: &corerepo.Stat{},
196
Encoders: cmds.EncoderMap{
197
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
198
stat, ok := v.(*corerepo.Stat)
199
if !ok {
200
+ fmt.Println("adios")
201
return e.TypeErr(stat, v)
202
}
203
@@ -200,32 +207,28 @@ Version string The repo version.
207
human, _ := req.Options["human"].(bool)
208
sizeOnly, _ := req.Options["size-only"].(bool)
209
203
- sizeInMiB := stat.RepoSize / (1024 * 1024)
204
- if human && sizeInMiB > 0 {
205
- fmt.Fprintf(wtr, "RepoSize (MiB):\t%d\n", sizeInMiB)
206
- } else {
207
- fmt.Fprintf(wtr, "RepoSize:\t%d\n", stat.RepoSize)
208
- }
209
-
210
- if stat.StorageMax != corerepo.NoLimit {
211
- maxSizeInMiB := stat.StorageMax / (1024 * 1024)
212
- if human && maxSizeInMiB > 0 {
213
- fmt.Fprintf(wtr, "StorageMax (MiB):\t%d\n", maxSizeInMiB)
210
+ printSize := func(name string, size uint64) {
211
+ sizeInMiB := size / (1024 * 1024)
212
+ if human && sizeInMiB > 0 {
213
+ fmt.Fprintf(wtr, "%s (MiB):\t%d\n", name, sizeInMiB)
214
} else {
215
- fmt.Fprintf(wtr, "StorageMax:\t%d\n", stat.StorageMax)
215
+ fmt.Fprintf(wtr, "%s:\t%d\n", name, size)
216
}
217
}
218
219
- if sizeOnly {
220
- return nil
219
+ if !sizeOnly {
220
+ fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
221
}
222
223
- fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
224
- fmt.Fprintf(wtr, "RepoPath:\t%s\n", stat.RepoPath)
225
- fmt.Fprintf(wtr, "Version:\t%s\n", stat.Version)
223
+ printSize("RepoSize", stat.RepoSize)
224
+ printSize("StorageMax", stat.StorageMax)
225
227
- return nil
226
+ if !sizeOnly {
227
+ fmt.Fprintf(wtr, "RepoPath:\t%s\n", stat.RepoPath)
228
+ fmt.Fprintf(wtr, "Version:\t%s\n", stat.Version)
229
+ }
230
231
+ return nil
232
}),
233
},
234
}
core/corerepo/stat.go
+21
-14
@@ -12,10 +12,15 @@ import (
12
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
13
)
14
15
-// Stat wraps information about the objects stored on disk.
16
-type Stat struct {
15
+// SizeStat wraps information about the repository size and its limit.
16
+type SizeStat struct {
17
RepoSize uint64 // size in bytes
18
StorageMax uint64 // size in bytes
19
+}
20
+
21
+// Stat wraps information about the objects stored on disk.
22
+type Stat struct {
23
+ SizeStat
24
NumObjects uint64
25
RepoPath string
26
Version string
@@ -25,15 +30,15 @@ type Stat struct {
30
const NoLimit uint64 = math.MaxUint64
31
32
// RepoStat returns a *Stat object with all the fields set.
28
-func RepoStat(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
33
+func RepoStat(ctx context.Context, n *core.IpfsNode) (Stat, error) {
34
sizeStat, err := RepoSize(ctx, n)
35
if err != nil {
31
- return nil, err
36
+ return Stat{}, err
37
}
38
39
allKeys, err := n.Blockstore.AllKeysChan(ctx)
40
if err != nil {
36
- return nil, err
41
+ return Stat{}, err
42
}
43
44
count := uint64(0)
@@ -43,41 +48,43 @@ func RepoStat(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
48
49
path, err := fsrepo.BestKnownPath()
50
if err != nil {
46
- return nil, err
51
+ return Stat{}, err
52
}
53
49
- return &Stat{
54
+ return Stat{
55
+ SizeStat: SizeStat{
56
+ RepoSize: sizeStat.RepoSize,
57
+ StorageMax: sizeStat.StorageMax,
58
+ },
59
NumObjects: count,
51
- RepoSize: sizeStat.RepoSize,
52
- StorageMax: sizeStat.StorageMax,
60
RepoPath: path,
61
Version: fmt.Sprintf("fs-repo@%d", fsrepo.RepoVersion),
62
}, nil
63
}
64
65
// RepoSize returns a *Stat object with the RepoSize and StorageMax fields set.
59
-func RepoSize(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
66
+func RepoSize(ctx context.Context, n *core.IpfsNode) (SizeStat, error) {
67
r := n.Repo
68
69
cfg, err := r.Config()
70
if err != nil {
64
- return nil, err
71
+ return SizeStat{}, err
72
}
73
74
usage, err := r.GetStorageUsage()
75
if err != nil {
69
- return nil, err
76
+ return SizeStat{}, err
77
}
78
79
storageMax := NoLimit
80
if cfg.Datastore.StorageMax != "" {
81
storageMax, err = humanize.ParseBytes(cfg.Datastore.StorageMax)
82
if err != nil {
76
- return nil, err
83
+ return SizeStat{}, err
84
}
85
}
86
80
- return &Stat{
87
+ return SizeStat{
88
RepoSize: usage,
89
StorageMax: storageMax,
90
}, nil