Skip to content

Commit a894a5d

Browse files
authored
Merge pull request #78 from latitudesh/fix/PD-5252/json-output-flags
fix: JSON output flags (--json, -o json)
2 parents ac8248f + b58ed37 commit a894a5d

5 files changed

Lines changed: 53 additions & 26 deletions

File tree

cli/new_plans_list.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ func newPlansAvailabilityCmd() *cobra.Command {
178178
// --- Grouped Plans (for plans list command) ---
179179

180180
type groupedPlan struct {
181-
Slug string
182-
CPU string
183-
Drives string
184-
NIC string
185-
ID string
186-
AvailableIn []string
187-
InStock []string
188-
Features []string
189-
Memory string
181+
Slug string `json:"slug"`
182+
CPU string `json:"cpu"`
183+
Drives string `json:"drives"`
184+
NIC string `json:"nic"`
185+
ID string `json:"id"`
186+
AvailableIn []string `json:"available_in"`
187+
InStock []string `json:"in_stock"`
188+
Features []string `json:"features"`
189+
Memory string `json:"memory"`
190190
}
191191

192192
func groupPlans(pr *plansResponse, gpu bool, name, slug string, inStock bool, location, stockLevel string, diskEql, diskGte, diskLte, ramEql, ramGte, ramLte int) []groupedPlan {
@@ -350,6 +350,12 @@ func renderGroupedPlans(plans []groupedPlan) {
350350
return
351351
}
352352

353+
// Check if JSON output was requested
354+
if viper.GetBool("json") || viper.GetString("output") == "json" {
355+
renderGroupedPlansJSON(plans)
356+
return
357+
}
358+
353359
if os.Getenv("LSH_CLASSIC_OUTPUT") == "true" {
354360
renderGroupedPlansClassic(plans)
355361
return
@@ -451,6 +457,15 @@ func wrapLocationsSmartLimit(locs []string, maxDisplay int) string {
451457
return fmt.Sprintf("%s, +%d", displayed, remaining)
452458
}
453459

460+
func renderGroupedPlansJSON(plans []groupedPlan) {
461+
jsonData, err := json.MarshalIndent(plans, "", " ")
462+
if err != nil {
463+
fmt.Println("Could not encode plans as JSON.")
464+
return
465+
}
466+
fmt.Println(string(jsonData))
467+
}
468+
454469
func renderGroupedPlansClassic(plans []groupedPlan) {
455470
table := tablewriter.NewWriter(os.Stdout)
456471
table.SetHeader([]string{"SLUG", "CPU", "DRIVES", "NIC", "ID", "FEATURES", "MEMORY", "AVAILABLE IN", "IN STOCK"})

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ require (
8181
go.uber.org/multierr v1.9.0 // indirect
8282
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
8383
golang.org/x/sync v0.11.0 // indirect
84-
golang.org/x/sys v0.36.0 // indirect
84+
golang.org/x/sys v0.38.0 // indirect
85+
golang.org/x/term v0.37.0 // indirect
8586
gopkg.in/ini.v1 v1.67.0 // indirect
8687
gopkg.in/yaml.v3 v3.0.1 // indirect
8788
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,12 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
285285
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
286286
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
287287
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
288+
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
289+
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
288290
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
289291
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
292+
golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU=
293+
golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254=
290294
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
291295
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
292296
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

internal/renderer/json.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
8-
"github.com/go-openapi/swag"
97
)
108

119
type JSONRenderer struct{}
1210

1311
func (jr JSONRenderer) Render(data []ResponseData) {
14-
if !swag.IsZero(data) {
15-
JSONString, err := json.Marshal(data)
16-
if err != nil {
17-
fmt.Println("Could not decode the result as JSON.")
18-
}
12+
if len(data) == 0 {
13+
return
14+
}
1915

20-
var prettyJSON bytes.Buffer
21-
if err := json.Indent(&prettyJSON, JSONString, "", " "); err != nil {
22-
fmt.Println("JSON format error")
23-
}
16+
JSONString, err := json.Marshal(data)
17+
if err != nil {
18+
fmt.Println("Could not decode the result as JSON.")
19+
return
20+
}
2421

25-
fmt.Println(prettyJSON.String())
22+
var prettyJSON bytes.Buffer
23+
if err := json.Indent(&prettyJSON, JSONString, "", " "); err != nil {
24+
fmt.Println("JSON format error")
25+
return
2626
}
27+
28+
fmt.Println(prettyJSON.String())
2729
}

internal/renderer/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
outputTable "github.com/latitudesh/lsh/internal/output/table"
77
"github.com/spf13/viper"
8+
"golang.org/x/term"
89
)
910

1011
type ResponseData interface {
@@ -22,10 +23,14 @@ func GetRenderer() Renderer {
2223
return TableRenderer{} // Old ASCII
2324
}
2425

25-
// Check if JSON was requested
26-
if viper.GetBool("json") {
27-
// You can create a JSONRenderer later
28-
return TableRenderer{} // fallback
26+
// Check if JSON was requested via --json flag or -o json
27+
if viper.GetBool("json") || viper.GetString("output") == "json" {
28+
return JSONRenderer{}
29+
}
30+
31+
// If stdout is not a terminal (e.g., pipe), use table output
32+
if !term.IsTerminal(int(os.Stdout.Fd())) {
33+
return TableRenderer{}
2934
}
3035

3136
// Default: use interactive Bubble Tea

0 commit comments

Comments
 (0)