From 0350faf75d657b08dcce5913c5170ac80ba7e198 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Mon, 27 Feb 2023 15:14:41 +0100 Subject: [PATCH] return empty strings for not applicable values --- client/cmd/status.go | 49 +++++++++++++++++++++------------- client/cmd/status_test.go | 56 ++++++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/client/cmd/status.go b/client/cmd/status.go index 17c3dc092..9310b898c 100644 --- a/client/cmd/status.go +++ b/client/cmd/status.go @@ -58,7 +58,7 @@ type statusOutputOverview struct { SignalState signalStateOutput `json:"signal" yaml:"signal"` IP string `json:"ip" yaml:"ip"` PubKey string `json:"publicKey" yaml:"publicKey"` - KernelInterface string `json:"interfaceType" yaml:"interfaceType"` + KernelInterface bool `json:"usesKernelInterface" yaml:"usesKernelInterface"` FQDN string `json:"domain" yaml:"domain"` } @@ -205,15 +205,6 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv peersOverview := mapPeers(resp.GetFullStatus().GetPeers()) - interfaceTypeString := "Userspace" - interfaceIP := pbFullStatus.GetLocalPeerState().GetIP() - if pbFullStatus.LocalPeerState.KernelInterface { - interfaceTypeString = "Kernel" - } else if pbFullStatus.LocalPeerState.IP == "" { - interfaceTypeString = "N/A" - interfaceIP = "N/A" - } - overview := statusOutputOverview{ Peers: peersOverview, CliVersion: system.NetbirdVersion(), @@ -221,9 +212,9 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv DaemonStatus: resp.GetStatus(), ManagementState: managementOverview, SignalState: signalOverview, - IP: interfaceIP, + IP: pbFullStatus.GetLocalPeerState().GetIP(), PubKey: pbFullStatus.GetLocalPeerState().GetPubKey(), - KernelInterface: interfaceTypeString, + KernelInterface: pbFullStatus.GetLocalPeerState().GetKernelInterface(), FQDN: pbFullStatus.GetLocalPeerState().GetFqdn(), } @@ -232,9 +223,9 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv func mapPeers(peers []*proto.PeerState) peersStateOutput { var peersStateDetail []peerStateDetailOutput - localICE := "-" - remoteICE := "-" - connType := "-" + localICE := "" + remoteICE := "" + connType := "" peersConnected := 0 for _, pbPeerState := range peers { isPeerConnected := pbPeerState.ConnStatus == peer.StatusConnected.String() @@ -330,6 +321,15 @@ func parseGeneralSummary(overview statusOutputOverview, showUrl bool) string { } } + interfaceTypeString := "Userspace" + interfaceIP := overview.IP + if overview.KernelInterface { + interfaceTypeString = "Kernel" + } else if overview.IP == "" { + interfaceTypeString = "N/A" + interfaceIP = "N/A" + } + peersCountString := fmt.Sprintf("%d/%d Connected", overview.Peers.Connected, overview.Peers.Total) summary := fmt.Sprintf( @@ -348,8 +348,8 @@ func parseGeneralSummary(overview statusOutputOverview, showUrl bool) string { managementConnString, signalConnString, overview.FQDN, - overview.IP, - overview.KernelInterface, + interfaceIP, + interfaceTypeString, peersCountString, ) return summary @@ -374,6 +374,17 @@ func parsePeers(peers peersStateOutput) string { ) for _, peerState := range peers.Details { + + localICE := "-" + if peerState.LocalIceCandidateType != "" { + localICE = peerState.LocalIceCandidateType + } + + remoteICE := "-" + if peerState.RemoteIceCandidateType != "" { + remoteICE = peerState.RemoteIceCandidateType + } + peerString := fmt.Sprintf( "\n %s:\n"+ " NetBird IP: %s\n"+ @@ -390,8 +401,8 @@ func parsePeers(peers peersStateOutput) string { peerState.ConnStatus, peerState.ConnType, peerState.Direct, - peerState.LocalIceCandidateType, - peerState.RemoteIceCandidateType, + localICE, + remoteICE, peerState.ConnStatusUpdate.Format("2006-01-02 15:04:05"), ) diff --git a/client/cmd/status_test.go b/client/cmd/status_test.go index a271554ac..b33d07c49 100644 --- a/client/cmd/status_test.go +++ b/client/cmd/status_test.go @@ -23,8 +23,8 @@ var resp = &proto.StatusResponse{ ConnStatusUpdate: timestamppb.New(time.Date(2001, time.Month(1), 1, 1, 1, 1, 0, time.UTC)), Relayed: false, Direct: true, - LocalIceCandidateType: "-", - RemoteIceCandidateType: "-", + LocalIceCandidateType: "", + RemoteIceCandidateType: "", }, { IP: "192.168.178.102", @@ -34,8 +34,8 @@ var resp = &proto.StatusResponse{ ConnStatusUpdate: timestamppb.New(time.Date(2002, time.Month(2), 2, 2, 2, 2, 0, time.UTC)), Relayed: true, Direct: false, - LocalIceCandidateType: "-", - RemoteIceCandidateType: "-", + LocalIceCandidateType: "relay", + RemoteIceCandidateType: "prflx", }, }, ManagementState: &proto.ManagementState{ @@ -69,8 +69,8 @@ var overview = statusOutputOverview{ ConnStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC), ConnType: "P2P", Direct: true, - LocalIceCandidateType: "-", - RemoteIceCandidateType: "-", + LocalIceCandidateType: "", + RemoteIceCandidateType: "", }, { IP: "192.168.178.102", @@ -80,8 +80,8 @@ var overview = statusOutputOverview{ ConnStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC), ConnType: "Relayed", Direct: false, - LocalIceCandidateType: "-", - RemoteIceCandidateType: "-", + LocalIceCandidateType: "relay", + RemoteIceCandidateType: "prflx", }, }, }, @@ -98,7 +98,7 @@ var overview = statusOutputOverview{ }, IP: "192.168.178.100/16", PubKey: "Some-Pub-Key", - KernelInterface: "Kernel", + KernelInterface: true, FQDN: "some-localhost.awesome-domain.com", } @@ -108,6 +108,18 @@ func TestConversionFromFullStatusToOutputOverview(t *testing.T) { assert.Equal(t, overview, convertedResult) } +func TestForErrorOnMultipleOutputFlags(t *testing.T) { + rootCmd.SetArgs([]string{ + "status", + "--yaml", + "--json", + }) + if err := rootCmd.Execute(); err != nil { + return + } + t.Errorf("expected error while running status command with 2 output flags") +} + func TestSortingOfPeers(t *testing.T) { peers := []peerStateDetailOutput{ { @@ -147,12 +159,12 @@ func TestParsingToJson(t *testing.T) { "\"ip\":\"192.168.178.101\"," + "\"publicKey\":\"Pubkey1\"," + "\"fqdn\":\"peer-1.awesome-domain.com\"," + - "\"connectionStatus\":\"Connected\"" + - ",\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," + + "\"connectionStatus\":\"Connected\"," + + "\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," + "\"connectionType\":\"P2P\"," + "\"direct\":true," + - "\"localIceCandidateType\":\"-\"," + - "\"remoteIceCandidateType\":\"-\"" + + "\"localIceCandidateType\":\"\"," + + "\"remoteIceCandidateType\":\"\"" + "}," + "{" + "\"ip\":\"192.168.178.102\"," + @@ -162,8 +174,8 @@ func TestParsingToJson(t *testing.T) { "\"connectionStatusUpdate\":\"2002-02-02T02:02:02Z\"," + "\"connectionType\":\"Relayed\"," + "\"direct\":false," + - "\"localIceCandidateType\":\"-\"," + - "\"remoteIceCandidateType\":\"-\"" + + "\"localIceCandidateType\":\"relay\"," + + "\"remoteIceCandidateType\":\"prflx\"" + "}" + "]" + "}," + @@ -182,7 +194,7 @@ func TestParsingToJson(t *testing.T) { "}," + "\"ip\":\"192.168.178.100/16\"," + "\"publicKey\":\"Some-Pub-Key\"," + - "\"interfaceType\":\"Kernel\"," + + "\"usesKernelInterface\":true," + "\"domain\":\"some-localhost.awesome-domain.com\"" + "}" // @formatter:on @@ -204,8 +216,8 @@ func TestParsingToYaml(t *testing.T) { " connectionStatusUpdate: 2001-01-01T01:01:01Z\n" + " connectionType: P2P\n" + " direct: true\n" + - " localIceCandidateType: '-'\n" + - " remoteIceCandidateType: '-'\n" + + " localIceCandidateType: \"\"\n" + + " remoteIceCandidateType: \"\"\n" + " - ip: 192.168.178.102\n" + " publicKey: Pubkey2\n" + " fqdn: peer-2.awesome-domain.com\n" + @@ -213,8 +225,8 @@ func TestParsingToYaml(t *testing.T) { " connectionStatusUpdate: 2002-02-02T02:02:02Z\n" + " connectionType: Relayed\n" + " direct: false\n" + - " localIceCandidateType: '-'\n" + - " remoteIceCandidateType: '-'\n" + + " localIceCandidateType: relay\n" + + " remoteIceCandidateType: prflx\n" + "cliVersion: development\n" + "daemonVersion: 0.14.1\n" + "daemonStatus: Connected\n" + @@ -226,7 +238,7 @@ func TestParsingToYaml(t *testing.T) { " connected: true\n" + "ip: 192.168.178.100/16\n" + "publicKey: Some-Pub-Key\n" + - "interfaceType: Kernel\n" + + "usesKernelInterface: true\n" + "domain: some-localhost.awesome-domain.com\n" assert.Equal(t, expectedYaml, yaml) @@ -253,7 +265,7 @@ func TestParsingToDetail(t *testing.T) { " -- detail --\n" + " Connection type: Relayed\n" + " Direct: false\n" + - " ICE candidate (Local/Remote): -/-\n" + + " ICE candidate (Local/Remote): relay/prflx\n" + " Last connection update: 2002-02-02 02:02:02\n" + "\n" + "Daemon version: 0.14.1\n" +