From 081662b5b2a17d119f6592e8ab0886bcf131b05a Mon Sep 17 00:00:00 2001 From: duanhongyi Date: Sat, 14 Sep 2024 17:07:59 +0800 Subject: [PATCH] chore(config): beautify the config list format --- cmd/config.go | 34 +++++++------------------- cmd/config_test.go | 59 +++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 55 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index a486495b..8428f189 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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() diff --git a/cmd/config_test.go b/cmd/config_test.go index 760d3501..5280a487 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -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") } @@ -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") } @@ -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") } @@ -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") }