Skip to content

Commit

Permalink
feat(cmd): allow tables to be output as json (#465)
Browse files Browse the repository at this point in the history
* feat(cmd): allow tables to be output as json

* chore(cmd): don't massage json response data before printing
  • Loading branch information
rektdeckard authored Nov 13, 2024
1 parent fa85edc commit f9635e1
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 123 deletions.
7 changes: 4 additions & 3 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ var (
Hidden: true,
},
&cli.BoolFlag{
Name: "install",
Usage: "Run installation tasks after creating the app",
Hidden: true,
Name: "install",
Aliases: []string{"i"},
Usage: "Run installation tasks after creating the app",
Hidden: true,
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions cmd/lk/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ var (
Action: handleAuth,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "R",
Aliases: []string{"revoke"},
Name: "revoke",
Aliases: []string{"R"},
Destination: &revoke,
},
&cli.IntFlag{
Name: "t",
Aliases: []string{"timeout"},
Name: "timeout",
Aliases: []string{"t"},
Usage: "Number of `SECONDS` to attempt authentication before giving up",
Destination: &timeout,
Value: 60 * 15,
},
&cli.IntFlag{
Name: "i",
Aliases: []string{"poll-interval"},
Name: "poll-interval",
Aliases: []string{"i"},
Usage: "Number of `SECONDS` between poll requests to verify authentication",
Destination: &interval,
Value: 4,
Expand Down
88 changes: 47 additions & 41 deletions cmd/lk/egress.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ var (
Usage: "Limits list to a certain room `NAME`",
},
&cli.BoolFlag{
Name: "active",
Usage: "Lists only active egresses",
Name: "active",
Aliases: []string{"a"},
Usage: "Lists only active egresses",
},
jsonFlag,
},
},
{
Expand Down Expand Up @@ -595,48 +597,52 @@ func listEgress(ctx context.Context, cmd *cli.Command) error {
items = res.Items
}

table := CreateTable().
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
for _, item := range items {
var startedAt string
if item.StartedAt != 0 {
startedAt = fmt.Sprint(time.Unix(0, item.StartedAt))
}
var egressType, egressSource string
switch req := item.Request.(type) {
case *livekit.EgressInfo_RoomComposite:
egressType = "room_composite"
egressSource = req.RoomComposite.RoomName
case *livekit.EgressInfo_Web:
egressType = "web"
egressSource = req.Web.Url
case *livekit.EgressInfo_Participant:
egressType = "participant"
egressSource = fmt.Sprintf("%s/%s", req.Participant.RoomName, req.Participant.Identity)
case *livekit.EgressInfo_TrackComposite:
egressType = "track_composite"
trackIDs := make([]string, 0)
if req.TrackComposite.VideoTrackId != "" {
trackIDs = append(trackIDs, req.TrackComposite.VideoTrackId)
if cmd.Bool("json") {
PrintJSON(items)
} else {
table := CreateTable().
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
for _, item := range items {
var startedAt string
if item.StartedAt != 0 {
startedAt = fmt.Sprint(time.Unix(0, item.StartedAt))
}
if req.TrackComposite.AudioTrackId != "" {
trackIDs = append(trackIDs, req.TrackComposite.AudioTrackId)
var egressType, egressSource string
switch req := item.Request.(type) {
case *livekit.EgressInfo_RoomComposite:
egressType = "room_composite"
egressSource = req.RoomComposite.RoomName
case *livekit.EgressInfo_Web:
egressType = "web"
egressSource = req.Web.Url
case *livekit.EgressInfo_Participant:
egressType = "participant"
egressSource = fmt.Sprintf("%s/%s", req.Participant.RoomName, req.Participant.Identity)
case *livekit.EgressInfo_TrackComposite:
egressType = "track_composite"
trackIDs := make([]string, 0)
if req.TrackComposite.VideoTrackId != "" {
trackIDs = append(trackIDs, req.TrackComposite.VideoTrackId)
}
if req.TrackComposite.AudioTrackId != "" {
trackIDs = append(trackIDs, req.TrackComposite.AudioTrackId)
}
egressSource = fmt.Sprintf("%s/%s", req.TrackComposite.RoomName, strings.Join(trackIDs, ","))
case *livekit.EgressInfo_Track:
egressType = "track"
egressSource = fmt.Sprintf("%s/%s", req.Track.RoomName, req.Track.TrackId)
}
egressSource = fmt.Sprintf("%s/%s", req.TrackComposite.RoomName, strings.Join(trackIDs, ","))
case *livekit.EgressInfo_Track:
egressType = "track"
egressSource = fmt.Sprintf("%s/%s", req.Track.RoomName, req.Track.TrackId)
table.Row(
item.EgressId,
item.Status.String(),
egressType,
egressSource,
startedAt,
item.Error,
)
}
table.Row(
item.EgressId,
item.Status.String(),
egressType,
egressSource,
startedAt,
item.Error,
)
}
fmt.Println(table)
fmt.Println(table)
}

return nil
}
Expand Down
54 changes: 29 additions & 25 deletions cmd/lk/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var (
Usage: "List a specific ingress `ID`",
Required: false,
},
jsonFlag,
},
},
{
Expand Down Expand Up @@ -223,33 +224,36 @@ func listIngress(ctx context.Context, cmd *cli.Command) error {
return err
}

table := CreateTable().
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
for _, item := range res.Items {
if item == nil {
continue
}
// NOTE: previously, the `verbose` flag was used to output JSON in addition to the table.
// This is inconsistent with other commands in which verbose is used for debug info, but is
// kept for compatibility with the previous behavior.
if cmd.Bool("verbose") || cmd.Bool("json") {
PrintJSON(res)
} else {
table := CreateTable().
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
for _, item := range res.Items {
if item == nil {
continue
}

var status, errorStr string
if item.State != nil {
status = item.State.Status.String()
errorStr = item.State.Error
}
var status, errorStr string
if item.State != nil {
status = item.State.Status.String()
errorStr = item.State.Error
}

table.Row(
item.IngressId,
item.Name,
item.RoomName,
item.StreamKey,
item.Url,
status,
errorStr,
)
}
fmt.Println(table)

if cmd.Bool("verbose") {
PrintJSON(res)
table.Row(
item.IngressId,
item.Name,
item.RoomName,
item.StreamKey,
item.Url,
status,
errorStr,
)
}
fmt.Println(table)
}

return nil
Expand Down
35 changes: 20 additions & 15 deletions cmd/lk/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var (
Usage: "List all configured projects",
UsageText: "lk project list",
Action: listProjects,
Flags: []cli.Flag{jsonFlag},
},
{
Name: "remove",
Expand Down Expand Up @@ -247,22 +248,26 @@ func listProjects(ctx context.Context, cmd *cli.Command) error {
headerStyle := baseStyle.Bold(true)
selectedStyle := theme.Focused.Title.Padding(0, 1)

table := CreateTable().
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == table.HeaderRow:
return headerStyle
case cliConfig.Projects[row].Name == cliConfig.DefaultProject:
return selectedStyle
default:
return baseStyle
}
}).
Headers("Name", "URL", "API Key", "Default")
for _, p := range cliConfig.Projects {
table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
if cmd.Bool("json") {
PrintJSON(cliConfig.Projects)
} else {
table := CreateTable().
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == table.HeaderRow:
return headerStyle
case cliConfig.Projects[row].Name == cliConfig.DefaultProject:
return selectedStyle
default:
return baseStyle
}
}).
Headers("Name", "URL", "API Key", "Default")
for _, p := range cliConfig.Projects {
table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
}
fmt.Println(table)
}
fmt.Println(table)

return nil
}
Expand Down
30 changes: 15 additions & 15 deletions cmd/lk/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,22 @@ func listAndPrint[
return err
}

