Skip to content

Commit 34d54f6

Browse files
authored
fix(rdb): instance list human formating (#1214)
1 parent 4799cd0 commit 34d54f6

File tree

6 files changed

+607
-6
lines changed

6 files changed

+607
-6
lines changed

internal/human/marshal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
264264
opt.Fields = getDefaultFieldsOpt(itemType)
265265
}
266266

267+
subOpts := &MarshalOpt{TableCell: true}
268+
267269
// Validate that all field exist
268270
for _, f := range opt.Fields {
269271
_, err := gofields.GetType(itemType, f.FieldName)
@@ -304,7 +306,7 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
304306
case fieldValue.Type().Kind() == reflect.Slice:
305307
str, err = marshalInlineSlice(fieldValue)
306308
default:
307-
str, err = Marshal(fieldValue.Interface(), opt)
309+
str, err = Marshal(fieldValue.Interface(), subOpts)
308310
}
309311
if err != nil {
310312
return "", err

internal/human/specs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ type MarshalOpt struct {
1111
Title string
1212
Fields []*MarshalFieldOpt
1313
Sections []*MarshalSection
14+
15+
// Is set to true if we are marshaling a table cell
16+
TableCell bool
1417
}
1518

1619
type MarshalFieldOpt struct {

internal/namespaces/rdb/v1/custom_instance.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func instanceMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error)
7474
}
7575

7676
func backupScheduleMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
77+
backupSchedule := i.(rdb.BackupSchedule)
78+
79+
if opt.TableCell {
80+
if backupSchedule.Disabled {
81+
return "Disabled", nil
82+
}
83+
return "Enabled", nil
84+
}
85+
7786
// To avoid recursion of human.Marshal we create a dummy type
7887
type LocalBackupSchedule rdb.BackupSchedule
7988
type tmp struct {
@@ -82,17 +91,17 @@ func backupScheduleMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string,
8291
Retention *scw.Duration `json:"retention"`
8392
}
8493

85-
backupSchedule := tmp{
86-
LocalBackupSchedule: LocalBackupSchedule(i.(rdb.BackupSchedule)),
94+
localBackupSchedule := tmp{
95+
LocalBackupSchedule: LocalBackupSchedule(backupSchedule),
8796
Frequency: &scw.Duration{
88-
Seconds: int64(i.(rdb.BackupSchedule).Frequency) * 3600,
97+
Seconds: int64(backupSchedule.Frequency) * 3600,
8998
},
9099
Retention: &scw.Duration{
91-
Seconds: int64(i.(rdb.BackupSchedule).Retention) * 24 * 3600,
100+
Seconds: int64(backupSchedule.Retention) * 24 * 3600,
92101
},
93102
}
94103

95-
str, err := human.Marshal(backupSchedule, opt)
104+
str, err := human.Marshal(localBackupSchedule, opt)
96105
if err != nil {
97106
return "", err
98107
}

internal/namespaces/rdb/v1/custom_instance_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77
"github.com/scaleway/scaleway-cli/internal/core"
88
)
99

10+
func Test_ListInstance(t *testing.T) {
11+
t.Run("Simple", core.Test(&core.TestConfig{
12+
Commands: GetCommands(),
13+
BeforeFunc: createInstance("PostgreSQL-12"),
14+
Cmd: "scw rdb instance list",
15+
Check: core.TestCheckGolden(),
16+
AfterFunc: deleteInstance(),
17+
}))
18+
}
19+
1020
func Test_CloneInstance(t *testing.T) {
1121
t.Run("Simple", core.Test(&core.TestConfig{
1222
Commands: GetCommands(),

0 commit comments

Comments
 (0)