Skip to content

Commit

Permalink
chore(config): beautify the config list format
Browse files Browse the repository at this point in the history
  • Loading branch information
duanhongyi committed Sep 14, 2024
1 parent 7025b48 commit 081662b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 55 deletions.
34 changes: 9 additions & 25 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,15 @@ func (d *DryccCmd) ConfigList(appID string, ptype string) error {
return err
}

configValues := config.Values
if ptype != "" {
configValues = config.TypedValues[ptype]
}

keys := *sortKeys(configValues)
table := d.getDefaultFormatTable([]string{"NAME", "VALUE"})

for _, key := range keys {
table.Append([]string{
key,
fmt.Sprintf("%v", configValues[key]),
})
}

// common config
if ptype != "" {
table.Append([]string{"--- Common Config:"})
configValues = config.Values
keys = *sortKeys(configValues)
for _, key := range keys {
table.Append([]string{
key,
fmt.Sprintf("%v", configValues[key]),
})
table := d.getDefaultFormatTable([]string{"PTYPE", "NAME", "VALUE"})
for _, key := range *sortKeys(config.Values) {
table.Append([]string{"N/A", key, fmt.Sprintf("%v", config.Values[key])})
}
for key, values := range config.TypedValues {
if ptype == "" || ptype == key {
for _, key2 := range *sortKeys(values) {
table.Append([]string{key, key2, fmt.Sprintf("%v", values[key2])})
}
}
}
table.Render()
Expand Down
59 changes: 29 additions & 30 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,23 @@ func TestConfigList(t *testing.T) {
err = cmdr.ConfigList("foo", "")
assert.NoError(t, err)

assert.Equal(t, b.String(), `NAME VALUE
FLOAT 12.34
NCC 1701
TEST testing
TRUE false
assert.Equal(t, b.String(), `PTYPE NAME VALUE
N/A FLOAT 12.34
N/A NCC 1701
N/A TEST testing
N/A TRUE false
web PORT 9000
`, "output")

b.Reset()
err = cmdr.ConfigList("foo", "web")
assert.NoError(t, err)
assert.Equal(t, b.String(), `NAME VALUE
PORT 9000
--- Common Config:
FLOAT 12.34
NCC 1701
TEST testing
TRUE false
assert.Equal(t, b.String(), `PTYPE NAME VALUE
N/A FLOAT 12.34
N/A NCC 1701
N/A TEST testing
N/A TRUE false
web PORT 9000
`, "output")

}
Expand Down Expand Up @@ -194,12 +194,12 @@ func TestConfigSet(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Creating config... done
NAME VALUE
FLOAT 12.34
NCC 1701
SSH_KEY LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0=
TEST testing
TRUE false
PTYPE NAME VALUE
N/A FLOAT 12.34
N/A NCC 1701
N/A SSH_KEY LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0=
N/A TEST testing
N/A TRUE false
`, "output")
}

Expand Down Expand Up @@ -248,11 +248,11 @@ func TestConfigUnset(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Removing config... done
NAME VALUE
FLOAT 12.34
NCC 1701
TEST testing
TRUE false
PTYPE NAME VALUE
N/A FLOAT 12.34
N/A NCC 1701
N/A TEST testing
N/A TRUE false
`, "output")
}

Expand Down Expand Up @@ -308,12 +308,11 @@ func TestConfigUnsetTypedValues(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Removing config... done
NAME VALUE
FLOAT 12.34
NCC 1701
TEST testing
TRUE false
--- Common Config:
RELEASE_VERSION v1
PTYPE NAME VALUE
N/A RELEASE_VERSION v1
web FLOAT 12.34
web NCC 1701
web TEST testing
web TRUE false
`, "output")
}

0 comments on commit 081662b

Please sign in to comment.