table := CreateTable().
Headers(header...)
for _, item := range res.GetItems() {
if item == nil {
continue
}
row := tableRow(item)
if len(row) == 0 {
continue
}
table.Row(row...)
}
fmt.Println(table)

if cmd.Bool("verbose") {
if cmd.Bool("json") {
PrintJSON(res)
} else {
table := CreateTable().
Headers(header...)
for _, item := range res.GetItems() {
if item == nil {
continue
}
row := tableRow(item)
if len(row) == 0 {
continue
}
table.Row(row...)
}
fmt.Println(table)
}

return nil
Expand Down
15 changes: 10 additions & 5 deletions cmd/lk/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
Name: "list",
Before: createReplayClient,
Action: listReplays,
Flags: []cli.Flag{jsonFlag},
},
{
Name: "load",
Expand Down Expand Up @@ -114,7 +115,7 @@ func createReplayClient(ctx context.Context, cmd *cli.Command) error {
return nil
}

func listReplays(ctx context.Context, _ *cli.Command) error {
func listReplays(ctx context.Context, cmd *cli.Command) error {
ctx, err := replayClient.withAuth(ctx)
if err != nil {
return err
Expand All @@ -126,11 +127,15 @@ func listReplays(ctx context.Context, _ *cli.Command) error {
return err
}

table := CreateTable().Headers("ReplayID")
for _, info := range res.Replays {
table.Row(info.ReplayId)
if cmd.Bool("json") {
PrintJSON(res.Replays)
} else {
table := CreateTable().Headers("ReplayID")
for _, info := range res.Replays {
table.Row(info.ReplayId)
}
fmt.Println(table)
}
fmt.Println(table)

return nil
}
Expand Down
31 changes: 21 additions & 10 deletions cmd/lk/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var (
Before: createRoomClient,
Action: listRooms,
ArgsUsage: "[ROOM_NAME ...]",
Flags: []cli.Flag{jsonFlag},
},
{
Name: "update",
Expand Down Expand Up @@ -641,6 +642,13 @@ func createRoom(ctx context.Context, cmd *cli.Command) error {

func listRooms(ctx context.Context, cmd *cli.Command) error {
names, _ := extractArgs(cmd)
if cmd.Bool("verbose") && len(names) > 0 {
fmt.Printf(
"Querying rooms matching %s",
strings.Join(mapStrings(names, wrapWith("\"")), ", "),
)
}

req := livekit.ListRoomsRequest{}
if len(names) > 0 {
req.Names = names
Expand All @@ -650,19 +658,22 @@ func listRooms(ctx context.Context, cmd *cli.Command) error {
if err != nil {
return err
}
if len(res.Rooms) == 0 {
if len(names) > 0 {
fmt.Printf(
"there are no rooms matching %s",
strings.Join(mapStrings(names, wrapWith("\"")), ", "),

if cmd.Bool("json") {
PrintJSON(res)
} else {
table := CreateTable().Headers("RoomID", "Name", "Participants", "Publishers")
for _, rm := range res.Rooms {
table.Row(
rm.Sid,
rm.Name,
fmt.Sprintf("%d", rm.NumParticipants),
fmt.Sprintf("%d", rm.NumPublishers),
)
} else {
fmt.Println("there are no active rooms")
}
fmt.Println(table)
}
for _, rm := range res.Rooms {
fmt.Printf("%s\t%s\t%d participants\n", rm.Sid, rm.Name, rm.NumParticipants)
}

return nil
}

Expand Down
Loading

0 comments on commit f9635e1

Please sign in to comment.