Skip to content

Commit f9635e1

Browse files
authored
feat(cmd): allow tables to be output as json (#465)
* feat(cmd): allow tables to be output as json * chore(cmd): don't massage json response data before printing
1 parent fa85edc commit f9635e1

File tree

11 files changed

+223
-123
lines changed

11 files changed

+223
-123
lines changed

cmd/lk/app.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ var (
7272
Hidden: true,
7373
},
7474
&cli.BoolFlag{
75-
Name: "install",
76-
Usage: "Run installation tasks after creating the app",
77-
Hidden: true,
75+
Name: "install",
76+
Aliases: []string{"i"},
77+
Usage: "Run installation tasks after creating the app",
78+
Hidden: true,
7879
},
7980
},
8081
},

cmd/lk/cloud.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ var (
7272
Action: handleAuth,
7373
Flags: []cli.Flag{
7474
&cli.BoolFlag{
75-
Name: "R",
76-
Aliases: []string{"revoke"},
75+
Name: "revoke",
76+
Aliases: []string{"R"},
7777
Destination: &revoke,
7878
},
7979
&cli.IntFlag{
80-
Name: "t",
81-
Aliases: []string{"timeout"},
80+
Name: "timeout",
81+
Aliases: []string{"t"},
8282
Usage: "Number of `SECONDS` to attempt authentication before giving up",
8383
Destination: &timeout,
8484
Value: 60 * 15,
8585
},
8686
&cli.IntFlag{
87-
Name: "i",
88-
Aliases: []string{"poll-interval"},
87+
Name: "poll-interval",
88+
Aliases: []string{"i"},
8989
Usage: "Number of `SECONDS` between poll requests to verify authentication",
9090
Destination: &interval,
9191
Value: 4,

cmd/lk/egress.go

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ var (
103103
Usage: "Limits list to a certain room `NAME`",
104104
},
105105
&cli.BoolFlag{
106-
Name: "active",
107-
Usage: "Lists only active egresses",
106+
Name: "active",
107+
Aliases: []string{"a"},
108+
Usage: "Lists only active egresses",
108109
},
110+
jsonFlag,
109111
},
110112
},
111113
{
@@ -595,48 +597,52 @@ func listEgress(ctx context.Context, cmd *cli.Command) error {
595597
items = res.Items
596598
}
597599

598-
table := CreateTable().
599-
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
600-
for _, item := range items {
601-
var startedAt string
602-
if item.StartedAt != 0 {
603-
startedAt = fmt.Sprint(time.Unix(0, item.StartedAt))
604-
}
605-
var egressType, egressSource string
606-
switch req := item.Request.(type) {
607-
case *livekit.EgressInfo_RoomComposite:
608-
egressType = "room_composite"
609-
egressSource = req.RoomComposite.RoomName
610-
case *livekit.EgressInfo_Web:
611-
egressType = "web"
612-
egressSource = req.Web.Url
613-
case *livekit.EgressInfo_Participant:
614-
egressType = "participant"
615-
egressSource = fmt.Sprintf("%s/%s", req.Participant.RoomName, req.Participant.Identity)
616-
case *livekit.EgressInfo_TrackComposite:
617-
egressType = "track_composite"
618-
trackIDs := make([]string, 0)
619-
if req.TrackComposite.VideoTrackId != "" {
620-
trackIDs = append(trackIDs, req.TrackComposite.VideoTrackId)
600+
if cmd.Bool("json") {
601+
PrintJSON(items)
602+
} else {
603+
table := CreateTable().
604+
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
605+
for _, item := range items {
606+
var startedAt string
607+
if item.StartedAt != 0 {
608+
startedAt = fmt.Sprint(time.Unix(0, item.StartedAt))
621609
}
622-
if req.TrackComposite.AudioTrackId != "" {
623-
trackIDs = append(trackIDs, req.TrackComposite.AudioTrackId)
610+
var egressType, egressSource string
611+
switch req := item.Request.(type) {
612+
case *livekit.EgressInfo_RoomComposite:
613+
egressType = "room_composite"
614+
egressSource = req.RoomComposite.RoomName
615+
case *livekit.EgressInfo_Web:
616+
egressType = "web"
617+
egressSource = req.Web.Url
618+
case *livekit.EgressInfo_Participant:
619+
egressType = "participant"
620+
egressSource = fmt.Sprintf("%s/%s", req.Participant.RoomName, req.Participant.Identity)
621+
case *livekit.EgressInfo_TrackComposite:
622+
egressType = "track_composite"
623+
trackIDs := make([]string, 0)
624+
if req.TrackComposite.VideoTrackId != "" {
625+
trackIDs = append(trackIDs, req.TrackComposite.VideoTrackId)
626+
}
627+
if req.TrackComposite.AudioTrackId != "" {
628+
trackIDs = append(trackIDs, req.TrackComposite.AudioTrackId)
629+
}
630+
egressSource = fmt.Sprintf("%s/%s", req.TrackComposite.RoomName, strings.Join(trackIDs, ","))
631+
case *livekit.EgressInfo_Track:
632+
egressType = "track"
633+
egressSource = fmt.Sprintf("%s/%s", req.Track.RoomName, req.Track.TrackId)
624634
}
625-
egressSource = fmt.Sprintf("%s/%s", req.TrackComposite.RoomName, strings.Join(trackIDs, ","))
626-
case *livekit.EgressInfo_Track:
627-
egressType = "track"
628-
egressSource = fmt.Sprintf("%s/%s", req.Track.RoomName, req.Track.TrackId)
635+
table.Row(
636+
item.EgressId,
637+
item.Status.String(),
638+
egressType,
639+
egressSource,
640+
startedAt,
641+
item.Error,
642+
)
629643
}
630-
table.Row(
631-
item.EgressId,
632-
item.Status.String(),
633-
egressType,
634-
egressSource,
635-
startedAt,
636-
item.Error,
637-
)
638-
}
639-
fmt.Println(table)
644+
fmt.Println(table)
645+
}
640646

641647
return nil
642648
}

cmd/lk/ingress.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var (
8282
Usage: "List a specific ingress `ID`",
8383
Required: false,
8484
},
85+
jsonFlag,
8586
},
8687
},
8788
{
@@ -223,33 +224,36 @@ func listIngress(ctx context.Context, cmd *cli.Command) error {
223224
return err
224225
}
225226

226-
table := CreateTable().
227-
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
228-
for _, item := range res.Items {
229-
if item == nil {
230-
continue
231-
}
227+
// NOTE: previously, the `verbose` flag was used to output JSON in addition to the table.
228+
// This is inconsistent with other commands in which verbose is used for debug info, but is
229+
// kept for compatibility with the previous behavior.
230+
if cmd.Bool("verbose") || cmd.Bool("json") {
231+
PrintJSON(res)
232+
} else {
233+
table := CreateTable().
234+
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
235+
for _, item := range res.Items {
236+
if item == nil {
237+
continue
238+
}
232239

233-
var status, errorStr string
234-
if item.State != nil {
235-
status = item.State.Status.String()
236-
errorStr = item.State.Error
237-
}
240+
var status, errorStr string
241+
if item.State != nil {
242+
status = item.State.Status.String()
243+
errorStr = item.State.Error
244+
}
238245

239-
table.Row(
240-
item.IngressId,
241-
item.Name,
242-
item.RoomName,
243-
item.StreamKey,
244-
item.Url,
245-
status,
246-
errorStr,
247-
)
248-
}
249-
fmt.Println(table)
250-
251-
if cmd.Bool("verbose") {
252-
PrintJSON(res)
246+
table.Row(
247+
item.IngressId,
248+
item.Name,
249+
item.RoomName,
250+
item.StreamKey,
251+
item.Url,
252+
status,
253+
errorStr,
254+
)
255+
}
256+
fmt.Println(table)
253257
}
254258

255259
return nil

cmd/lk/project.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var (
6767
Usage: "List all configured projects",
6868
UsageText: "lk project list",
6969
Action: listProjects,
70+
Flags: []cli.Flag{jsonFlag},
7071
},
7172
{
7273
Name: "remove",
@@ -247,22 +248,26 @@ func listProjects(ctx context.Context, cmd *cli.Command) error {
247248
headerStyle := baseStyle.Bold(true)
248249
selectedStyle := theme.Focused.Title.Padding(0, 1)
249250

250-
table := CreateTable().
251-
StyleFunc(func(row, col int) lipgloss.Style {
252-
switch {
253-
case row == table.HeaderRow:
254-
return headerStyle
255-
case cliConfig.Projects[row].Name == cliConfig.DefaultProject:
256-
return selectedStyle
257-
default:
258-
return baseStyle
259-
}
260-
}).
261-
Headers("Name", "URL", "API Key", "Default")
262-
for _, p := range cliConfig.Projects {
263-
table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
251+
if cmd.Bool("json") {
252+
PrintJSON(cliConfig.Projects)
253+
} else {
254+
table := CreateTable().
255+
StyleFunc(func(row, col int) lipgloss.Style {
256+
switch {
257+
case row == table.HeaderRow:
258+
return headerStyle
259+
case cliConfig.Projects[row].Name == cliConfig.DefaultProject:
260+
return selectedStyle
261+
default:
262+
return baseStyle
263+
}
264+
}).
265+
Headers("Name", "URL", "API Key", "Default")
266+
for _, p := range cliConfig.Projects {
267+
table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
268+
}
269+
fmt.Println(table)
264270
}
265-
fmt.Println(table)
266271

267272
return nil
268273
}

cmd/lk/proto.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,22 @@ func listAndPrint[
191191
return err
192192
}
193193

194-
table := CreateTable().
195-
Headers(header...)
196-
for _, item := range res.GetItems() {
197-
if item == nil {
198-
continue
199-
}
200-
row := tableRow(item)
201-
if len(row) == 0 {
202-
continue
203-
}
204-
table.Row(row...)
205-
}
206-
fmt.Println(table)
207-
208-
if cmd.Bool("verbose") {
194+
if cmd.Bool("json") {
209195
PrintJSON(res)
196+
} else {
197+
table := CreateTable().
198+
Headers(header...)
199+
for _, item := range res.GetItems() {
200+
if item == nil {
201+
continue
202+
}
203+
row := tableRow(item)
204+
if len(row) == 0 {
205+
continue
206+
}
207+
table.Row(row...)
208+
}
209+
fmt.Println(table)
210210
}
211211

212212
return nil

cmd/lk/replay.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
Name: "list",
2828
Before: createReplayClient,
2929
Action: listReplays,
30+
Flags: []cli.Flag{jsonFlag},
3031
},
3132
{
3233
Name: "load",
@@ -114,7 +115,7 @@ func createReplayClient(ctx context.Context, cmd *cli.Command) error {
114115
return nil
115116
}
116117

117-
func listReplays(ctx context.Context, _ *cli.Command) error {
118+
func listReplays(ctx context.Context, cmd *cli.Command) error {
118119
ctx, err := replayClient.withAuth(ctx)
119120
if err != nil {
120121
return err
@@ -126,11 +127,15 @@ func listReplays(ctx context.Context, _ *cli.Command) error {
126127
return err
127128
}
128129

129-
table := CreateTable().Headers("ReplayID")
130-
for _, info := range res.Replays {
131-
table.Row(info.ReplayId)
130+
if cmd.Bool("json") {
131+
PrintJSON(res.Replays)
132+
} else {
133+
table := CreateTable().Headers("ReplayID")
134+
for _, info := range res.Replays {
135+
table.Row(info.ReplayId)
136+
}
137+
fmt.Println(table)
132138
}
133-
fmt.Println(table)
134139

135140
return nil
136141
}

cmd/lk/room.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var (
109109
Before: createRoomClient,
110110
Action: listRooms,
111111
ArgsUsage: "[ROOM_NAME ...]",
112+
Flags: []cli.Flag{jsonFlag},
112113
},
113114
{
114115
Name: "update",
@@ -641,6 +642,13 @@ func createRoom(ctx context.Context, cmd *cli.Command) error {
641642

642643
func listRooms(ctx context.Context, cmd *cli.Command) error {
643644
names, _ := extractArgs(cmd)
645+
if cmd.Bool("verbose") && len(names) > 0 {
646+
fmt.Printf(
647+
"Querying rooms matching %s",
648+
strings.Join(mapStrings(names, wrapWith("\"")), ", "),
649+
)
650+
}
651+
644652
req := livekit.ListRoomsRequest{}
645653
if len(names) > 0 {
646654
req.Names = names
@@ -650,19 +658,22 @@ func listRooms(ctx context.Context, cmd *cli.Command) error {
650658
if err != nil {
651659
return err
652660
}
653-
if len(res.Rooms) == 0 {
654-
if len(names) > 0 {
655-
fmt.Printf(
656-
"there are no rooms matching %s",
657-
strings.Join(mapStrings(names, wrapWith("\"")), ", "),
661+
662+
if cmd.Bool("json") {
663+
PrintJSON(res)
664+
} else {
665+
table := CreateTable().Headers("RoomID", "Name", "Participants", "Publishers")
666+
for _, rm := range res.Rooms {
667+
table.Row(
668+
rm.Sid,
669+
rm.Name,
670+
fmt.Sprintf("%d", rm.NumParticipants),
671+
fmt.Sprintf("%d", rm.NumPublishers),
658672
)
659-
} else {
660-
fmt.Println("there are no active rooms")
661673
}
674+
fmt.Println(table)
662675
}
663-
for _, rm := range res.Rooms {
664-
fmt.Printf("%s\t%s\t%d participants\n", rm.Sid, rm.Name, rm.NumParticipants)
665-
}
676+
666677
return nil
667678
}
668679

0 commit comments

Comments
 (0)