Skip to content

Commit

Permalink
Add --json alias if --format json is supported (#127)
Browse files Browse the repository at this point in the history
* Add --json alias if --format json is supported

* Rename function to ResolveFormat

* Update description for --json flag
  • Loading branch information
narasaka authored Sep 21, 2023
1 parent d8c6c75 commit 0d032d9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions foxglove/cmd/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ func newListAttachmentsCommand(params *baseParams) *cobra.Command {
var format string
var importID string
var recordingID string
var isJsonFormat bool
attachmentsListCmd := &cobra.Command{
Use: "list",
Short: "List MCAP attachments",
Run: func(cmd *cobra.Command, args []string) {
format = ResolveFormat(format, isJsonFormat)
client := console.NewRemoteFoxgloveClient(
params.baseURL, *params.clientID,
viper.GetString("bearer_token"),
Expand All @@ -42,6 +44,7 @@ func newListAttachmentsCommand(params *baseParams) *cobra.Command {
attachmentsListCmd.PersistentFlags().StringVarP(&importID, "import-id", "", "", "Import ID")
attachmentsListCmd.PersistentFlags().StringVarP(&recordingID, "recording-id", "", "", "Recording ID")
AddFormatFlag(attachmentsListCmd, &format)
AddJsonFlag(attachmentsListCmd, &isJsonFormat)
return attachmentsListCmd
}

Expand Down
3 changes: 3 additions & 0 deletions foxglove/cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
var tolerance int
var recordingID string
var includeEdgeRecordings bool
var isJsonFormat bool
coverageListCmd := &cobra.Command{
Use: "list",
Short: "List coverage ranges",
Expand All @@ -35,6 +36,7 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
if err != nil {
dief("failed to parse end time: %s", err)
}
format = ResolveFormat(format, isJsonFormat)
err = renderList(
os.Stdout,
&console.CoverageRequest{
Expand Down Expand Up @@ -66,5 +68,6 @@ func newListCoverageCommand(params *baseParams) *cobra.Command {
coverageListCmd.PersistentFlags().StringVarP(&end, "end", "", "", "end of coverage time range (ISO8601)")
AddFormatFlag(coverageListCmd, &format)
AddDeviceAutocompletion(coverageListCmd, params)
AddJsonFlag(coverageListCmd, &isJsonFormat)
return coverageListCmd
}
3 changes: 3 additions & 0 deletions foxglove/cmd/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func newListDevicesCommand(params *baseParams) *cobra.Command {
var format string
var isJsonFormat bool
deviceListCmd := &cobra.Command{
Use: "list",
Short: "List devices registered to your organization",
Expand All @@ -20,6 +21,7 @@ func newListDevicesCommand(params *baseParams) *cobra.Command {
params.token,
params.userAgent,
)
format = ResolveFormat(format, isJsonFormat)
err := renderList(
os.Stdout,
console.DevicesRequest{},
Expand All @@ -33,6 +35,7 @@ func newListDevicesCommand(params *baseParams) *cobra.Command {
}
deviceListCmd.InheritedFlags()
AddFormatFlag(deviceListCmd, &format)
AddJsonFlag(deviceListCmd, &isJsonFormat)
return deviceListCmd
}

Expand Down
3 changes: 3 additions & 0 deletions foxglove/cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
var start string
var end string
var query string
var isJsonFormat bool
eventsListCmd := &cobra.Command{
Use: "list",
Short: "List events",
Expand All @@ -73,6 +74,7 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
params.token,
params.userAgent,
)
format = ResolveFormat(format, isJsonFormat)
err := renderList(
os.Stdout,
&console.EventsRequest{
Expand Down Expand Up @@ -104,5 +106,6 @@ func newListEventsCommand(params *baseParams) *cobra.Command {
eventsListCmd.PersistentFlags().StringVarP(&query, "query", "", "", "Filter by metadata with keyword or \"$key:$value\"")
AddDeviceAutocompletion(eventsListCmd, params)
AddFormatFlag(eventsListCmd, &format)
AddJsonFlag(eventsListCmd, &isJsonFormat)
return eventsListCmd
}
3 changes: 3 additions & 0 deletions foxglove/cmd/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func newPublishExtensionCommand(params *baseParams) *cobra.Command {

func newListExtensionsCommand(params *baseParams) *cobra.Command {
var format string
var isJsonFormat bool
listCmd := &cobra.Command{
Use: "list",
Short: "List Studio extensions created for your organization",
Expand All @@ -56,6 +57,7 @@ func newListExtensionsCommand(params *baseParams) *cobra.Command {
params.token,
params.userAgent,
)
format = ResolveFormat(format, isJsonFormat)
err := renderList(
os.Stdout,
console.ExtensionsRequest{},
Expand All @@ -70,6 +72,7 @@ func newListExtensionsCommand(params *baseParams) *cobra.Command {
}
listCmd.InheritedFlags()
AddFormatFlag(listCmd, &format)
AddJsonFlag(listCmd, &isJsonFormat)
return listCmd
}

Expand Down
3 changes: 3 additions & 0 deletions foxglove/cmd/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
var dataStart string
var includeDeleted bool
var dataEnd string
var isJsonFormat bool
importsListCmd := &cobra.Command{
Use: "list",
Short: "List imports for a device",
Expand All @@ -41,6 +42,7 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
if err != nil {
dief("failed to parse data end time: %s", err)
}
format = ResolveFormat(format, isJsonFormat)
err = renderList(
os.Stdout,
&console.ImportsRequest{
Expand Down Expand Up @@ -68,5 +70,6 @@ func newListImportsCommand(params *baseParams) *cobra.Command {
importsListCmd.PersistentFlags().StringVarP(&dataEnd, "data-end", "", "", "end of data time range (ISO8601)")
importsListCmd.PersistentFlags().BoolVarP(&includeDeleted, "include-deleted", "", false, "end of data time range")
AddFormatFlag(importsListCmd, &format)
AddJsonFlag(importsListCmd, &isJsonFormat)
return importsListCmd
}
3 changes: 3 additions & 0 deletions foxglove/cmd/pending_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
var showQuarantined bool
var siteId string
var updatedSince string
var isJsonFormat bool
pendingImportsCmd := &cobra.Command{
Use: "list",
Short: "List the pending and errored import jobs for uploaded recordings",
Expand All @@ -33,6 +34,7 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
fmt.Fprintf(os.Stderr, "Failed to parse value of --updated-since: %s\n", err)
os.Exit(1)
}
format = ResolveFormat(format, isJsonFormat)
err = renderList(
os.Stdout,
console.PendingImportsRequest{
Expand Down Expand Up @@ -66,5 +68,6 @@ func newPendingImportsCommand(params *baseParams) *cobra.Command {
pendingImportsCmd.PersistentFlags().StringVarP(&siteId, "site-id", "", "", "Site ID")
pendingImportsCmd.PersistentFlags().StringVarP(&updatedSince, "updated-since", "", "", "Filter pending imports updated since this time (ISO8601)")
AddFormatFlag(pendingImportsCmd, &format)
AddJsonFlag(pendingImportsCmd, &isJsonFormat)
return pendingImportsCmd
}
3 changes: 3 additions & 0 deletions foxglove/cmd/recordings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
var start string
var end string
var importStatus string
var isJsonFormat bool
recordingsListCmd := &cobra.Command{
Use: "list",
Short: "List recordings",
Expand All @@ -35,6 +36,7 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
if err != nil {
dief("failed to parse end time: %s", err)
}
format = ResolveFormat(format, isJsonFormat)
err = renderList(
os.Stdout,
&console.RecordingsRequest{
Expand Down Expand Up @@ -67,5 +69,6 @@ func newListRecordingsCommand(params *baseParams) *cobra.Command {
recordingsListCmd.PersistentFlags().StringVarP(&importStatus, "import-status", "", "", "import status")
AddFormatFlag(recordingsListCmd, &format)
AddDeviceAutocompletion(recordingsListCmd, params)
AddJsonFlag(recordingsListCmd, &isJsonFormat)
return recordingsListCmd
}
28 changes: 27 additions & 1 deletion foxglove/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,37 @@ func AddFormatFlag(cmd *cobra.Command, format *string) {
format,
"format",
"",
"table",
"",
"render output in specified format (table, json, csv)",
)
}

func AddJsonFlag(cmd *cobra.Command, isJsonFormat *bool) {
cmd.PersistentFlags().BoolVar(
isJsonFormat,
"json",
false,
"alias for --format json",
)
}

// Ensure --json alias is not conflicting with --format's value
func ResolveFormat(formatFlagValue string, jsonFlagValue bool) string {
if formatFlagValue == "" {
if jsonFlagValue {
return "json"
} else {
return "table"
}
}

if jsonFlagValue && formatFlagValue != "json" {
dief("Command failed. Output format conflict: --json, --format")
}

return formatFlagValue
}

// AddDeviceAutocompletion adds autocompletion for device-name and device-id
// parameters to the command.
func AddDeviceAutocompletion(cmd *cobra.Command, params *baseParams) {
Expand Down

0 comments on commit 0d032d9

Please sign in to comment.