go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv3/command/printer_fields.go (about)

     1  // Copyright 2016 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package command
    16  
    17  import (
    18  	"fmt"
    19  
    20  	v3 "github.com/coreos/etcd/clientv3"
    21  	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
    22  	spb "github.com/coreos/etcd/mvcc/mvccpb"
    23  )
    24  
    25  type fieldsPrinter struct{ printer }
    26  
    27  func (p *fieldsPrinter) kv(pfx string, kv *spb.KeyValue) {
    28  	fmt.Printf("\"%sKey\" : %q\n", pfx, string(kv.Key))
    29  	fmt.Printf("\"%sCreateRevision\" : %d\n", pfx, kv.CreateRevision)
    30  	fmt.Printf("\"%sModRevision\" : %d\n", pfx, kv.ModRevision)
    31  	fmt.Printf("\"%sVersion\" : %d\n", pfx, kv.Version)
    32  	fmt.Printf("\"%sValue\" : %q\n", pfx, string(kv.Value))
    33  	fmt.Printf("\"%sLease\" : %d\n", pfx, kv.Lease)
    34  }
    35  
    36  func (p *fieldsPrinter) hdr(h *pb.ResponseHeader) {
    37  	fmt.Println(`"ClusterID" :`, h.ClusterId)
    38  	fmt.Println(`"MemberID" :`, h.MemberId)
    39  	fmt.Println(`"Revision" :`, h.Revision)
    40  	fmt.Println(`"RaftTerm" :`, h.RaftTerm)
    41  }
    42  
    43  func (p *fieldsPrinter) Del(r v3.DeleteResponse) {
    44  	p.hdr(r.Header)
    45  	fmt.Println(`"Deleted" :`, r.Deleted)
    46  	for _, kv := range r.PrevKvs {
    47  		p.kv("Prev", kv)
    48  	}
    49  }
    50  
    51  func (p *fieldsPrinter) Get(r v3.GetResponse) {
    52  	p.hdr(r.Header)
    53  	for _, kv := range r.Kvs {
    54  		p.kv("", kv)
    55  	}
    56  	fmt.Println(`"More" :`, r.More)
    57  	fmt.Println(`"Count" :`, r.Count)
    58  }
    59  
    60  func (p *fieldsPrinter) Put(r v3.PutResponse) {
    61  	p.hdr(r.Header)
    62  	if r.PrevKv != nil {
    63  		p.kv("Prev", r.PrevKv)
    64  	}
    65  }
    66  
    67  func (p *fieldsPrinter) Txn(r v3.TxnResponse) {
    68  	p.hdr(r.Header)
    69  	fmt.Println(`"Succeeded" :`, r.Succeeded)
    70  	for _, resp := range r.Responses {
    71  		switch v := resp.Response.(type) {
    72  		case *pb.ResponseOp_ResponseDeleteRange:
    73  			p.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
    74  		case *pb.ResponseOp_ResponsePut:
    75  			p.Put((v3.PutResponse)(*v.ResponsePut))
    76  		case *pb.ResponseOp_ResponseRange:
    77  			p.Get((v3.GetResponse)(*v.ResponseRange))
    78  		default:
    79  			fmt.Printf("\"Unknown\" : %q\n", fmt.Sprintf("%+v", v))
    80  		}
    81  	}
    82  }
    83  
    84  func (p *fieldsPrinter) Watch(resp v3.WatchResponse) {
    85  	p.hdr(&resp.Header)
    86  	for _, e := range resp.Events {
    87  		fmt.Println(`"Type" :`, e.Type)
    88  		if e.PrevKv != nil {
    89  			p.kv("Prev", e.PrevKv)
    90  		}
    91  		p.kv("", e.Kv)
    92  	}
    93  }
    94  
    95  func (p *fieldsPrinter) Grant(r v3.LeaseGrantResponse) {
    96  	p.hdr(r.ResponseHeader)
    97  	fmt.Println(`"ID" :`, r.ID)
    98  	fmt.Println(`"TTL" :`, r.TTL)
    99  }
   100  
   101  func (p *fieldsPrinter) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) {
   102  	p.hdr(r.Header)
   103  }
   104  
   105  func (p *fieldsPrinter) KeepAlive(r v3.LeaseKeepAliveResponse) {
   106  	p.hdr(r.ResponseHeader)
   107  	fmt.Println(`"ID" :`, r.ID)
   108  	fmt.Println(`"TTL" :`, r.TTL)
   109  }
   110  
   111  func (p *fieldsPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) {
   112  	p.hdr(r.ResponseHeader)
   113  	fmt.Println(`"ID" :`, r.ID)
   114  	fmt.Println(`"TTL" :`, r.TTL)
   115  	fmt.Println(`"GrantedTTL" :`, r.GrantedTTL)
   116  	for _, k := range r.Keys {
   117  		fmt.Printf("\"Key\" : %q\n", string(k))
   118  	}
   119  }
   120  
   121  func (p *fieldsPrinter) Leases(r v3.LeaseLeasesResponse) {
   122  	p.hdr(r.ResponseHeader)
   123  	for _, item := range r.Leases {
   124  		fmt.Println(`"ID" :`, item.ID)
   125  	}
   126  }
   127  
   128  func (p *fieldsPrinter) MemberList(r v3.MemberListResponse) {
   129  	p.hdr(r.Header)
   130  	for _, m := range r.Members {
   131  		fmt.Println(`"ID" :`, m.ID)
   132  		fmt.Printf("\"Name\" : %q\n", m.Name)
   133  		for _, u := range m.PeerURLs {
   134  			fmt.Printf("\"PeerURL\" : %q\n", u)
   135  		}
   136  		for _, u := range m.ClientURLs {
   137  			fmt.Printf("\"ClientURL\" : %q\n", u)
   138  		}
   139  		fmt.Println()
   140  	}
   141  }
   142  
   143  func (p *fieldsPrinter) EndpointHealth(hs []epHealth) {
   144  	for _, h := range hs {
   145  		fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
   146  		fmt.Println(`"Health" :`, h.Health)
   147  		fmt.Println(`"Took" :`, h.Took)
   148  		fmt.Println(`"Error" :`, h.Error)
   149  		fmt.Println()
   150  	}
   151  }
   152  
   153  func (p *fieldsPrinter) EndpointStatus(eps []epStatus) {
   154  	for _, ep := range eps {
   155  		p.hdr(ep.Resp.Header)
   156  		fmt.Printf("\"Version\" : %q\n", ep.Resp.Version)
   157  		fmt.Println(`"DBSize" :`, ep.Resp.DbSize)
   158  		fmt.Println(`"Leader" :`, ep.Resp.Leader)
   159  		fmt.Println(`"RaftIndex" :`, ep.Resp.RaftIndex)
   160  		fmt.Println(`"RaftTerm" :`, ep.Resp.RaftTerm)
   161  		fmt.Printf("\"Endpoint\" : %q\n", ep.Ep)
   162  		fmt.Println()
   163  	}
   164  }
   165  
   166  func (p *fieldsPrinter) EndpointHashKV(hs []epHashKV) {
   167  	for _, h := range hs {
   168  		p.hdr(h.Resp.Header)
   169  		fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
   170  		fmt.Println(`"Hash" :`, h.Resp.Hash)
   171  		fmt.Println()
   172  	}
   173  }
   174  
   175  func (p *fieldsPrinter) Alarm(r v3.AlarmResponse) {
   176  	p.hdr(r.Header)
   177  	for _, a := range r.Alarms {
   178  		fmt.Println(`"MemberID" :`, a.MemberID)
   179  		fmt.Println(`"AlarmType" :`, a.Alarm)
   180  		fmt.Println()
   181  	}
   182  }
   183  
   184  func (p *fieldsPrinter) DBStatus(r dbstatus) {
   185  	fmt.Println(`"Hash" :`, r.Hash)
   186  	fmt.Println(`"Revision" :`, r.Revision)
   187  	fmt.Println(`"Keys" :`, r.TotalKey)
   188  	fmt.Println(`"Size" :`, r.TotalSize)
   189  }
   190  
   191  func (p *fieldsPrinter) RoleAdd(role string, r v3.AuthRoleAddResponse) { p.hdr(r.Header) }
   192  func (p *fieldsPrinter) RoleGet(role string, r v3.AuthRoleGetResponse) {
   193  	p.hdr(r.Header)
   194  	for _, p := range r.Perm {
   195  		fmt.Println(`"PermType" : `, p.PermType.String())
   196  		fmt.Printf("\"Key\" : %q\n", string(p.Key))
   197  		fmt.Printf("\"RangeEnd\" : %q\n", string(p.RangeEnd))
   198  	}
   199  }
   200  func (p *fieldsPrinter) RoleDelete(role string, r v3.AuthRoleDeleteResponse) { p.hdr(r.Header) }
   201  func (p *fieldsPrinter) RoleList(r v3.AuthRoleListResponse) {
   202  	p.hdr(r.Header)
   203  	fmt.Printf(`"Roles" :`)
   204  	for _, r := range r.Roles {
   205  		fmt.Printf(" %q", r)
   206  	}
   207  	fmt.Println()
   208  }
   209  func (p *fieldsPrinter) RoleGrantPermission(role string, r v3.AuthRoleGrantPermissionResponse) {
   210  	p.hdr(r.Header)
   211  }
   212  func (p *fieldsPrinter) RoleRevokePermission(role string, key string, end string, r v3.AuthRoleRevokePermissionResponse) {
   213  	p.hdr(r.Header)
   214  }
   215  func (p *fieldsPrinter) UserAdd(user string, r v3.AuthUserAddResponse)          { p.hdr(r.Header) }
   216  func (p *fieldsPrinter) UserChangePassword(r v3.AuthUserChangePasswordResponse) { p.hdr(r.Header) }
   217  func (p *fieldsPrinter) UserGrantRole(user string, role string, r v3.AuthUserGrantRoleResponse) {
   218  	p.hdr(r.Header)
   219  }
   220  func (p *fieldsPrinter) UserRevokeRole(user string, role string, r v3.AuthUserRevokeRoleResponse) {
   221  	p.hdr(r.Header)
   222  }
   223  func (p *fieldsPrinter) UserDelete(user string, r v3.AuthUserDeleteResponse) { p.hdr(r.Header) }