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 {
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"`
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"`
LocalIceCandidateType string `json:"localIceCandidateType" yaml:"localIceCandidateType"`
RemoteIceCandidateType string `json:"remoteIceCandidateType" yaml:"remoteIceCandidateType"`
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,24 +132,23 @@ 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)
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
}
} else if yamlFlag {
statusOutputString, err = parseToYAML(statusOutputOverview)
if err != nil {
return err
}
} else {
statusOutputString = parseGeneralSummary(statusOutputOverview, false)
}
cmd.Print(statusOutputString)
@ -247,12 +250,14 @@ func mapPeers(peers []*proto.PeerState) peersStateOutput {
peerState := peerStateDetailOutput{
IP: pbPeerState.GetIP(),
PubKey: pbPeerState.GetPubKey(),
ConnStatus: pbPeerState.GetConnStatus(),
ConnStatusUpdate: timeLocal.UTC(),
Status: pbPeerState.GetConnStatus(),
LastStatusUpdate: timeLocal.UTC(),
ConnType: connType,
Direct: pbPeerState.GetDirect(),
LocalIceCandidateType: localICE,
RemoteIceCandidateType: remoteICE,
IceCandidateType: iceCandidateType{
Local: localICE,
Remote: remoteICE,
},
FQDN: pbPeerState.GetFqdn(),
}
@ -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

View File

@ -65,23 +65,27 @@ var overview = statusOutputOverview{
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),
Status: "Connected",
LastStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC),
ConnType: "P2P",
Direct: true,
LocalIceCandidateType: "",
RemoteIceCandidateType: "",
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),
Status: "Connected",
LastStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC),
ConnType: "Relayed",
Direct: false,
LocalIceCandidateType: "relay",
RemoteIceCandidateType: "prflx",
IceCandidateType: iceCandidateType{
Local: "relay",
Remote: "prflx",
},
},
},
},
@ -148,34 +152,40 @@ func TestParsingToJSON(t *testing.T) {
json, _ := parseToJSON(overview)
//@formatter:off
expectedJSON := "{" +
"\"peers\":" +
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"