From 23610db7273d0ed823ccc1618717bdfec710f460 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Mon, 27 Feb 2023 17:06:20 +0100 Subject: [PATCH] apply first set of review comments (mostly reorder and naming) --- client/cmd/status.go | 89 ++++++++++++++-------------- client/cmd/status_test.go | 118 +++++++++++++++++++++----------------- 2 files changed, 112 insertions(+), 95 deletions(-) diff --git a/client/cmd/status.go b/client/cmd/status.go index cb98aabf2..44ba24251 100644 --- a/client/cmd/status.go +++ b/client/cmd/status.go @@ -22,15 +22,14 @@ import ( ) type peerStateDetailOutput struct { - IP string `json:"ip" yaml:"ip"` - PubKey string `json:"publicKey" yaml:"publicKey"` - FQDN string `json:"fqdn" yaml:"fqdn"` - ConnStatus string `json:"connectionStatus" yaml:"connectionStatus"` - ConnStatusUpdate time.Time `json:"connectionStatusUpdate" yaml:"connectionStatusUpdate"` - ConnType string `json:"connectionType" yaml:"connectionType"` - Direct bool `json:"direct" yaml:"direct"` - LocalIceCandidateType string `json:"localIceCandidateType" yaml:"localIceCandidateType"` - RemoteIceCandidateType string `json:"remoteIceCandidateType" yaml:"remoteIceCandidateType"` + FQDN string `json:"fqdn" yaml:"fqdn"` + IP string `json:"netbirdIp" yaml:"netbirdIp"` + PubKey string `json:"publicKey" yaml:"publicKey"` + Status string `json:"status" yaml:"status"` + LastStatusUpdate time.Time `json:"lastStatusUpdate" yaml:"lastStatusUpdate"` + ConnType string `json:"connectionType" yaml:"connectionType"` + Direct bool `json:"direct" yaml:"direct"` + IceCandidateType iceCandidateType `json:"iceCandidateType" yaml:"iceCandidateType"` } type peersStateOutput struct { @@ -49,6 +48,11 @@ type managementStateOutput struct { Connected bool `json:"connected" yaml:"connected"` } +type iceCandidateType struct { + Local string `json:"local" yaml:"local"` + Remote string `json:"remote" yaml:"remote"` +} + type statusOutputOverview struct { Peers peersStateOutput `json:"peers" yaml:"peers"` CliVersion string `json:"cliVersion" yaml:"cliVersion"` @@ -56,10 +60,10 @@ type statusOutputOverview struct { DaemonStatus string `json:"daemonStatus" yaml:"daemonStatus"` ManagementState managementStateOutput `json:"management" yaml:"management"` SignalState signalStateOutput `json:"signal" yaml:"signal"` - IP string `json:"ip" yaml:"ip"` + IP string `json:"netbirdIp" yaml:"netbirdIp"` PubKey string `json:"publicKey" yaml:"publicKey"` KernelInterface bool `json:"usesKernelInterface" yaml:"usesKernelInterface"` - FQDN string `json:"domain" yaml:"domain"` + FQDN string `json:"fqdn" yaml:"fqdn"` } var ( @@ -128,23 +132,22 @@ func statusFunc(cmd *cobra.Command, args []string) error { return nil } - statusOutputOverview := convertToStatusOutputOverview(resp) + outputInformationHolder := convertToStatusOutputOverview(resp) statusOutputString := "" - if detailFlag { - statusOutputString = parseToFullDetailSummary(statusOutputOverview) - } else if jsonFlag { - statusOutputString, err = parseToJSON(statusOutputOverview) - if err != nil { - return err - } - } else if yamlFlag { - statusOutputString, err = parseToYAML(statusOutputOverview) - if err != nil { - return err - } - } else { - statusOutputString = parseGeneralSummary(statusOutputOverview, false) + switch { + case detailFlag: + statusOutputString = parseToFullDetailSummary(outputInformationHolder) + case jsonFlag: + statusOutputString, err = parseToJSON(outputInformationHolder) + case yamlFlag: + statusOutputString, err = parseToYAML(outputInformationHolder) + default: + statusOutputString = parseGeneralSummary(outputInformationHolder, false) + } + + if err != nil { + return err } cmd.Print(statusOutputString) @@ -245,15 +248,17 @@ func mapPeers(peers []*proto.PeerState) peersStateOutput { timeLocal := pbPeerState.GetConnStatusUpdate().AsTime().Local() peerState := peerStateDetailOutput{ - IP: pbPeerState.GetIP(), - PubKey: pbPeerState.GetPubKey(), - ConnStatus: pbPeerState.GetConnStatus(), - ConnStatusUpdate: timeLocal.UTC(), - ConnType: connType, - Direct: pbPeerState.GetDirect(), - LocalIceCandidateType: localICE, - RemoteIceCandidateType: remoteICE, - FQDN: pbPeerState.GetFqdn(), + IP: pbPeerState.GetIP(), + PubKey: pbPeerState.GetPubKey(), + Status: pbPeerState.GetConnStatus(), + LastStatusUpdate: timeLocal.UTC(), + ConnType: connType, + Direct: pbPeerState.GetDirect(), + IceCandidateType: iceCandidateType{ + Local: localICE, + Remote: remoteICE, + }, + FQDN: pbPeerState.GetFqdn(), } peersStateDetail = append(peersStateDetail, peerState) @@ -338,7 +343,7 @@ func parseGeneralSummary(overview statusOutputOverview, showURL bool) string { "%s"+ // daemon status "Management: %s\n"+ "Signal: %s\n"+ - "Domain: %s\n"+ + "FQDN: %s\n"+ "NetBird IP: %s\n"+ "Interface type: %s\n"+ "Peers count: %s\n", @@ -376,13 +381,13 @@ func parsePeers(peers peersStateOutput) string { for _, peerState := range peers.Details { localICE := "-" - if peerState.LocalIceCandidateType != "" { - localICE = peerState.LocalIceCandidateType + if peerState.IceCandidateType.Local != "" { + localICE = peerState.IceCandidateType.Local } remoteICE := "-" - if peerState.RemoteIceCandidateType != "" { - remoteICE = peerState.RemoteIceCandidateType + if peerState.IceCandidateType.Remote != "" { + remoteICE = peerState.IceCandidateType.Remote } peerString := fmt.Sprintf( @@ -398,12 +403,12 @@ func parsePeers(peers peersStateOutput) string { peerState.FQDN, peerState.IP, peerState.PubKey, - peerState.ConnStatus, + peerState.Status, peerState.ConnType, peerState.Direct, localICE, remoteICE, - peerState.ConnStatusUpdate.Format("2006-01-02 15:04:05"), + peerState.LastStatusUpdate.Format("2006-01-02 15:04:05"), ) peersString = peersString + peerString diff --git a/client/cmd/status_test.go b/client/cmd/status_test.go index ff62c438e..348ba537a 100644 --- a/client/cmd/status_test.go +++ b/client/cmd/status_test.go @@ -62,26 +62,30 @@ var overview = statusOutputOverview{ Connected: 2, Details: []peerStateDetailOutput{ { - IP: "192.168.178.101", - PubKey: "Pubkey1", - FQDN: "peer-1.awesome-domain.com", - ConnStatus: "Connected", - ConnStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC), - ConnType: "P2P", - Direct: true, - LocalIceCandidateType: "", - RemoteIceCandidateType: "", + IP: "192.168.178.101", + PubKey: "Pubkey1", + FQDN: "peer-1.awesome-domain.com", + Status: "Connected", + LastStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC), + ConnType: "P2P", + Direct: true, + IceCandidateType: iceCandidateType{ + Local: "", + Remote: "", + }, }, { - IP: "192.168.178.102", - PubKey: "Pubkey2", - FQDN: "peer-2.awesome-domain.com", - ConnStatus: "Connected", - ConnStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC), - ConnType: "Relayed", - Direct: false, - LocalIceCandidateType: "relay", - RemoteIceCandidateType: "prflx", + IP: "192.168.178.102", + PubKey: "Pubkey2", + FQDN: "peer-2.awesome-domain.com", + Status: "Connected", + LastStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC), + ConnType: "Relayed", + Direct: false, + IceCandidateType: iceCandidateType{ + Local: "relay", + Remote: "prflx", + }, }, }, }, @@ -147,35 +151,41 @@ func TestSortingOfPeers(t *testing.T) { func TestParsingToJSON(t *testing.T) { json, _ := parseToJSON(overview) - // @formatter:off - expectedJSON := "{" + - "\"peers\":" + + //@formatter:off + expectedJSON := "{\"" + + "peers\":" + "{" + "\"total\":2," + "\"connected\":2," + "\"details\":" + "[" + "{" + - "\"ip\":\"192.168.178.101\"," + - "\"publicKey\":\"Pubkey1\"," + "\"fqdn\":\"peer-1.awesome-domain.com\"," + - "\"connectionStatus\":\"Connected\"," + - "\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," + + "\"netbirdIp\":\"192.168.178.101\"," + + "\"publicKey\":\"Pubkey1\"," + + "\"status\":\"Connected\"," + + "\"lastStatusUpdate\":\"2001-01-01T01:01:01Z\"," + "\"connectionType\":\"P2P\"," + "\"direct\":true," + - "\"localIceCandidateType\":\"\"," + - "\"remoteIceCandidateType\":\"\"" + + "\"iceCandidateType\":" + + "{" + + "\"local\":\"\"," + + "\"remote\":\"\"" + + "}" + "}," + "{" + - "\"ip\":\"192.168.178.102\"," + - "\"publicKey\":\"Pubkey2\"," + "\"fqdn\":\"peer-2.awesome-domain.com\"," + - "\"connectionStatus\":\"Connected\"," + - "\"connectionStatusUpdate\":\"2002-02-02T02:02:02Z\"," + + "\"netbirdIp\":\"192.168.178.102\"," + + "\"publicKey\":\"Pubkey2\"," + + "\"status\":\"Connected\"," + + "\"lastStatusUpdate\":\"2002-02-02T02:02:02Z\"," + "\"connectionType\":\"Relayed\"," + "\"direct\":false," + - "\"localIceCandidateType\":\"relay\"," + - "\"remoteIceCandidateType\":\"prflx\"" + + "\"iceCandidateType\":" + + "{" + + "\"local\":\"relay\"," + + "\"remote\":\"prflx\"" + + "}" + "}" + "]" + "}," + @@ -188,14 +198,14 @@ func TestParsingToJSON(t *testing.T) { "\"connected\":true" + "}," + "\"signal\":" + - "{" + - "\"url\":\"my-awesome-signal.com:443\"," + + "{\"" + + "url\":\"my-awesome-signal.com:443\"," + "\"connected\":true" + "}," + - "\"ip\":\"192.168.178.100/16\"," + + "\"netbirdIp\":\"192.168.178.100/16\"," + "\"publicKey\":\"Some-Pub-Key\"," + "\"usesKernelInterface\":true," + - "\"domain\":\"some-localhost.awesome-domain.com\"" + + "\"fqdn\":\"some-localhost.awesome-domain.com\"" + "}" // @formatter:on @@ -209,24 +219,26 @@ func TestParsingToYAML(t *testing.T) { " total: 2\n" + " connected: 2\n" + " details:\n" + - " - ip: 192.168.178.101\n" + + " - fqdn: peer-1.awesome-domain.com\n" + + " netbirdIp: 192.168.178.101\n" + " publicKey: Pubkey1\n" + - " fqdn: peer-1.awesome-domain.com\n" + - " connectionStatus: Connected\n" + - " connectionStatusUpdate: 2001-01-01T01:01:01Z\n" + + " status: Connected\n" + + " lastStatusUpdate: 2001-01-01T01:01:01Z\n" + " connectionType: P2P\n" + " direct: true\n" + - " localIceCandidateType: \"\"\n" + - " remoteIceCandidateType: \"\"\n" + - " - ip: 192.168.178.102\n" + + " iceCandidateType:\n" + + " local: \"\"\n" + + " remote: \"\"\n" + + " - fqdn: peer-2.awesome-domain.com\n" + + " netbirdIp: 192.168.178.102\n" + " publicKey: Pubkey2\n" + - " fqdn: peer-2.awesome-domain.com\n" + - " connectionStatus: Connected\n" + - " connectionStatusUpdate: 2002-02-02T02:02:02Z\n" + + " status: Connected\n" + + " lastStatusUpdate: 2002-02-02T02:02:02Z\n" + " connectionType: Relayed\n" + " direct: false\n" + - " localIceCandidateType: relay\n" + - " remoteIceCandidateType: prflx\n" + + " iceCandidateType:\n" + + " local: relay\n" + + " remote: prflx\n" + "cliVersion: development\n" + "daemonVersion: 0.14.1\n" + "daemonStatus: Connected\n" + @@ -236,10 +248,10 @@ func TestParsingToYAML(t *testing.T) { "signal:\n" + " url: my-awesome-signal.com:443\n" + " connected: true\n" + - "ip: 192.168.178.100/16\n" + + "netbirdIp: 192.168.178.100/16\n" + "publicKey: Some-Pub-Key\n" + "usesKernelInterface: true\n" + - "domain: some-localhost.awesome-domain.com\n" + "fqdn: some-localhost.awesome-domain.com\n" assert.Equal(t, expectedYAML, yaml) } @@ -272,7 +284,7 @@ func TestParsingToDetail(t *testing.T) { "CLI version: development\n" + "ConnectedManagement: Connected to my-awesome-management.com:443\n" + "Signal: Connected to my-awesome-signal.com:443\n" + - "Domain: some-localhost.awesome-domain.com\n" + + "FQDN: some-localhost.awesome-domain.com\n" + "NetBird IP: 192.168.178.100/16\n" + "Interface type: Kernel\n" + "Peers count: 2/2 Connected\n" @@ -287,7 +299,7 @@ func TestParsingToShortVersion(t *testing.T) { "CLI version: development\n" + "ConnectedManagement: Connected\n" + "Signal: Connected\n" + - "Domain: some-localhost.awesome-domain.com\n" + + "FQDN: some-localhost.awesome-domain.com\n" + "NetBird IP: 192.168.178.100/16\n" + "Interface type: Kernel\n" + "Peers count: 2/2 Connected\n"