@@ -11,20 +11,25 @@ import (
1111)
1212
1313var (
14+ outputJSON bool
15+
1416 outputCmd = & cobra.Command {
1517 Use : "output <stack> [component] [key]" ,
1618 Short : "Show output values from components" ,
1719 Long : `Show output values from components.
1820
1921If only stack is provided, shows outputs from all components.
2022If stack and component are provided, shows outputs from that component.
21- If stack, component, and key are provided, shows only that specific output value.` ,
23+ If stack, component, and key are provided, shows only that specific output value.
24+
25+ The --json flag formats the output as JSON, which can be piped to tools like jq.` ,
2226 Run : output ,
2327 Args : cobra .RangeArgs (1 , 3 ),
2428 }
2529)
2630
2731func init () {
32+ outputCmd .Flags ().BoolVar (& outputJSON , "json" , false , "Output in JSON format" )
2833 rootCmd .AddCommand (outputCmd )
2934}
3035
@@ -43,30 +48,66 @@ func output(cmd *cobra.Command, args []string) {
4348 log .Fatal (err )
4449 }
4550
51+ // JSON output mode
52+ if outputJSON {
53+ // If a specific key is requested, output only that value as JSON
54+ if keyFilter != "" {
55+ if v , ok := out [keyFilter ]; ok {
56+ var rawValue interface {}
57+ if err := json .Unmarshal (v .Value , & rawValue ); err == nil {
58+ jsonBytes , _ := json .MarshalIndent (rawValue , "" , " " )
59+ fmt .Println (string (jsonBytes ))
60+ } else {
61+ // Fallback to string
62+ jsonBytes , _ := json .MarshalIndent (v .String (), "" , " " )
63+ fmt .Println (string (jsonBytes ))
64+ }
65+ } else {
66+ log .Fatal (fmt .Errorf ("output key '%s' not found in component '%s'" , keyFilter , component .Name ))
67+ }
68+ return
69+ }
70+
71+ // Output all values as JSON object
72+ result := make (map [string ]interface {})
73+ for k , v := range out {
74+ var rawValue interface {}
75+ if err := json .Unmarshal (v .Value , & rawValue ); err == nil {
76+ result [k ] = rawValue
77+ } else {
78+ result [k ] = v .String ()
79+ }
80+ }
81+ jsonBytes , _ := json .MarshalIndent (result , "" , " " )
82+ fmt .Println (string (jsonBytes ))
83+ return
84+ }
85+
86+ // Plain text output mode
4687 // If a specific key is requested, only show that
4788 if keyFilter != "" {
4889 if v , ok := out [keyFilter ]; ok {
4990 var rawValue interface {}
5091 if err := json .Unmarshal (v .Value , & rawValue ); err == nil {
5192 switch val := rawValue .(type ) {
5293 case string :
53- fmt .Printf ( " \" %s \" \n " , val )
94+ fmt .Println ( val )
5495 case []interface {}, map [string ]interface {}:
5596 jsonBytes , _ := json .Marshal (val )
56- fmt .Printf ( "%s \n " , string (jsonBytes ))
97+ fmt .Println ( string (jsonBytes ))
5798 default :
5899 fmt .Printf ("%v\n " , val )
59100 }
60101 } else {
61- fmt .Printf ( " \" %s \" \n " , v .String ())
102+ fmt .Println ( v .String ())
62103 }
63104 } else {
64105 log .Fatal (fmt .Errorf ("output key '%s' not found in component '%s'" , keyFilter , component .Name ))
65106 }
66107 return
67108 }
68109
69- // Show all outputs
110+ // Show all outputs in human-readable format
70111 for k , v := range out {
71112 // Try to unmarshal to detect the actual type
72113 var rawValue interface {}
0 commit comments