mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
Merge pull request #2598 from pfista/2571-repo-version
`ipfs repo version` command
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
|
||||
config "github.com/ipfs/go-ipfs/repo/config"
|
||||
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
|
||||
lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
|
||||
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
|
||||
"io"
|
||||
@ -13,6 +14,10 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type RepoVersion struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
var RepoCmd = &cmds.Command{
|
||||
Helptext: cmds.HelpText{
|
||||
Tagline: "Manipulate the IPFS repo.",
|
||||
@ -22,9 +27,10 @@ var RepoCmd = &cmds.Command{
|
||||
},
|
||||
|
||||
Subcommands: map[string]*cmds.Command{
|
||||
"gc": repoGcCmd,
|
||||
"stat": repoStatCmd,
|
||||
"fsck": RepoFsckCmd,
|
||||
"gc": repoGcCmd,
|
||||
"stat": repoStatCmd,
|
||||
"fsck": RepoFsckCmd,
|
||||
"version": repoVersionCmd,
|
||||
},
|
||||
}
|
||||
|
||||
@ -109,6 +115,7 @@ set of stored objects and print repo statistics. It outputs to stdout:
|
||||
NumObjects int Number of objects in the local repo.
|
||||
RepoPath string The path to the repo being currently used.
|
||||
RepoSize int Size in bytes that the repo is currently taking.
|
||||
Version string The repo version.
|
||||
`,
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
@ -151,6 +158,7 @@ RepoSize int Size in bytes that the repo is currently taking.
|
||||
fmt.Fprintf(buf, "RepoSize \t %d\n", stat.RepoSize)
|
||||
}
|
||||
fmt.Fprintf(buf, "RepoPath \t %s\n", stat.RepoPath)
|
||||
fmt.Fprintf(buf, "Version \t %s\n", stat.Version)
|
||||
|
||||
return buf, nil
|
||||
},
|
||||
@ -208,3 +216,41 @@ daemons are running.
|
||||
cmds.Text: MessageTextMarshaler,
|
||||
},
|
||||
}
|
||||
|
||||
var repoVersionCmd = &cmds.Command{
|
||||
Helptext: cmds.HelpText{
|
||||
Tagline: "Show the repo version.",
|
||||
ShortDescription: `
|
||||
'ipfs repo version' returns the current repo version.
|
||||
`,
|
||||
},
|
||||
|
||||
Options: []cmds.Option{
|
||||
cmds.BoolOption("quiet", "q", "Write minimal output."),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
res.SetOutput(&RepoVersion{
|
||||
Version: fsrepo.RepoVersion,
|
||||
})
|
||||
},
|
||||
Type: RepoVersion{},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
response := res.Output().(*RepoVersion)
|
||||
|
||||
quiet, _, err := res.Request().Option("quiet").Bool()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if quiet {
|
||||
buf = bytes.NewBufferString(fmt.Sprintf("fs-repo@%s\n", response.Version))
|
||||
} else {
|
||||
buf = bytes.NewBufferString(fmt.Sprintf("ipfs repo version fs-repo@%s\n", response.Version))
|
||||
}
|
||||
return buf, nil
|
||||
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ type Stat struct {
|
||||
NumObjects uint64
|
||||
RepoSize uint64 // size in bytes
|
||||
RepoPath string
|
||||
Version string
|
||||
}
|
||||
|
||||
func RepoStat(n *core.IpfsNode, ctx context.Context) (*Stat, error) {
|
||||
@ -39,5 +40,6 @@ func RepoStat(n *core.IpfsNode, ctx context.Context) (*Stat, error) {
|
||||
NumObjects: count,
|
||||
RepoSize: usage,
|
||||
RepoPath: path,
|
||||
Version: "fs-repo@" + fsrepo.RepoVersion,
|
||||
}, nil
|
||||
}
|
||||
|
@ -233,6 +233,7 @@ test_expect_success "repo stats came out correct" '
|
||||
grep "RepoPath" repo-stats &&
|
||||
grep "RepoSize" repo-stats &&
|
||||
grep "NumObjects" repo-stats
|
||||
grep "Version" repo-stats
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs repo stat' after adding a file" '
|
||||
@ -244,6 +245,21 @@ test_expect_success "repo stats are updated correctly" '
|
||||
test $(get_field_num "RepoSize" repo-stats-2) -ge $(get_field_num "RepoSize" repo-stats)
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs repo version' succeeds" '
|
||||
ipfs repo version > repo-version
|
||||
'
|
||||
|
||||
test_expect_success "repo version came out correct" '
|
||||
egrep "^ipfs repo version fs-repo@[0-9]" repo-version >/dev/null
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs repo version -q' succeeds" '
|
||||
ipfs repo version -q > repo-version-q
|
||||
'
|
||||
test_expect_success "repo version came out correct" '
|
||||
egrep "^fs-repo@[0-9]" repo-version-q >/dev/null
|
||||
'
|
||||
|
||||
test_kill_ipfs_daemon
|
||||
|
||||
test_done
|
||||
|
Reference in New Issue
Block a user