Skip to content

etcdctl: refactor json printing to support custom writers #20177

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

Merged
merged 1 commit into from
Jun 16, 2025
Merged
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
19 changes: 13 additions & 6 deletions etcdctl/ctlv3/command/printer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand All @@ -25,12 +26,14 @@
)

type jsonPrinter struct {
isHex bool
writer io.Writer
isHex bool
printer
}

func newJSONPrinter(isHex bool) printer {
return &jsonPrinter{
writer: os.Stdout,

Check warning on line 36 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L36

Added line #L36 was not covered by tests
isHex: isHex,
printer: &printerRPC{newPrinterUnsupported("json"), printJSON},
}
Expand All @@ -42,22 +45,26 @@

func (p *jsonPrinter) MemberList(r clientv3.MemberListResponse) {
if p.isHex {
printMemberListWithHexJSON(r)
printMemberListWithHexJSON(p.writer, r)

Check warning on line 48 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L48

Added line #L48 was not covered by tests
} else {
printJSON(r)
}
}

func printJSON(v any) {
func printJSONTo(w io.Writer, v any) {

Check warning on line 54 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L54

Added line #L54 was not covered by tests
b, err := json.Marshal(v)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
fmt.Println(string(b))
fmt.Fprintln(w, string(b))

Check warning on line 60 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L60

Added line #L60 was not covered by tests
}

func printJSON(v any) {
printJSONTo(os.Stdout, v)

Check warning on line 64 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}

func printMemberListWithHexJSON(r clientv3.MemberListResponse) {
func printMemberListWithHexJSON(w io.Writer, r clientv3.MemberListResponse) {

Check warning on line 67 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L67

Added line #L67 was not covered by tests
var buffer strings.Builder
var b []byte
buffer.WriteString("{\"header\":{\"cluster_id\":\"")
Expand Down Expand Up @@ -96,5 +103,5 @@
}
}
buffer.WriteString("}")
fmt.Println(buffer.String())
fmt.Fprintln(w, buffer.String())

Check warning on line 106 in etcdctl/ctlv3/command/printer_json.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L106

Added line #L106 was not covered by tests
}