44 "encoding/json"
55 "fmt"
66
7+ "github.com/olekukonko/tablewriter"
78 "github.com/spf13/cobra"
9+ "github.com/spf13/viper"
810 "go.uber.org/zap"
911 "gopkg.in/yaml.v3"
1012
@@ -13,7 +15,11 @@ import (
1315)
1416
1517func buildConfigShowCmd () * cobra.Command {
16- return & cobra.Command {
18+ var output string
19+ var noDefaults bool
20+ var withEnv bool
21+
22+ cmd := & cobra.Command {
1723 Use : "show" ,
1824 Short : "Show current configuration" ,
1925 Long : `Display the current CLI configuration settings` ,
@@ -26,16 +32,55 @@ func buildConfigShowCmd() *cobra.Command {
2632 return fmt .Errorf ("failed to load config: %w" , err )
2733 }
2834
29- switch cfg .Output {
35+ // Use flag value if provided, otherwise use config value
36+ outputFormat := cfg .Output
37+ if cmd .Flags ().Changed ("output" ) {
38+ outputFormat = output
39+ }
40+
41+ configFile , err := cfg .EnsureConfigDir ()
42+ if err != nil {
43+ return err
44+ }
45+
46+ // a new viper, free from env and cli flags
47+ v := viper .New ()
48+ v .SetConfigFile (configFile )
49+ if withEnv {
50+ config .ApplyEnvOverrides (v )
51+ }
52+ if ! noDefaults {
53+ config .ApplyDefaults (v )
54+ }
55+ if err := config .ReadInConfig (v ); err != nil {
56+ return err
57+ }
58+
59+ cfgOut , err := config .ForOutputFromViper (v )
60+ if err != nil {
61+ return err
62+ }
63+
64+ if * cfgOut .ConfigDir == config .GetDefaultConfigDir () {
65+ cfgOut .ConfigDir = nil
66+ }
67+
68+ switch outputFormat {
3069 case "json" :
31- return outputJSON (cfg , cmd )
70+ return outputJSON (cfgOut , cmd )
3271 case "yaml" :
33- return outputYAML (cfg , cmd )
72+ return outputYAML (cfgOut , cmd )
3473 default :
35- return outputTable (cfg , cmd )
74+ return outputTable (cfgOut , cmd )
3675 }
3776 },
3877 }
78+
79+ cmd .Flags ().VarP ((* outputFlag )(& output ), "output" , "o" , "output format (json, yaml, table)" )
80+ cmd .Flags ().BoolVar (& noDefaults , "no-defaults" , false , "do not show default values for unset fields" )
81+ cmd .Flags ().BoolVar (& withEnv , "with-env" , false , "apply environment variable overrides" )
82+
83+ return cmd
3984}
4085
4186func buildConfigSetCmd () * cobra.Command {
@@ -132,69 +177,56 @@ func buildConfigCmd() *cobra.Command {
132177 return cmd
133178}
134179
135- func outputTable (cfg * config.Config , cmd * cobra.Command ) error {
136- out := cmd .OutOrStdout ()
137- fmt .Fprintln (out , "Current Configuration:" )
138- fmt .Fprintf (out , " api_url: %s\n " , cfg .APIURL )
139- fmt .Fprintf (out , " console_url: %s\n " , cfg .ConsoleURL )
140- fmt .Fprintf (out , " gateway_url: %s\n " , cfg .GatewayURL )
141- fmt .Fprintf (out , " docs_mcp: %t\n " , cfg .DocsMCP )
142- fmt .Fprintf (out , " docs_mcp_url: %s\n " , cfg .DocsMCPURL )
143- fmt .Fprintf (out , " project_id: %s\n " , valueOrEmpty (cfg .ProjectID ))
144- fmt .Fprintf (out , " service_id: %s\n " , valueOrEmpty (cfg .ServiceID ))
145- fmt .Fprintf (out , " output: %s\n " , cfg .Output )
146- fmt .Fprintf (out , " analytics: %t\n " , cfg .Analytics )
147- fmt .Fprintf (out , " password_storage: %s\n " , cfg .PasswordStorage )
148- fmt .Fprintf (out , " debug: %t\n " , cfg .Debug )
149- fmt .Fprintf (out , " config_dir: %s\n " , cfg .ConfigDir )
150- return nil
151- }
152-
153- func outputJSON (cfg * config.Config , cmd * cobra.Command ) error {
154- data := map [string ]interface {}{
155- "api_url" : cfg .APIURL ,
156- "console_url" : cfg .ConsoleURL ,
157- "gateway_url" : cfg .GatewayURL ,
158- "docs_mcp" : cfg .DocsMCP ,
159- "docs_mcp_url" : cfg .DocsMCPURL ,
160- "project_id" : cfg .ProjectID ,
161- "service_id" : cfg .ServiceID ,
162- "output" : cfg .Output ,
163- "analytics" : cfg .Analytics ,
164- "password_storage" : cfg .PasswordStorage ,
165- "debug" : cfg .Debug ,
166- "config_dir" : cfg .ConfigDir ,
180+ func outputTable (cfg * config.ConfigOutput , cmd * cobra.Command ) error {
181+ table := tablewriter .NewWriter (cmd .OutOrStdout ())
182+ table .Header ("PROPERTY" , "VALUE" )
183+ if cfg .APIURL != nil {
184+ table .Append ("api_url" , * cfg .APIURL )
185+ }
186+ if cfg .ConsoleURL != nil {
187+ table .Append ("console_url" , * cfg .ConsoleURL )
188+ }
189+ if cfg .GatewayURL != nil {
190+ table .Append ("gateway_url" , * cfg .GatewayURL )
191+ }
192+ if cfg .DocsMCP != nil {
193+ table .Append ("docs_mcp" , fmt .Sprintf ("%t" , * cfg .DocsMCP ))
194+ }
195+ if cfg .DocsMCPURL != nil {
196+ table .Append ("docs_mcp_url" , * cfg .DocsMCPURL )
197+ }
198+ if cfg .ProjectID != nil {
199+ table .Append ("project_id" , * cfg .ProjectID )
200+ }
201+ if cfg .ServiceID != nil {
202+ table .Append ("service_id" , * cfg .ServiceID )
203+ }
204+ if cfg .Output != nil {
205+ table .Append ("output" , * cfg .Output )
206+ }
207+ if cfg .Analytics != nil {
208+ table .Append ("analytics" , fmt .Sprintf ("%t" , * cfg .Analytics ))
167209 }
210+ if cfg .PasswordStorage != nil {
211+ table .Append ("password_storage" , * cfg .PasswordStorage )
212+ }
213+ if cfg .Debug != nil {
214+ table .Append ("debug" , fmt .Sprintf ("%t" , * cfg .Debug ))
215+ }
216+ if cfg .ConfigDir != nil {
217+ table .Append ("config_dir" , * cfg .ConfigDir )
218+ }
219+ return table .Render ()
220+ }
168221
222+ func outputJSON (cfg * config.ConfigOutput , cmd * cobra.Command ) error {
169223 encoder := json .NewEncoder (cmd .OutOrStdout ())
170224 encoder .SetIndent ("" , " " )
171- return encoder .Encode (data )
225+ return encoder .Encode (cfg )
172226}
173227
174- func outputYAML (cfg * config.Config , cmd * cobra.Command ) error {
175- data := map [string ]interface {}{
176- "api_url" : cfg .APIURL ,
177- "console_url" : cfg .ConsoleURL ,
178- "gateway_url" : cfg .GatewayURL ,
179- "docs_mcp" : cfg .DocsMCP ,
180- "docs_mcp_url" : cfg .DocsMCPURL ,
181- "project_id" : cfg .ProjectID ,
182- "service_id" : cfg .ServiceID ,
183- "output" : cfg .Output ,
184- "analytics" : cfg .Analytics ,
185- "password_storage" : cfg .PasswordStorage ,
186- "debug" : cfg .Debug ,
187- "config_dir" : cfg .ConfigDir ,
188- }
189-
228+ func outputYAML (cfg * config.ConfigOutput , cmd * cobra.Command ) error {
190229 encoder := yaml .NewEncoder (cmd .OutOrStdout ())
191230 defer encoder .Close ()
192- return encoder .Encode (data )
193- }
194-
195- func valueOrEmpty (s string ) string {
196- if s == "" {
197- return "(not set)"
198- }
199- return s
231+ return encoder .Encode (cfg )
200232}
0 commit comments