Friendly formats for getDHT, getSessions, setTunTap etc

This commit is contained in:
Neil Alexander 2018-05-21 14:14:57 +01:00
parent 201701ae4a
commit ca3e541d3b
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 25 additions and 17 deletions

View File

@ -46,7 +46,7 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("help", nil, func(in admin_info) (admin_info, error) { a.addHandler("help", nil, func(in admin_info) (admin_info, error) {
handlers := make(map[string]interface{}) handlers := make(map[string]interface{})
for _, handler := range a.handlers { for _, handler := range a.handlers {
handlers[handler.name] = admin_info{ "fields": handler.args } handlers[handler.name] = admin_info{"fields": handler.args}
} }
return admin_info{"help": handlers}, nil return admin_info{"help": handlers}, nil
}) })
@ -57,7 +57,7 @@ func (a *admin) init(c *Core, listenaddr string) {
self := a.getData_getSelf().asMap() self := a.getData_getSelf().asMap()
ip := fmt.Sprint(self["ip"]) ip := fmt.Sprint(self["ip"])
delete(self, "ip") delete(self, "ip")
return admin_info{"self": admin_info{ ip: self }}, nil return admin_info{"self": admin_info{ip: self}}, nil
}) })
a.addHandler("getPeers", []string{}, func(in admin_info) (admin_info, error) { a.addHandler("getPeers", []string{}, func(in admin_info) (admin_info, error) {
sort := "ip" sort := "ip"
@ -136,7 +136,7 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("getTunTap", []string{}, func(in admin_info) (r admin_info, e error) { a.addHandler("getTunTap", []string{}, func(in admin_info) (r admin_info, e error) {
defer func() { defer func() {
recover() recover()
r = admin_info{ "tuntap": admin_info{ "none": admin_info{ } } } r = admin_info{"none": admin_info{}}
e = nil e = nil
}() }()
@ -278,7 +278,7 @@ func (a *admin) handleRequest(conn net.Conn) {
if _, ok := recv[arg]; !ok { if _, ok := recv[arg]; !ok {
send = admin_info{ send = admin_info{
"status": "error", "status": "error",
"error": "Expected field missing", "error": "Expected field missing: " + arg,
"expecting": arg, "expecting": arg,
} }
break handlers break handlers

View File

@ -61,7 +61,7 @@ func main() {
if err := decoder.Decode(&recv); err == nil { if err := decoder.Decode(&recv); err == nil {
if recv["status"] == "error" { if recv["status"] == "error" {
if err, ok := recv["error"]; ok { if err, ok := recv["error"]; ok {
fmt.Println(err) fmt.Println("Error:", err)
} else { } else {
fmt.Println("Unspecified error occured") fmt.Println("Unspecified error occured")
} }
@ -77,16 +77,11 @@ func main() {
} }
req := recv["request"].(map[string]interface{}) req := recv["request"].(map[string]interface{})
res := recv["response"].(map[string]interface{}) res := recv["response"].(map[string]interface{})
defer func() {
recover()
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
fmt.Println(string(json))
}
}()
switch req["request"] { switch req["request"] {
case "dot": case "dot":
fmt.Println(res["dot"]) fmt.Println(res["dot"])
default: case "help", "getPeers", "getSwitchPeers", "getDHT", "getSessions":
maxWidths := make(map[string]int) maxWidths := make(map[string]int)
var keyOrder []string var keyOrder []string
keysOrdered := false keysOrdered := false
@ -114,22 +109,35 @@ func main() {
} }
if len(keyOrder) > 0 { if len(keyOrder) > 0 {
fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", "") fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", "")
for _, v := range keyOrder { for _, v := range keyOrder {
fmt.Printf("%-" + fmt.Sprint(maxWidths[v]) + "s ", v) fmt.Printf("%-"+fmt.Sprint(maxWidths[v])+"s ", v)
} }
fmt.Println() fmt.Println()
} }
for slk, slv := range tlv.(map[string]interface{}) { for slk, slv := range tlv.(map[string]interface{}) {
fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", slk) fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", slk)
for _, k := range keyOrder { for _, k := range keyOrder {
fmt.Printf("%-" + fmt.Sprint(maxWidths[k]) + "s ", fmt.Sprint(slv.(map[string]interface{})[k])) fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", fmt.Sprint(slv.(map[string]interface{})[k]))
} }
fmt.Println() fmt.Println()
} }
} }
case "getTunTap", "setTunTap":
for k, v := range res {
fmt.Println("Interface name:", k)
if mtu, ok := v.(map[string]interface{})["mtu"].(float64); ok {
fmt.Println("Interface MTU:", mtu)
}
if tap_mode, ok := v.(map[string]interface{})["tap_mode"].(bool); ok {
fmt.Println("TAP mode:", tap_mode)
}
}
default:
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
fmt.Println(string(json))
}
} }
} }