Skip to content

etcdctl: fix json output #19469

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions etcdctl/ctlv3/command/printer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"strconv"
"strings"

pb "go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
)

Expand All @@ -29,6 +30,37 @@
printer
}

type (
HexResponseHeader pb.ResponseHeader
HexMember pb.Member
)

func (h *HexResponseHeader) MarshalJSON() ([]byte, error) {
type Alias pb.ResponseHeader

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L38-L39

Added lines #L38 - L39 were not covered by tests

return json.Marshal(&struct {
ClusterID string `json:"cluster_id"`
MemberID string `json:"member_id"`
Alias
}{
ClusterID: fmt.Sprintf("%x", h.ClusterId),
MemberID: fmt.Sprintf("%x", h.MemberId),
Alias: (Alias)(*h),
})

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L41-L49

Added lines #L41 - L49 were not covered by tests
}

func (m *HexMember) MarshalJSON() ([]byte, error) {
type Alias pb.Member

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L52-L53

Added lines #L52 - L53 were not covered by tests

return json.Marshal(&struct {
ID string `json:"ID"`
Alias
}{
ID: fmt.Sprintf("%x", m.ID),
Alias: (Alias)(*m),
})

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L55-L61

Added lines #L55 - L61 were not covered by tests
}

func newJSONPrinter(isHex bool) printer {
return &jsonPrinter{
isHex: isHex,
Expand All @@ -47,6 +79,7 @@
printJSON(r)
}
}
func (p *jsonPrinter) MemberAdd(r clientv3.MemberAddResponse) { p.printJSON(r) }

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L82

Added line #L82 was not covered by tests

func printJSON(v any) {
b, err := json.Marshal(v)
Expand Down Expand Up @@ -98,3 +131,40 @@
buffer.WriteString("}")
fmt.Println(buffer.String())
}

func (p *jsonPrinter) printJSON(v any) {
var data any
if !p.isHex {
printJSON(v)
return

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L135-L139

Added lines #L135 - L139 were not covered by tests
}

switch r := v.(type) {
case clientv3.MemberAddResponse:
type Alias clientv3.MemberAddResponse

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L142-L144

Added lines #L142 - L144 were not covered by tests

data = &struct {
Header *HexResponseHeader `json:"header"`
Member *HexMember `json:"member"`
Members []*HexMember `json:"members"`
*Alias
}{
Header: (*HexResponseHeader)(r.Header),
Member: (*HexMember)(r.Member),
Members: toHexMembers(r.Members),
Alias: (*Alias)(&r),

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L146-L155

Added lines #L146 - L155 were not covered by tests
}
default:
data = v

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L157-L158

Added lines #L157 - L158 were not covered by tests
}

printJSON(data)

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L161

Added line #L161 was not covered by tests
}

func toHexMembers(members []*pb.Member) []*HexMember {
hexMembers := make([]*HexMember, len(members))
for i, member := range members {
hexMembers[i] = (*HexMember)(member)

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L164-L167

Added lines #L164 - L167 were not covered by tests
}
return hexMembers

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

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/printer_json.go#L169

Added line #L169 was not covered by tests
}