From db2fe33f57026568c259adde4aef5c0b3aad6552 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Fri, 8 Nov 2024 09:31:42 -0500 Subject: [PATCH] feat: Add ndjson output type Newline-delimeted JSON as alternate output type for list values. --- Makefile | 11 ++++++----- pkg/cmd/main.go | 2 +- pkg/cmd/operations.go | 4 ++-- pkg/cmd/output.go | 24 +++++++++++++++++------- pkg/cmd/web.go | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 5cd4e80..df50e40 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,6 @@ include .bingo/Variables.mk VERSION_TXT=pkg/build/version.txt SWAGGER_SPEC=swagger.json SWAGGER_CLIENT=pkg/swagger -KORREL8RCLI=./korrel8rcli lint: $(SWAGGER_CLIENT) $(GOLANGCI_LINT) go mod tidy @@ -17,16 +16,18 @@ lint: $(SWAGGER_CLIENT) $(GOLANGCI_LINT) exit 1; \ fi -build: $(KORREL8RCLI) -$(KORREL8RCLI): $(VERSION_TXT) $(SWAGGER_CLIENT) - go build -o $@ ./cmd/korrel8rcli +build: $(VERSION_TXT) $(SWAGGER_CLIENT) + go build ./cmd/korrel8rcli + +install: $(VERSION_TXT) $(SWAGGER_CLIENT) + go install ./cmd/korrel8rcli test: go test -cover -race ./... go tool covdata percent -i pkg/cmd/_covdata clean: - rm -rfv $(SWAGGER_CLIENT) $(KORREL8RCLI) + rm -rfv $(SWAGGER_CLIENT) korrel8rcli git clean -dfx run: diff --git a/pkg/cmd/main.go b/pkg/cmd/main.go index c8d62a9..2fc7d49 100644 --- a/pkg/cmd/main.go +++ b/pkg/cmd/main.go @@ -36,7 +36,7 @@ var ( } // Global Flags - output = EnumFlag("yaml", "json-pretty", "json") + output = EnumFlag("yaml", "json-pretty", "json", "ndjson") korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", urlDefault(), fmt.Sprintf("URL of remote korrel8r. Default from env %v", envURL)) insecure = rootCmd.PersistentFlags().BoolP("insecure", "k", false, "Insecure connection, skip TLS verification.") diff --git a/pkg/cmd/operations.go b/pkg/cmd/operations.go index b40204a..c409452 100644 --- a/pkg/cmd/operations.go +++ b/pkg/cmd/operations.go @@ -63,7 +63,7 @@ func init() { } var neighboursCmd = &cobra.Command{ - Use: "neighbours [FLAGS]", + Use: "neighbours", Short: "Get graph of nearest neighbours", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { @@ -81,7 +81,7 @@ var neighboursCmd = &cobra.Command{ } var goalsCmd = &cobra.Command{ - Use: "goals [FLAGS] CLASS...", + Use: "goals CLASS...", Short: "Get graph of goal classes reachable from start", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { diff --git a/pkg/cmd/output.go b/pkg/cmd/output.go index 42b45e5..5c941f4 100644 --- a/pkg/cmd/output.go +++ b/pkg/cmd/output.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "reflect" "sigs.k8s.io/yaml" ) @@ -13,19 +14,28 @@ func NewPrinter(format string, w io.Writer) func(any) { switch format { case "json": - return func(v any) { - if b, err := json.Marshal(v); err != nil { - fmt.Fprintf(w, "%v\n", err) - } else { - fmt.Fprintf(w, "%v\n", string(b)) - } - } + encoder := json.NewEncoder(w) + return func(v any) { _ = encoder.Encode(v) } case "json-pretty": encoder := json.NewEncoder(w) encoder.SetIndent("", " ") return func(v any) { _ = encoder.Encode(v) } + case "ndjson": + encoder := json.NewEncoder(w) + return func(v any) { + r := reflect.ValueOf(v) + switch r.Kind() { + case reflect.Array, reflect.Slice: + for i := 0; i < r.Len(); i++ { + _ = encoder.Encode(r.Index(i).Interface()) + } + default: + _ = encoder.Encode(v) + } + } + case "yaml": return func(v any) { y, _ := yaml.Marshal(v); fmt.Fprintf(w, "%s", y) } diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index ac86d13..bb789a3 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -12,7 +12,7 @@ import ( ) var webCmd = &cobra.Command{ - Use: "web [FLAGS]", + Use: "web", Short: "Connect to remote korrel8r, show graphs in local HTTP server.", Args: cobra.NoArgs, RunE: func(_ *cobra.Command, args []string) error {