Mercurial > hg > Members > anatofuz > growsync
changeset 4:359eff175bf1
add version cmd
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 01 Dec 2020 21:37:47 +0900 |
parents | 3032e9f78e4b |
children | af840bc25791 |
files | cmd_version.go growsync.go version.go |
diffstat | 3 files changed, 24 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd_version.go Tue Dec 01 21:37:47 2020 +0900 @@ -0,0 +1,22 @@ +package growsync + +import ( + "context" + "fmt" + "io" +) + +type versionCmd struct{} + +func (pc *versionCmd) name() string { + return "version" +} + +func (pc *versionCmd) description() string { + return "display version" +} + +func (pc *versionCmd) run(ctx context.Context, argv []string, config *growiConfig, stdWriter io.Writer, errorWriter io.Writer) error { + _, err := fmt.Fprintf(stdWriter, "%s v%s\n", cmdName, version) + return err +}
--- a/growsync.go Tue Dec 01 21:30:45 2020 +0900 +++ b/growsync.go Tue Dec 01 21:37:47 2020 +0900 @@ -13,7 +13,7 @@ const cmdName = "growsync" var ( - subCommands = []cmd{&newCmd{}, &pushCmd{}, &rootCmd{}} + subCommands = []cmd{&newCmd{}, &pushCmd{}, &rootCmd{}, &versionCmd{}} dispatch = make(map[string]cmd, len(subCommands)) maxSubcommandName int ) @@ -32,7 +32,7 @@ // Run the lectable func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) error { log.SetOutput(errStream) - nameAndVer := fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision) + nameAndVer := fmt.Sprintf("%s (v%s)", cmdName, version) fs := flag.NewFlagSet(nameAndVer, flag.ContinueOnError) fs.SetOutput(errStream) fs.Usage = func() { @@ -41,13 +41,9 @@ fmt.Fprintf(fs.Output(), "\nCommands:\n") formatCommands(fs.Output()) } - ver := fs.Bool("version", false, "display version") if err := fs.Parse(argv); err != nil { return err } - if *ver { - return printVersion(outStream) - } argv = fs.Args() if len(argv) < 1 { @@ -66,11 +62,6 @@ return rnr.run(context.Background(), argv[1:], config, outStream, errStream) } -func printVersion(out io.Writer) error { - _, err := fmt.Fprintf(out, "%s v%s (rev:%s)\n", cmdName, version, revision) - return err -} - func formatCommands(out io.Writer) { format := fmt.Sprintf(" %%-%ds %%s\n", maxSubcommandName) for _, r := range subCommands {