Skip to content

Commit

Permalink
fix(get): avoid usage of --format and --pretty at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
graugans committed May 14, 2024
1 parent b35da82 commit 0b8c5f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/ovp8xx/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"os"
"text/template"
Expand Down Expand Up @@ -54,6 +55,10 @@ func getCommand(cmd *cobra.Command, args []string) error {
}

if cmd.Flags().Changed("format") {
if helper.prettyPrint() {
return errors.New("you can't use --pretty and --format at the same time")
}

format, err := cmd.Flags().GetString("format")
if err != nil {
return fmt.Errorf("unable to get the format string from the command line: %w", err)
Expand Down
7 changes: 6 additions & 1 deletion cmd/ovp8xx/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type helperConfig struct {

func (c *helperConfig) printJSONResult(data string) error {
var message string = data
if c.pretty {
if c.prettyPrint() {
var js json.RawMessage
if err := json.Unmarshal([]byte(data), &js); err != nil {
return errors.New("malformed json")
Expand All @@ -44,6 +44,11 @@ func (c *helperConfig) remotePort() uint16 {
return c.port
}

// prettyPrint returns a boolean value indicating whether the output should be pretty-printed.
func (c *helperConfig) prettyPrint() bool {
return c.pretty
}

func NewHelper(cmd *cobra.Command) (helperConfig, error) {
var conf = helperConfig{}
var err error
Expand Down

0 comments on commit 0b8c5f8

Please sign in to comment.