Skip to content

Commit

Permalink
Merge pull request #3915 from ipfs/kevina/repo-stat-storagemax
Browse files Browse the repository at this point in the history
Add MaxStorage field to output of "repo stat".
  • Loading branch information
whyrusleeping committed May 15, 2017
2 parents a4ffefd + cd8c4ee commit 219b41b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
19 changes: 14 additions & 5 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"text/tabwriter"

bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
cmds "github.com/ipfs/go-ipfs/commands"
Expand Down Expand Up @@ -182,15 +183,23 @@ Version string The repo version.
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "NumObjects \t %d\n", stat.NumObjects)
wtr := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
sizeInMiB := stat.RepoSize / (1024 * 1024)
if human && sizeInMiB > 0 {
fmt.Fprintf(buf, "RepoSize (MiB) \t %d\n", sizeInMiB)
fmt.Fprintf(wtr, "RepoSize (MiB):\t%d\n", sizeInMiB)
} else {
fmt.Fprintf(buf, "RepoSize \t %d\n", stat.RepoSize)
fmt.Fprintf(wtr, "RepoSize:\t%d\n", stat.RepoSize)
}
fmt.Fprintf(buf, "RepoPath \t %s\n", stat.RepoPath)
fmt.Fprintf(buf, "Version \t %s\n", stat.Version)
maxSizeInMiB := stat.StorageMax / (1024 * 1024)
if human && maxSizeInMiB > 0 {
fmt.Fprintf(wtr, "StorageMax (MiB):\t%d\n", maxSizeInMiB)
} else {
fmt.Fprintf(wtr, "StorageMax:\t%d\n", stat.StorageMax)
}
fmt.Fprintf(wtr, "RepoPath:\t%s\n", stat.RepoPath)
fmt.Fprintf(wtr, "Version:\t%s\n", stat.Version)
wtr.Flush()

return buf, nil
},
Expand Down
14 changes: 14 additions & 0 deletions core/corerepo/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
context "context"
"github.com/ipfs/go-ipfs/core"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
)

type Stat struct {
NumObjects uint64
RepoSize uint64 // size in bytes
RepoPath string
Version string
StorageMax uint64 // size in bytes
}

func RepoStat(n *core.IpfsNode, ctx context.Context) (*Stat, error) {
Expand All @@ -38,10 +41,21 @@ func RepoStat(n *core.IpfsNode, ctx context.Context) (*Stat, error) {
return nil, err
}

cfg, err := r.Config()
if err != nil {
return nil, err
}

storageMax, err := humanize.ParseBytes(cfg.Datastore.StorageMax)
if err != nil {
return nil, err
}

return &Stat{
NumObjects: count,
RepoSize: usage,
RepoPath: path,
Version: fmt.Sprintf("fs-repo@%d", fsrepo.RepoVersion),
StorageMax: storageMax,
}, nil
}
5 changes: 3 additions & 2 deletions test/sharness/t0080-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ test_expect_success "'ipfs repo stat' succeeds" '
test_expect_success "repo stats came out correct" '
grep "RepoPath" repo-stats &&
grep "RepoSize" repo-stats &&
grep "NumObjects" repo-stats
grep "Version" repo-stats
grep "NumObjects" repo-stats &&
grep "Version" repo-stats &&
grep "StorageMax" repo-stats
'

test_expect_success "'ipfs repo stat' after adding a file" '
Expand Down

0 comments on commit 219b41b

Please sign in to comment.