Skip to content

Commit 0d032d9

Browse files
authored
Add --json alias if --format json is supported (#127)
* Add --json alias if --format json is supported * Rename function to ResolveFormat * Update description for --json flag
1 parent d8c6c75 commit 0d032d9

9 files changed

+51
-1
lines changed

foxglove/cmd/attachments.go

+3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ func newListAttachmentsCommand(params *baseParams) *cobra.Command {
1414
var format string
1515
var importID string
1616
var recordingID string
17+
var isJsonFormat bool
1718
attachmentsListCmd := &cobra.Command{
1819
Use: "list",
1920
Short: "List MCAP attachments",
2021
Run: func(cmd *cobra.Command, args []string) {
22+
format = ResolveFormat(format, isJsonFormat)
2123
client := console.NewRemoteFoxgloveClient(
2224
params.baseURL, *params.clientID,
2325
viper.GetString("bearer_token"),
@@ -42,6 +44,7 @@ func newListAttachmentsCommand(params *baseParams) *cobra.Command {
4244
attachmentsListCmd.PersistentFlags().StringVarP(&importID, "import-id", "", "", "Import ID")
4345
attachmentsListCmd.PersistentFlags().StringVarP(&recordingID, "recording-id", "", "", "Recording ID")
4446
AddFormatFlag(attachmentsListCmd, &format)
47+
AddJsonFlag(attachmentsListCmd, &isJsonFormat)
4548
return attachmentsListCmd
4649
}
4750

foxglove/cmd/coverage.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
1616
var tolerance int
1717
var recordingID string
1818
var includeEdgeRecordings bool
19+
var isJsonFormat bool
1920
coverageListCmd := &cobra.Command{
2021
Use: "list",
2122
Short: "List coverage ranges",
@@ -35,6 +36,7 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
3536
if err != nil {
3637
dief("failed to parse end time: %s", err)
3738
}
39+
format = ResolveFormat(format, isJsonFormat)
3840
err = renderList(
3941
os.Stdout,
4042
&console.CoverageRequest{
@@ -66,5 +68,6 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
6668
coverageListCmd.PersistentFlags().StringVarP(&end, "end", "", "", "end of coverage time range (ISO8601)")
6769
AddFormatFlag(coverageListCmd, &format)
6870
AddDeviceAutocompletion(coverageListCmd, params)
71+
AddJsonFlag(coverageListCmd, &isJsonFormat)
6972
return coverageListCmd
7073
}

foxglove/cmd/devices.go

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
func newListDevicesCommand(params *baseParams) *cobra.Command {
1313
var format string
14+
var isJsonFormat bool
1415
deviceListCmd := &cobra.Command{
1516
Use: "list",
1617
Short: "List devices registered to your organization",
@@ -20,6 +21,7 @@ func newListDevicesCommand(params *baseParams) *cobra.Command {
2021
params.token,
2122
params.userAgent,
2223
)
24+
format = ResolveFormat(format, isJsonFormat)
2325
err := renderList(
2426
os.Stdout,
2527
console.DevicesRequest{},
@@ -33,6 +35,7 @@ func newListDevicesCommand(params *baseParams) *cobra.Command {
3335
}
3436
deviceListCmd.InheritedFlags()
3537
AddFormatFlag(deviceListCmd, &format)
38+
AddJsonFlag(deviceListCmd, &isJsonFormat)
3639
return deviceListCmd
3740
}
3841

foxglove/cmd/events.go

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
6464
var start string
6565
var end string
6666
var query string
67+
var isJsonFormat bool
6768
eventsListCmd := &cobra.Command{
6869
Use: "list",
6970
Short: "List events",
@@ -73,6 +74,7 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
7374
params.token,
7475
params.userAgent,
7576
)
77+
format = ResolveFormat(format, isJsonFormat)
7678
err := renderList(
7779
os.Stdout,
7880
&console.EventsRequest{
@@ -104,5 +106,6 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
104106
eventsListCmd.PersistentFlags().StringVarP(&query, "query", "", "", "Filter by metadata with keyword or \"$key:$value\"")
105107
AddDeviceAutocompletion(eventsListCmd, params)
106108
AddFormatFlag(eventsListCmd, &format)
109+
AddJsonFlag(eventsListCmd, &isJsonFormat)
107110
return eventsListCmd
108111
}

foxglove/cmd/extensions.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func newPublishExtensionCommand(params *baseParams) *cobra.Command {
4747

4848
func newListExtensionsCommand(params *baseParams) *cobra.Command {
4949
var format string
50+
var isJsonFormat bool
5051
listCmd := &cobra.Command{
5152
Use: "list",
5253
Short: "List Studio extensions created for your organization",
@@ -56,6 +57,7 @@ func newListExtensionsCommand(params *baseParams) *cobra.Command {
5657
params.token,
5758
params.userAgent,
5859
)
60+
format = ResolveFormat(format, isJsonFormat)
5961
err := renderList(
6062
os.Stdout,
6163
console.ExtensionsRequest{},
@@ -70,6 +72,7 @@ func newListExtensionsCommand(params *baseParams) *cobra.Command {
7072
}
7173
listCmd.InheritedFlags()
7274
AddFormatFlag(listCmd, &format)
75+
AddJsonFlag(listCmd, &isJsonFormat)
7376
return listCmd
7477
}
7578

foxglove/cmd/imports.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
1616
var dataStart string
1717
var includeDeleted bool
1818
var dataEnd string
19+
var isJsonFormat bool
1920
importsListCmd := &cobra.Command{
2021
Use: "list",
2122
Short: "List imports for a device",
@@ -41,6 +42,7 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
4142
if err != nil {
4243
dief("failed to parse data end time: %s", err)
4344
}
45+
format = ResolveFormat(format, isJsonFormat)
4446
err = renderList(
4547
os.Stdout,
4648
&console.ImportsRequest{
@@ -68,5 +70,6 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
6870
importsListCmd.PersistentFlags().StringVarP(&dataEnd, "data-end", "", "", "end of data time range (ISO8601)")
6971
importsListCmd.PersistentFlags().BoolVarP(&includeDeleted, "include-deleted", "", false, "end of data time range")
7072
AddFormatFlag(importsListCmd, &format)
73+
AddJsonFlag(importsListCmd, &isJsonFormat)
7174
return importsListCmd
7275
}

foxglove/cmd/pending_imports.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
1919
var showQuarantined bool
2020
var siteId string
2121
var updatedSince string
22+
var isJsonFormat bool
2223
pendingImportsCmd := &cobra.Command{
2324
Use: "list",
2425
Short: "List the pending and errored import jobs for uploaded recordings",
@@ -33,6 +34,7 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
3334
fmt.Fprintf(os.Stderr, "Failed to parse value of --updated-since: %s\n", err)
3435
os.Exit(1)
3536
}
37+
format = ResolveFormat(format, isJsonFormat)
3638
err = renderList(
3739
os.Stdout,
3840
console.PendingImportsRequest{
@@ -66,5 +68,6 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
6668
pendingImportsCmd.PersistentFlags().StringVarP(&siteId, "site-id", "", "", "Site ID")
6769
pendingImportsCmd.PersistentFlags().StringVarP(&updatedSince, "updated-since", "", "", "Filter pending imports updated since this time (ISO8601)")
6870
AddFormatFlag(pendingImportsCmd, &format)
71+
AddJsonFlag(pendingImportsCmd, &isJsonFormat)
6972
return pendingImportsCmd
7073
}

foxglove/cmd/recordings.go

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
1818
var start string
1919
var end string
2020
var importStatus string
21+
var isJsonFormat bool
2122
recordingsListCmd := &cobra.Command{
2223
Use: "list",
2324
Short: "List recordings",
@@ -35,6 +36,7 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
3536
if err != nil {
3637
dief("failed to parse end time: %s", err)
3738
}
39+
format = ResolveFormat(format, isJsonFormat)
3840
err = renderList(
3941
os.Stdout,
4042
&console.RecordingsRequest{
@@ -67,5 +69,6 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
6769
recordingsListCmd.PersistentFlags().StringVarP(&importStatus, "import-status", "", "", "import status")
6870
AddFormatFlag(recordingsListCmd, &format)
6971
AddDeviceAutocompletion(recordingsListCmd, params)
72+
AddJsonFlag(recordingsListCmd, &isJsonFormat)
7073
return recordingsListCmd
7174
}

foxglove/cmd/utils.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,37 @@ func AddFormatFlag(cmd *cobra.Command, format *string) {
150150
format,
151151
"format",
152152
"",
153-
"table",
153+
"",
154154
"render output in specified format (table, json, csv)",
155155
)
156156
}
157157

158+
func AddJsonFlag(cmd *cobra.Command, isJsonFormat *bool) {
159+
cmd.PersistentFlags().BoolVar(
160+
isJsonFormat,
161+
"json",
162+
false,
163+
"alias for --format json",
164+
)
165+
}
166+
167+
// Ensure --json alias is not conflicting with --format's value
168+
func ResolveFormat(formatFlagValue string, jsonFlagValue bool) string {
169+
if formatFlagValue == "" {
170+
if jsonFlagValue {
171+
return "json"
172+
} else {
173+
return "table"
174+
}
175+
}
176+
177+
if jsonFlagValue && formatFlagValue != "json" {
178+
dief("Command failed. Output format conflict: --json, --format")
179+
}
180+
181+
return formatFlagValue
182+
}
183+
158184
// AddDeviceAutocompletion adds autocompletion for device-name and device-id
159185
// parameters to the command.
160186
func AddDeviceAutocompletion(cmd *cobra.Command, params *baseParams) {

0 commit comments

Comments
 (0)