return empty strings for not applicable values

This commit is contained in:
Pascal Fischer 2023-02-27 15:14:41 +01:00
parent 8276e0908a
commit 0350faf75d
2 changed files with 64 additions and 41 deletions

View File

@ -58,7 +58,7 @@ type statusOutputOverview struct {
SignalState signalStateOutput `json:"signal" yaml:"signal"` SignalState signalStateOutput `json:"signal" yaml:"signal"`
IP string `json:"ip" yaml:"ip"` IP string `json:"ip" yaml:"ip"`
PubKey string `json:"publicKey" yaml:"publicKey"` PubKey string `json:"publicKey" yaml:"publicKey"`
KernelInterface string `json:"interfaceType" yaml:"interfaceType"` KernelInterface bool `json:"usesKernelInterface" yaml:"usesKernelInterface"`
FQDN string `json:"domain" yaml:"domain"` FQDN string `json:"domain" yaml:"domain"`
} }
@ -205,15 +205,6 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv
peersOverview := mapPeers(resp.GetFullStatus().GetPeers()) 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{ overview := statusOutputOverview{
Peers: peersOverview, Peers: peersOverview,
CliVersion: system.NetbirdVersion(), CliVersion: system.NetbirdVersion(),
@ -221,9 +212,9 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv
DaemonStatus: resp.GetStatus(), DaemonStatus: resp.GetStatus(),
ManagementState: managementOverview, ManagementState: managementOverview,
SignalState: signalOverview, SignalState: signalOverview,
IP: interfaceIP, IP: pbFullStatus.GetLocalPeerState().GetIP(),
PubKey: pbFullStatus.GetLocalPeerState().GetPubKey(), PubKey: pbFullStatus.GetLocalPeerState().GetPubKey(),
KernelInterface: interfaceTypeString, KernelInterface: pbFullStatus.GetLocalPeerState().GetKernelInterface(),
FQDN: pbFullStatus.GetLocalPeerState().GetFqdn(), FQDN: pbFullStatus.GetLocalPeerState().GetFqdn(),
} }
@ -232,9 +223,9 @@ func convertToStatusOutputOverview(resp *proto.StatusResponse) statusOutputOverv
func mapPeers(peers []*proto.PeerState) peersStateOutput { func mapPeers(peers []*proto.PeerState) peersStateOutput {
var peersStateDetail []peerStateDetailOutput var peersStateDetail []peerStateDetailOutput
localICE := "-" localICE := ""
remoteICE := "-" remoteICE := ""
connType := "-" connType := ""
peersConnected := 0 peersConnected := 0
for _, pbPeerState := range peers { for _, pbPeerState := range peers {
isPeerConnected := pbPeerState.ConnStatus == peer.StatusConnected.String() 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) peersCountString := fmt.Sprintf("%d/%d Connected", overview.Peers.Connected, overview.Peers.Total)
summary := fmt.Sprintf( summary := fmt.Sprintf(
@ -348,8 +348,8 @@ func parseGeneralSummary(overview statusOutputOverview, showUrl bool) string {
managementConnString, managementConnString,
signalConnString, signalConnString,
overview.FQDN, overview.FQDN,
overview.IP, interfaceIP,
overview.KernelInterface, interfaceTypeString,
peersCountString, peersCountString,
) )
return summary return summary
@ -374,6 +374,17 @@ func parsePeers(peers peersStateOutput) string {
) )
for _, peerState := range peers.Details { for _, peerState := range peers.Details {
localICE := "-"
if peerState.LocalIceCandidateType != "" {
localICE = peerState.LocalIceCandidateType
}
remoteICE := "-"
if peerState.RemoteIceCandidateType != "" {
remoteICE = peerState.RemoteIceCandidateType
}
peerString := fmt.Sprintf( peerString := fmt.Sprintf(
"\n %s:\n"+ "\n %s:\n"+
" NetBird IP: %s\n"+ " NetBird IP: %s\n"+
@ -390,8 +401,8 @@ func parsePeers(peers peersStateOutput) string {
peerState.ConnStatus, peerState.ConnStatus,
peerState.ConnType, peerState.ConnType,
peerState.Direct, peerState.Direct,
peerState.LocalIceCandidateType, localICE,
peerState.RemoteIceCandidateType, remoteICE,
peerState.ConnStatusUpdate.Format("2006-01-02 15:04:05"), peerState.ConnStatusUpdate.Format("2006-01-02 15:04:05"),
) )

View File

@ -23,8 +23,8 @@ var resp = &proto.StatusResponse{
ConnStatusUpdate: timestamppb.New(time.Date(2001, time.Month(1), 1, 1, 1, 1, 0, time.UTC)), ConnStatusUpdate: timestamppb.New(time.Date(2001, time.Month(1), 1, 1, 1, 1, 0, time.UTC)),
Relayed: false, Relayed: false,
Direct: true, Direct: true,
LocalIceCandidateType: "-", LocalIceCandidateType: "",
RemoteIceCandidateType: "-", RemoteIceCandidateType: "",
}, },
{ {
IP: "192.168.178.102", 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)), ConnStatusUpdate: timestamppb.New(time.Date(2002, time.Month(2), 2, 2, 2, 2, 0, time.UTC)),
Relayed: true, Relayed: true,
Direct: false, Direct: false,
LocalIceCandidateType: "-", LocalIceCandidateType: "relay",
RemoteIceCandidateType: "-", RemoteIceCandidateType: "prflx",
}, },
}, },
ManagementState: &proto.ManagementState{ ManagementState: &proto.ManagementState{
@ -69,8 +69,8 @@ var overview = statusOutputOverview{
ConnStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC), ConnStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC),
ConnType: "P2P", ConnType: "P2P",
Direct: true, Direct: true,
LocalIceCandidateType: "-", LocalIceCandidateType: "",
RemoteIceCandidateType: "-", RemoteIceCandidateType: "",
}, },
{ {
IP: "192.168.178.102", IP: "192.168.178.102",
@ -80,8 +80,8 @@ var overview = statusOutputOverview{
ConnStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC), ConnStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC),
ConnType: "Relayed", ConnType: "Relayed",
Direct: false, Direct: false,
LocalIceCandidateType: "-", LocalIceCandidateType: "relay",
RemoteIceCandidateType: "-", RemoteIceCandidateType: "prflx",
}, },
}, },
}, },
@ -98,7 +98,7 @@ var overview = statusOutputOverview{
}, },
IP: "192.168.178.100/16", IP: "192.168.178.100/16",
PubKey: "Some-Pub-Key", PubKey: "Some-Pub-Key",
KernelInterface: "Kernel", KernelInterface: true,
FQDN: "some-localhost.awesome-domain.com", FQDN: "some-localhost.awesome-domain.com",
} }
@ -108,6 +108,18 @@ func TestConversionFromFullStatusToOutputOverview(t *testing.T) {
assert.Equal(t, overview, convertedResult) 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) { func TestSortingOfPeers(t *testing.T) {
peers := []peerStateDetailOutput{ peers := []peerStateDetailOutput{
{ {
@ -147,12 +159,12 @@ func TestParsingToJson(t *testing.T) {
"\"ip\":\"192.168.178.101\"," + "\"ip\":\"192.168.178.101\"," +
"\"publicKey\":\"Pubkey1\"," + "\"publicKey\":\"Pubkey1\"," +
"\"fqdn\":\"peer-1.awesome-domain.com\"," + "\"fqdn\":\"peer-1.awesome-domain.com\"," +
"\"connectionStatus\":\"Connected\"" + "\"connectionStatus\":\"Connected\"," +
",\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," + "\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," +
"\"connectionType\":\"P2P\"," + "\"connectionType\":\"P2P\"," +
"\"direct\":true," + "\"direct\":true," +
"\"localIceCandidateType\":\"-\"," + "\"localIceCandidateType\":\"\"," +
"\"remoteIceCandidateType\":\"-\"" + "\"remoteIceCandidateType\":\"\"" +
"}," + "}," +
"{" + "{" +
"\"ip\":\"192.168.178.102\"," + "\"ip\":\"192.168.178.102\"," +
@ -162,8 +174,8 @@ func TestParsingToJson(t *testing.T) {
"\"connectionStatusUpdate\":\"2002-02-02T02:02:02Z\"," + "\"connectionStatusUpdate\":\"2002-02-02T02:02:02Z\"," +
"\"connectionType\":\"Relayed\"," + "\"connectionType\":\"Relayed\"," +
"\"direct\":false," + "\"direct\":false," +
"\"localIceCandidateType\":\"-\"," + "\"localIceCandidateType\":\"relay\"," +
"\"remoteIceCandidateType\":\"-\"" + "\"remoteIceCandidateType\":\"prflx\"" +
"}" + "}" +
"]" + "]" +
"}," + "}," +
@ -182,7 +194,7 @@ func TestParsingToJson(t *testing.T) {
"}," + "}," +
"\"ip\":\"192.168.178.100/16\"," + "\"ip\":\"192.168.178.100/16\"," +
"\"publicKey\":\"Some-Pub-Key\"," + "\"publicKey\":\"Some-Pub-Key\"," +
"\"interfaceType\":\"Kernel\"," + "\"usesKernelInterface\":true," +
"\"domain\":\"some-localhost.awesome-domain.com\"" + "\"domain\":\"some-localhost.awesome-domain.com\"" +
"}" "}"
// @formatter:on // @formatter:on
@ -204,8 +216,8 @@ func TestParsingToYaml(t *testing.T) {
" connectionStatusUpdate: 2001-01-01T01:01:01Z\n" + " connectionStatusUpdate: 2001-01-01T01:01:01Z\n" +
" connectionType: P2P\n" + " connectionType: P2P\n" +
" direct: true\n" + " direct: true\n" +
" localIceCandidateType: '-'\n" + " localIceCandidateType: \"\"\n" +
" remoteIceCandidateType: '-'\n" + " remoteIceCandidateType: \"\"\n" +
" - ip: 192.168.178.102\n" + " - ip: 192.168.178.102\n" +
" publicKey: Pubkey2\n" + " publicKey: Pubkey2\n" +
" fqdn: peer-2.awesome-domain.com\n" + " fqdn: peer-2.awesome-domain.com\n" +
@ -213,8 +225,8 @@ func TestParsingToYaml(t *testing.T) {
" connectionStatusUpdate: 2002-02-02T02:02:02Z\n" + " connectionStatusUpdate: 2002-02-02T02:02:02Z\n" +
" connectionType: Relayed\n" + " connectionType: Relayed\n" +
" direct: false\n" + " direct: false\n" +
" localIceCandidateType: '-'\n" + " localIceCandidateType: relay\n" +
" remoteIceCandidateType: '-'\n" + " remoteIceCandidateType: prflx\n" +
"cliVersion: development\n" + "cliVersion: development\n" +
"daemonVersion: 0.14.1\n" + "daemonVersion: 0.14.1\n" +
"daemonStatus: Connected\n" + "daemonStatus: Connected\n" +
@ -226,7 +238,7 @@ func TestParsingToYaml(t *testing.T) {
" connected: true\n" + " connected: true\n" +
"ip: 192.168.178.100/16\n" + "ip: 192.168.178.100/16\n" +
"publicKey: Some-Pub-Key\n" + "publicKey: Some-Pub-Key\n" +
"interfaceType: Kernel\n" + "usesKernelInterface: true\n" +
"domain: some-localhost.awesome-domain.com\n" "domain: some-localhost.awesome-domain.com\n"
assert.Equal(t, expectedYaml, yaml) assert.Equal(t, expectedYaml, yaml)
@ -253,7 +265,7 @@ func TestParsingToDetail(t *testing.T) {
" -- detail --\n" + " -- detail --\n" +
" Connection type: Relayed\n" + " Connection type: Relayed\n" +
" Direct: false\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" + " Last connection update: 2002-02-02 02:02:02\n" +
"\n" + "\n" +
"Daemon version: 0.14.1\n" + "Daemon version: 0.14.1\n" +