apply first set of review comments (mostly reorder and naming)

This commit is contained in:
Pascal Fischer 2023-02-27 17:06:20 +01:00
parent 4330bfd8ca
commit 23610db727
2 changed files with 112 additions and 95 deletions

View File

@ -22,15 +22,14 @@ import (
) )
type peerStateDetailOutput struct { type peerStateDetailOutput struct {
IP string `json:"ip" yaml:"ip"` FQDN string `json:"fqdn" yaml:"fqdn"`
PubKey string `json:"publicKey" yaml:"publicKey"` IP string `json:"netbirdIp" yaml:"netbirdIp"`
FQDN string `json:"fqdn" yaml:"fqdn"` PubKey string `json:"publicKey" yaml:"publicKey"`
ConnStatus string `json:"connectionStatus" yaml:"connectionStatus"` Status string `json:"status" yaml:"status"`
ConnStatusUpdate time.Time `json:"connectionStatusUpdate" yaml:"connectionStatusUpdate"` LastStatusUpdate time.Time `json:"lastStatusUpdate" yaml:"lastStatusUpdate"`
ConnType string `json:"connectionType" yaml:"connectionType"` ConnType string `json:"connectionType" yaml:"connectionType"`
Direct bool `json:"direct" yaml:"direct"` Direct bool `json:"direct" yaml:"direct"`
LocalIceCandidateType string `json:"localIceCandidateType" yaml:"localIceCandidateType"` IceCandidateType iceCandidateType `json:"iceCandidateType" yaml:"iceCandidateType"`
RemoteIceCandidateType string `json:"remoteIceCandidateType" yaml:"remoteIceCandidateType"`
} }
type peersStateOutput struct { type peersStateOutput struct {
@ -49,6 +48,11 @@ type managementStateOutput struct {
Connected bool `json:"connected" yaml:"connected"` Connected bool `json:"connected" yaml:"connected"`
} }
type iceCandidateType struct {
Local string `json:"local" yaml:"local"`
Remote string `json:"remote" yaml:"remote"`
}
type statusOutputOverview struct { type statusOutputOverview struct {
Peers peersStateOutput `json:"peers" yaml:"peers"` Peers peersStateOutput `json:"peers" yaml:"peers"`
CliVersion string `json:"cliVersion" yaml:"cliVersion"` CliVersion string `json:"cliVersion" yaml:"cliVersion"`
@ -56,10 +60,10 @@ type statusOutputOverview struct {
DaemonStatus string `json:"daemonStatus" yaml:"daemonStatus"` DaemonStatus string `json:"daemonStatus" yaml:"daemonStatus"`
ManagementState managementStateOutput `json:"management" yaml:"management"` ManagementState managementStateOutput `json:"management" yaml:"management"`
SignalState signalStateOutput `json:"signal" yaml:"signal"` SignalState signalStateOutput `json:"signal" yaml:"signal"`
IP string `json:"ip" yaml:"ip"` IP string `json:"netbirdIp" yaml:"netbirdIp"`
PubKey string `json:"publicKey" yaml:"publicKey"` PubKey string `json:"publicKey" yaml:"publicKey"`
KernelInterface bool `json:"usesKernelInterface" yaml:"usesKernelInterface"` KernelInterface bool `json:"usesKernelInterface" yaml:"usesKernelInterface"`
FQDN string `json:"domain" yaml:"domain"` FQDN string `json:"fqdn" yaml:"fqdn"`
} }
var ( var (
@ -128,23 +132,22 @@ func statusFunc(cmd *cobra.Command, args []string) error {
return nil return nil
} }
statusOutputOverview := convertToStatusOutputOverview(resp) outputInformationHolder := convertToStatusOutputOverview(resp)
statusOutputString := "" statusOutputString := ""
if detailFlag { switch {
statusOutputString = parseToFullDetailSummary(statusOutputOverview) case detailFlag:
} else if jsonFlag { statusOutputString = parseToFullDetailSummary(outputInformationHolder)
statusOutputString, err = parseToJSON(statusOutputOverview) case jsonFlag:
if err != nil { statusOutputString, err = parseToJSON(outputInformationHolder)
return err case yamlFlag:
} statusOutputString, err = parseToYAML(outputInformationHolder)
} else if yamlFlag { default:
statusOutputString, err = parseToYAML(statusOutputOverview) statusOutputString = parseGeneralSummary(outputInformationHolder, false)
if err != nil { }
return err
} if err != nil {
} else { return err
statusOutputString = parseGeneralSummary(statusOutputOverview, false)
} }
cmd.Print(statusOutputString) cmd.Print(statusOutputString)
@ -245,15 +248,17 @@ func mapPeers(peers []*proto.PeerState) peersStateOutput {
timeLocal := pbPeerState.GetConnStatusUpdate().AsTime().Local() timeLocal := pbPeerState.GetConnStatusUpdate().AsTime().Local()
peerState := peerStateDetailOutput{ peerState := peerStateDetailOutput{
IP: pbPeerState.GetIP(), IP: pbPeerState.GetIP(),
PubKey: pbPeerState.GetPubKey(), PubKey: pbPeerState.GetPubKey(),
ConnStatus: pbPeerState.GetConnStatus(), Status: pbPeerState.GetConnStatus(),
ConnStatusUpdate: timeLocal.UTC(), LastStatusUpdate: timeLocal.UTC(),
ConnType: connType, ConnType: connType,
Direct: pbPeerState.GetDirect(), Direct: pbPeerState.GetDirect(),
LocalIceCandidateType: localICE, IceCandidateType: iceCandidateType{
RemoteIceCandidateType: remoteICE, Local: localICE,
FQDN: pbPeerState.GetFqdn(), Remote: remoteICE,
},
FQDN: pbPeerState.GetFqdn(),
} }
peersStateDetail = append(peersStateDetail, peerState) peersStateDetail = append(peersStateDetail, peerState)
@ -338,7 +343,7 @@ func parseGeneralSummary(overview statusOutputOverview, showURL bool) string {
"%s"+ // daemon status "%s"+ // daemon status
"Management: %s\n"+ "Management: %s\n"+
"Signal: %s\n"+ "Signal: %s\n"+
"Domain: %s\n"+ "FQDN: %s\n"+
"NetBird IP: %s\n"+ "NetBird IP: %s\n"+
"Interface type: %s\n"+ "Interface type: %s\n"+
"Peers count: %s\n", "Peers count: %s\n",
@ -376,13 +381,13 @@ func parsePeers(peers peersStateOutput) string {
for _, peerState := range peers.Details { for _, peerState := range peers.Details {
localICE := "-" localICE := "-"
if peerState.LocalIceCandidateType != "" { if peerState.IceCandidateType.Local != "" {
localICE = peerState.LocalIceCandidateType localICE = peerState.IceCandidateType.Local
} }
remoteICE := "-" remoteICE := "-"
if peerState.RemoteIceCandidateType != "" { if peerState.IceCandidateType.Remote != "" {
remoteICE = peerState.RemoteIceCandidateType remoteICE = peerState.IceCandidateType.Remote
} }
peerString := fmt.Sprintf( peerString := fmt.Sprintf(
@ -398,12 +403,12 @@ func parsePeers(peers peersStateOutput) string {
peerState.FQDN, peerState.FQDN,
peerState.IP, peerState.IP,
peerState.PubKey, peerState.PubKey,
peerState.ConnStatus, peerState.Status,
peerState.ConnType, peerState.ConnType,
peerState.Direct, peerState.Direct,
localICE, localICE,
remoteICE, remoteICE,
peerState.ConnStatusUpdate.Format("2006-01-02 15:04:05"), peerState.LastStatusUpdate.Format("2006-01-02 15:04:05"),
) )
peersString = peersString + peerString peersString = peersString + peerString

View File

@ -62,26 +62,30 @@ var overview = statusOutputOverview{
Connected: 2, Connected: 2,
Details: []peerStateDetailOutput{ Details: []peerStateDetailOutput{
{ {
IP: "192.168.178.101", IP: "192.168.178.101",
PubKey: "Pubkey1", PubKey: "Pubkey1",
FQDN: "peer-1.awesome-domain.com", FQDN: "peer-1.awesome-domain.com",
ConnStatus: "Connected", Status: "Connected",
ConnStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC), LastStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC),
ConnType: "P2P", ConnType: "P2P",
Direct: true, Direct: true,
LocalIceCandidateType: "", IceCandidateType: iceCandidateType{
RemoteIceCandidateType: "", Local: "",
Remote: "",
},
}, },
{ {
IP: "192.168.178.102", IP: "192.168.178.102",
PubKey: "Pubkey2", PubKey: "Pubkey2",
FQDN: "peer-2.awesome-domain.com", FQDN: "peer-2.awesome-domain.com",
ConnStatus: "Connected", Status: "Connected",
ConnStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC), LastStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC),
ConnType: "Relayed", ConnType: "Relayed",
Direct: false, Direct: false,
LocalIceCandidateType: "relay", IceCandidateType: iceCandidateType{
RemoteIceCandidateType: "prflx", Local: "relay",
Remote: "prflx",
},
}, },
}, },
}, },
@ -147,35 +151,41 @@ func TestSortingOfPeers(t *testing.T) {
func TestParsingToJSON(t *testing.T) { func TestParsingToJSON(t *testing.T) {
json, _ := parseToJSON(overview) json, _ := parseToJSON(overview)
// @formatter:off //@formatter:off
expectedJSON := "{" + expectedJSON := "{\"" +
"\"peers\":" + "peers\":" +
"{" + "{" +
"\"total\":2," + "\"total\":2," +
"\"connected\":2," + "\"connected\":2," +
"\"details\":" + "\"details\":" +
"[" + "[" +
"{" + "{" +
"\"ip\":\"192.168.178.101\"," +
"\"publicKey\":\"Pubkey1\"," +
"\"fqdn\":\"peer-1.awesome-domain.com\"," + "\"fqdn\":\"peer-1.awesome-domain.com\"," +
"\"connectionStatus\":\"Connected\"," + "\"netbirdIp\":\"192.168.178.101\"," +
"\"connectionStatusUpdate\":\"2001-01-01T01:01:01Z\"," + "\"publicKey\":\"Pubkey1\"," +
"\"status\":\"Connected\"," +
"\"lastStatusUpdate\":\"2001-01-01T01:01:01Z\"," +
"\"connectionType\":\"P2P\"," + "\"connectionType\":\"P2P\"," +
"\"direct\":true," + "\"direct\":true," +
"\"localIceCandidateType\":\"\"," + "\"iceCandidateType\":" +
"\"remoteIceCandidateType\":\"\"" + "{" +
"\"local\":\"\"," +
"\"remote\":\"\"" +
"}" +
"}," + "}," +
"{" + "{" +
"\"ip\":\"192.168.178.102\"," +
"\"publicKey\":\"Pubkey2\"," +
"\"fqdn\":\"peer-2.awesome-domain.com\"," + "\"fqdn\":\"peer-2.awesome-domain.com\"," +
"\"connectionStatus\":\"Connected\"," + "\"netbirdIp\":\"192.168.178.102\"," +
"\"connectionStatusUpdate\":\"2002-02-02T02:02:02Z\"," + "\"publicKey\":\"Pubkey2\"," +
"\"status\":\"Connected\"," +
"\"lastStatusUpdate\":\"2002-02-02T02:02:02Z\"," +
"\"connectionType\":\"Relayed\"," + "\"connectionType\":\"Relayed\"," +
"\"direct\":false," + "\"direct\":false," +
"\"localIceCandidateType\":\"relay\"," + "\"iceCandidateType\":" +
"\"remoteIceCandidateType\":\"prflx\"" + "{" +
"\"local\":\"relay\"," +
"\"remote\":\"prflx\"" +
"}" +
"}" + "}" +
"]" + "]" +
"}," + "}," +
@ -188,14 +198,14 @@ func TestParsingToJSON(t *testing.T) {
"\"connected\":true" + "\"connected\":true" +
"}," + "}," +
"\"signal\":" + "\"signal\":" +
"{" + "{\"" +
"\"url\":\"my-awesome-signal.com:443\"," + "url\":\"my-awesome-signal.com:443\"," +
"\"connected\":true" + "\"connected\":true" +
"}," + "}," +
"\"ip\":\"192.168.178.100/16\"," + "\"netbirdIp\":\"192.168.178.100/16\"," +
"\"publicKey\":\"Some-Pub-Key\"," + "\"publicKey\":\"Some-Pub-Key\"," +
"\"usesKernelInterface\":true," + "\"usesKernelInterface\":true," +
"\"domain\":\"some-localhost.awesome-domain.com\"" + "\"fqdn\":\"some-localhost.awesome-domain.com\"" +
"}" "}"
// @formatter:on // @formatter:on
@ -209,24 +219,26 @@ func TestParsingToYAML(t *testing.T) {
" total: 2\n" + " total: 2\n" +
" connected: 2\n" + " connected: 2\n" +
" details:\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" + " publicKey: Pubkey1\n" +
" fqdn: peer-1.awesome-domain.com\n" + " status: Connected\n" +
" connectionStatus: Connected\n" + " lastStatusUpdate: 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" + " iceCandidateType:\n" +
" remoteIceCandidateType: \"\"\n" + " local: \"\"\n" +
" - ip: 192.168.178.102\n" + " remote: \"\"\n" +
" - fqdn: peer-2.awesome-domain.com\n" +
" netbirdIp: 192.168.178.102\n" +
" publicKey: Pubkey2\n" + " publicKey: Pubkey2\n" +
" fqdn: peer-2.awesome-domain.com\n" + " status: Connected\n" +
" connectionStatus: Connected\n" + " lastStatusUpdate: 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: relay\n" + " iceCandidateType:\n" +
" remoteIceCandidateType: prflx\n" + " local: relay\n" +
" remote: prflx\n" +
"cliVersion: development\n" + "cliVersion: development\n" +
"daemonVersion: 0.14.1\n" + "daemonVersion: 0.14.1\n" +
"daemonStatus: Connected\n" + "daemonStatus: Connected\n" +
@ -236,10 +248,10 @@ func TestParsingToYAML(t *testing.T) {
"signal:\n" + "signal:\n" +
" url: my-awesome-signal.com:443\n" + " url: my-awesome-signal.com:443\n" +
" connected: true\n" + " connected: true\n" +
"ip: 192.168.178.100/16\n" + "netbirdIp: 192.168.178.100/16\n" +
"publicKey: Some-Pub-Key\n" + "publicKey: Some-Pub-Key\n" +
"usesKernelInterface: true\n" + "usesKernelInterface: true\n" +
"domain: some-localhost.awesome-domain.com\n" "fqdn: some-localhost.awesome-domain.com\n"
assert.Equal(t, expectedYAML, yaml) assert.Equal(t, expectedYAML, yaml)
} }
@ -272,7 +284,7 @@ func TestParsingToDetail(t *testing.T) {
"CLI version: development\n" + "CLI version: development\n" +
"ConnectedManagement: Connected to my-awesome-management.com:443\n" + "ConnectedManagement: Connected to my-awesome-management.com:443\n" +
"Signal: Connected to my-awesome-signal.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" + "NetBird IP: 192.168.178.100/16\n" +
"Interface type: Kernel\n" + "Interface type: Kernel\n" +
"Peers count: 2/2 Connected\n" "Peers count: 2/2 Connected\n"
@ -287,7 +299,7 @@ func TestParsingToShortVersion(t *testing.T) {
"CLI version: development\n" + "CLI version: development\n" +
"ConnectedManagement: Connected\n" + "ConnectedManagement: Connected\n" +
"Signal: 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" + "NetBird IP: 192.168.178.100/16\n" +
"Interface type: Kernel\n" + "Interface type: Kernel\n" +
"Peers count: 2/2 Connected\n" "Peers count: 2/2 Connected\n"