Skip to content

Commit 0c6fc93

Browse files
authored
feat(version): honor shared output settings (#49)
1 parent b43876b commit 0c6fc93

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

internal/buildinfo/buildinfo.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ var (
1515
Date = "unknown"
1616
)
1717

18+
// Details describes the current CLI build metadata.
19+
type Details struct {
20+
Version string `json:"version"`
21+
Commit string `json:"commit"`
22+
Date string `json:"date"`
23+
Dirty bool `json:"dirty"`
24+
}
25+
1826
func info() (version, commit, date string, dirty bool) {
1927
version = Version
2028
commit = Commit
@@ -47,20 +55,31 @@ func info() (version, commit, date string, dirty bool) {
4755
return version, commit, date, dirty
4856
}
4957

58+
// Current returns the current build metadata as a structured value.
59+
func Current() Details {
60+
version, commit, date, dirty := info()
61+
return Details{
62+
Version: version,
63+
Commit: commit,
64+
Date: date,
65+
Dirty: dirty,
66+
}
67+
}
68+
5069
// Short returns a concise version string suitable for --version output.
5170
func Short() string {
52-
version, commit, _, dirty := info()
71+
current := Current()
5372

5473
var parts []string
55-
parts = append(parts, version)
56-
if commit != "" && commit != "unknown" {
57-
shortCommit := commit
74+
parts = append(parts, current.Version)
75+
if current.Commit != "" && current.Commit != "unknown" {
76+
shortCommit := current.Commit
5877
if len(shortCommit) > 7 {
5978
shortCommit = shortCommit[:7]
6079
}
6180
parts = append(parts, shortCommit)
6281
}
63-
if dirty {
82+
if current.Dirty {
6483
parts = append(parts, "dirty")
6584
}
6685

@@ -69,11 +88,11 @@ func Short() string {
6988

7089
// Long returns full build details.
7190
func Long() string {
72-
version, commit, date, _ := info()
91+
current := Current()
7392
return fmt.Sprintf(
7493
"Version: %s\nCommit: %s\nDate: %s",
75-
version,
76-
commit,
77-
date,
94+
current.Version,
95+
current.Commit,
96+
current.Date,
7897
)
7998
}

internal/commands/version/version.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ import (
66

77
"github.com/urfave/cli/v3"
88

9+
"github.com/sumup/sumup-cli/internal/app"
910
"github.com/sumup/sumup-cli/internal/buildinfo"
11+
"github.com/sumup/sumup-cli/internal/display"
1012
)
1113

1214
// NewCommand returns the version command.
1315
func NewCommand() *cli.Command {
1416
return &cli.Command{
1517
Name: "version",
1618
Usage: "Print CLI build information",
17-
Action: func(_ context.Context, _ *cli.Command) error {
18-
fmt.Println(buildinfo.Long())
19-
return nil
19+
Action: func(_ context.Context, cmd *cli.Command) error {
20+
appCtx, err := app.GetAppContext(cmd)
21+
if err != nil {
22+
return err
23+
}
24+
25+
if appCtx.JSONOutput {
26+
return display.PrintJSON(appCtx.Output, buildinfo.Current())
27+
}
28+
29+
_, err = fmt.Fprintln(appCtx.Output, buildinfo.Long())
30+
return err
2031
},
2132
}
2233
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package version
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"strings"
7+
"testing"
8+
9+
"github.com/sumup/sumup-cli/internal/app"
10+
)
11+
12+
func TestVersionCommandWritesToAppOutput(t *testing.T) {
13+
var out bytes.Buffer
14+
cmd := NewCommand()
15+
cmd.Metadata = map[string]any{
16+
app.ContextKey: &app.Context{Output: &out},
17+
}
18+
19+
if err := cmd.Action(context.Background(), cmd); err != nil {
20+
t.Fatalf("Action() error = %v", err)
21+
}
22+
23+
if !strings.Contains(out.String(), "Version:") {
24+
t.Fatalf("Action() output = %q, want version details", out.String())
25+
}
26+
}
27+
28+
func TestVersionCommandPrintsJSONWhenRequested(t *testing.T) {
29+
var out bytes.Buffer
30+
cmd := NewCommand()
31+
cmd.Metadata = map[string]any{
32+
app.ContextKey: &app.Context{Output: &out, JSONOutput: true},
33+
}
34+
35+
if err := cmd.Action(context.Background(), cmd); err != nil {
36+
t.Fatalf("Action() error = %v", err)
37+
}
38+
39+
if !strings.Contains(out.String(), `"version"`) {
40+
t.Fatalf("Action() output = %q, want JSON payload", out.String())
41+
}
42+
}

0 commit comments

Comments
 (0)