From 07638d65fdaa7d94f91ea81b3739f4270ff46865 Mon Sep 17 00:00:00 2001 From: Michael Pfister Date: Wed, 20 Apr 2016 19:41:44 -0700 Subject: [PATCH] Adds repo version subcommand Fixes #2571 License: MIT Signed-off-by: Mike Pfister --- core/commands/repo.go | 48 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/core/commands/repo.go b/core/commands/repo.go index a333814aae9c..c5c8daec2d49 100644 --- a/core/commands/repo.go +++ b/core/commands/repo.go @@ -5,10 +5,15 @@ import ( "fmt" 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" "io" ) +type RepoVersion struct { + Version string +} + var RepoCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Manipulate the IPFS repo.", @@ -18,8 +23,9 @@ var RepoCmd = &cmds.Command{ }, Subcommands: map[string]*cmds.Command{ - "gc": repoGcCmd, - "stat": repoStatCmd, + "gc": repoGcCmd, + "stat": repoStatCmd, + "version": repoVersionCmd, }, } @@ -154,3 +160,41 @@ Version string The repo version }, }, } + +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 + + }, + }, +}