vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/cli/awk.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 "time" 24 25 "vitess.io/vitess/go/vt/logutil" 26 "vitess.io/vitess/go/vt/topo" 27 "vitess.io/vitess/go/vt/topo/topoproto" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 ) 31 32 // MarshalMapAWK returns a string representation of a string->string map in an 33 // AWK-friendly format. 34 func MarshalMapAWK(m map[string]string) string { 35 pairs := make([]string, len(m)) 36 i := 0 37 38 for k, v := range m { 39 pairs[i] = fmt.Sprintf("%v: %q", k, v) 40 41 i++ 42 } 43 44 sort.Strings(pairs) 45 46 return "[" + strings.Join(pairs, " ") + "]" 47 } 48 49 // MarshalTabletAWK marshals a tablet into an AWK-friendly line. 50 func MarshalTabletAWK(t *topodatapb.Tablet) string { 51 ti := topo.TabletInfo{ 52 Tablet: t, 53 } 54 55 keyspace := t.Keyspace 56 if keyspace == "" { 57 keyspace = "<null>" 58 } 59 60 shard := t.Shard 61 if shard == "" { 62 shard = "<null>" 63 } 64 65 mtst := "<null>" 66 // special case for old primary that hasn't been updated in the topo 67 // yet. 68 if t.PrimaryTermStartTime != nil && t.PrimaryTermStartTime.Seconds > 0 { 69 mtst = logutil.ProtoToTime(t.PrimaryTermStartTime).Format(time.RFC3339) 70 } 71 72 return fmt.Sprintf("%v %v %v %v %v %v %v %v", topoproto.TabletAliasString(t.Alias), keyspace, shard, topoproto.TabletTypeLString(t.Type), ti.Addr(), ti.MysqlAddr(), MarshalMapAWK(t.Tags), mtst) 73 }