1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-01 16:05:47 +08:00
Files
kubo/core/commands/version.go
Jeromy 7811983901 add option to version command to print repo version
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
2015-11-03 12:19:12 -08:00

72 lines
1.6 KiB
Go

package commands
import (
"fmt"
"io"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
)
type VersionOutput struct {
Version string
Commit string
Repo string
}
var VersionCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Shows ipfs version information",
ShortDescription: "Returns the current version of ipfs and exits.",
},
Options: []cmds.Option{
cmds.BoolOption("number", "n", "Only show the version number"),
cmds.BoolOption("commit", "Show the commit hash"),
cmds.BoolOption("repo", "Show repo version"),
},
Run: func(req cmds.Request, res cmds.Response) {
res.SetOutput(&VersionOutput{
Version: config.CurrentVersionNumber,
Commit: config.CurrentCommit,
Repo: fsrepo.RepoVersion,
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*VersionOutput)
repo, _, err := res.Request().Option("repo").Bool()
if err != nil {
return nil, err
}
if repo {
return strings.NewReader(v.Repo + "\n"), nil
}
commit, found, err := res.Request().Option("commit").Bool()
commitTxt := ""
if err != nil {
return nil, err
}
if found && commit {
commitTxt = "-" + v.Commit
}
number, found, err := res.Request().Option("number").Bool()
if err != nil {
return nil, err
}
if found && number {
return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
}
return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
},
},
Type: VersionOutput{},
}