Skip to content

Commit

Permalink
feat: Add ndjson output type
Browse files Browse the repository at this point in the history
Newline-delimeted JSON as alternate output type for list values.
  • Loading branch information
alanconway committed Nov 8, 2024
1 parent 5152af7 commit db2fe33
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
24 changes: 17 additions & 7 deletions pkg/cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"reflect"

"sigs.k8s.io/yaml"
)
Expand All @@ -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) }

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit db2fe33

Please sign in to comment.