Skip to content

Commit 6f3f52e

Browse files
authored
fix: use account currency when formatting credits (#666)
1 parent 0504c8f commit 6f3f52e

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Log API requests and responses when using global `--debug` option.
1313

14+
### Fixed
15+
16+
- In human readable output of `account show`, use accounts currency for credits.
17+
1418
## [3.27.0] - 2025-12-31
1519

1620
### Added

internal/commands/account/show.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
88
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
9+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
910
"github.com/jedib0t/go-pretty/v6/text"
1011
)
1112

@@ -28,14 +29,22 @@ func (s *showCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
2829
return nil, err
2930
}
3031

32+
detailsAccount, err := svc.GetAccountDetails(exec.Context(), &request.GetAccountDetailsRequest{Username: account.UserName})
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
creditsFormatter := getFormatCredits(detailsAccount.Currency)
38+
3139
details := output.Details{
3240
Sections: []output.DetailSection{
3341
{
3442
Rows: []output.DetailRow{
3543
{Title: "Username:", Key: "username", Value: account.UserName},
36-
{Title: "Credits:", Key: "credits", Value: account.Credits, Format: formatCredits},
44+
{Title: "Credits:", Key: "credits", Value: account.Credits, Format: creditsFormatter},
3745
},
3846
},
47+
3948
{
4049
Title: "Resource Limits:", Key: "resource_limits", Rows: []output.DetailRow{
4150
{
@@ -119,16 +128,18 @@ func (s *showCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
119128
}, nil
120129
}
121130

122-
func formatCredits(val any) (text.Colors, string, error) {
123-
credits, ok := val.(float64)
124-
if !ok {
125-
return nil, "", fmt.Errorf("cannot parse %T, expected float64", val)
126-
}
131+
func getFormatCredits(currency string) func(val any) (text.Colors, string, error) {
132+
return func(val any) (text.Colors, string, error) {
133+
credits, ok := val.(float64)
134+
if !ok {
135+
return nil, "", fmt.Errorf("cannot parse %T, expected float64", val)
136+
}
127137

128-
if math.Abs(credits) < 0.001 {
129-
return nil, "Denied", nil
130-
}
138+
if math.Abs(credits) < 0.001 {
139+
return nil, "Denied", nil
140+
}
131141

132-
// Format does not follow european standards, but this is in sync with UI
133-
return nil, fmt.Sprintf("€%.2f", credits/100), nil
142+
// Credits are returned in cents
143+
return nil, fmt.Sprintf("%.2f %s", credits/100, currency), nil
144+
}
134145
}

internal/commands/account/show_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1414
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/mock"
1516
)
1617

1718
func TestShowCommand(t *testing.T) {
@@ -39,7 +40,7 @@ func TestShowCommand(t *testing.T) {
3940

4041
expected := `
4142
Username: upctl_test
42-
Credits: 123.45
43+
Credits: 123.45 EUR
4344
4445
Resource Limits:
4546
Cores: 100
@@ -64,6 +65,10 @@ func TestShowCommand(t *testing.T) {
6465
mService := new(smock.Service)
6566

6667
mService.On("GetAccount").Return(&account, nil)
68+
mService.On("GetAccountDetails", mock.Anything).Return(&upcloud.AccountDetails{
69+
Currency: "EUR",
70+
}, nil)
71+
6772
// force human output
6873
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
6974

@@ -73,4 +78,5 @@ func TestShowCommand(t *testing.T) {
7378
assert.NoError(t, err)
7479
assert.Equal(t, expected, output)
7580
mService.AssertNumberOfCalls(t, "GetAccount", 1)
81+
mService.AssertNumberOfCalls(t, "GetAccountDetails", 1)
7682
}

0 commit comments

Comments
 (0)