Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --no-pretty global flag for parsable CLI output #399

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func init() {
rootCmd.PersistentFlags().String("client-id", "", "Client ID. Sent to the Token Issuer during the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().String("client-secret", "", "Client Secret. Sent to the Token Issuer during the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().StringArray("api-scopes", []string{}, "API Scopes (repeat option for multiple values). Used in the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().Bool("no-pretty", false, "Disable pretty output features (useful for scripting)") //nolint:lll

rootCmd.MarkFlagsRequiredTogether(
"api-token-issuer",
Expand Down
14 changes: 14 additions & 0 deletions internal/output/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"os"

"github.com/gocarina/gocsv"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"

"github.com/mattn/go-isatty"
Expand Down Expand Up @@ -184,6 +185,10 @@

// Display will decorate the output if possible. Otherwise, will print out the standard JSON.
func Display(data any) error {
if viper.GetBool("no-pretty") {
return outputParsable(data)
}

if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
if os.Getenv("NO_COLOR") != "" {
return displayNoColorTerminal(data)
Expand All @@ -194,3 +199,12 @@

return outputNonTerminal(data)
}

func outputParsable(data any) error {
result, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("unable to marshal json with error %w", err)
}
fmt.Println(string(result))

Check failure on line 208 in internal/output/marshal.go

View workflow job for this annotation

GitHub Actions / Lints

expressions should not be cuddled with blocks (wsl)
return nil

Check failure on line 209 in internal/output/marshal.go

View workflow job for this annotation

GitHub Actions / Lints

return with no blank line before (nlreturn)
}
Loading