Skip to content

Commit

Permalink
fix(cli): Implicitly output JSON formatted if the output is a valid J…
Browse files Browse the repository at this point in the history
…SON string.

Signed-off-by: sbailey <[email protected]>
  • Loading branch information
spbsoluble committed Aug 26, 2024
1 parent eecd92c commit ab0cc59
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
35 changes: 28 additions & 7 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func boolToPointer(b bool) *bool {
Expand Down Expand Up @@ -328,11 +329,31 @@ func outputError(err error, isFatal bool, format string) {
}

func outputResult(result interface{}, format string) {
output := fmt.Sprintf("%s", result)

// test if output is a JSON string
var jsonMap map[string]interface{}
jerr := json.Unmarshal([]byte(output), &jsonMap)
if jerr == nil {
format = "json"
}

if format == "json" {
fmt.Println(result)
} else {
fmt.Println(fmt.Sprintf("%s", result))
jsonOutput, err := json.MarshalIndent(output, "", " ")
if err != nil {
//then output a { "message": "result" } json
output = fmt.Sprintf("{\"message\": \"%s\"}", result)
fmt.Println(output)
return
}
output = fmt.Sprintf("%s", jsonOutput)
fmt.Println(output)
return
}

fmt.Println(output)
return

}

func readCSVHeader(filename string) ([]string, error) {
Expand Down
9 changes: 4 additions & 5 deletions cmd/storeTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ var storesTypeCreateCmd = &cobra.Command{
if storeTypeConfigFile != "" {
createdStore, err := createStoreFromFile(storeTypeConfigFile, kfClient)
if err != nil {
fmt.Printf("Failed to create store type from file \"%s\"", err)
log.Error().Err(err).Msg("unable to create store type from file")
return err
}

fmt.Printf("Created store type called \"%s\"\n", createdStore.Name)
outputResult(fmt.Sprintf("Created store type called \"%s\"", createdStore.Name), outputFormat)
return nil
}

Expand Down Expand Up @@ -362,7 +362,7 @@ var fetchStoreTypesCmd = &cobra.Command{
log.Error().Err(jErr).Msg("unable to marshal store types to JSON")
return jErr
}
fmt.Println(string(output))
outputResult(output, outputFormat)
return nil
},
}
Expand Down Expand Up @@ -450,8 +450,7 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
func getValidStoreTypes(fp string, gitRef string) []string {
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef)
if rErr != nil {
log.Printf("Error: %s", rErr)
fmt.Printf("Error: %s\n", rErr)
log.Error().Err(rErr).Msg("unable to read store types")
return nil
}
validStoreTypesList := make([]string, 0, len(validStoreTypes))
Expand Down

0 comments on commit ab0cc59

Please sign in to comment.