version: --short flag to print only the version (#601)

Per discussion in #572, add a --short flag to the version command
to print only the version number.

Resolves #572
This commit is contained in:
Abhinav Gupta
2025-02-24 09:30:37 -05:00
committed by GitHub
parent 25bf36c2d6
commit a4b98093b0
3 changed files with 37 additions and 1 deletions
+4
View File
@@ -1034,3 +1034,7 @@ gs version [flags]
Print version information and quit
**Flags**
* `--short`: Print only the version number.
+8 -1
View File
@@ -19,9 +19,16 @@ func (v versionFlag) BeforeReset(app *kong.Kong) error {
return nil
}
type versionCmd struct{}
type versionCmd struct {
Short bool `help:"Print only the version number."`
}
func (cmd *versionCmd) Run(app *kong.Kong) error {
if cmd.Short {
fmt.Fprintln(app.Stdout, _version)
return nil
}
fmt.Fprint(app.Stdout, "git-spice ", _version)
if report := _generateBuildReport(); report != "" {
fmt.Fprintf(app.Stdout, " (%s)", report)
+25
View File
@@ -7,6 +7,7 @@ import (
"github.com/alecthomas/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/testing/stub"
)
@@ -29,6 +30,30 @@ func TestVersionFlag(t *testing.T) {
assert.Contains(t, stdout.String(), "(commithash timestamp)")
}
func TestVersionCmd(t *testing.T) {
defer stub.Value(&_version, "v1.2.3")()
t.Run("Default", func(t *testing.T) {
var stdout bytes.Buffer
err := new(versionCmd).Run(&kong.Kong{
Stdout: &stdout,
})
require.NoError(t, err)
assert.Contains(t, stdout.String(), "git-spice v1.2.3")
})
t.Run("Short", func(t *testing.T) {
var stdout bytes.Buffer
err := (&versionCmd{Short: true}).Run(&kong.Kong{
Stdout: &stdout,
})
require.NoError(t, err)
assert.Equal(t, "v1.2.3\n", stdout.String())
})
}
func TestGenerateBuildReport(t *testing.T) {
tests := []struct {
name string