mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-22 20:00:27 +00:00
Add some pretty printing to yggdrasilctl, small modifications to JSON formatting in admin socket
This commit is contained in:
parent
bd32f5890b
commit
201701ae4a
@ -44,17 +44,20 @@ func (a *admin) init(c *Core, listenaddr string) {
|
|||||||
a.core = c
|
a.core = c
|
||||||
a.listenaddr = listenaddr
|
a.listenaddr = listenaddr
|
||||||
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][]string)
|
handlers := make(map[string]interface{})
|
||||||
for _, handler := range a.handlers {
|
for _, handler := range a.handlers {
|
||||||
handlers[handler.name] = handler.args
|
handlers[handler.name] = admin_info{ "fields": handler.args }
|
||||||
}
|
}
|
||||||
return admin_info{"handlers": handlers}, nil
|
return admin_info{"help": handlers}, nil
|
||||||
})
|
})
|
||||||
a.addHandler("dot", []string{}, func(in admin_info) (admin_info, error) {
|
a.addHandler("dot", []string{}, func(in admin_info) (admin_info, error) {
|
||||||
return admin_info{"dot": string(a.getResponse_dot())}, nil
|
return admin_info{"dot": string(a.getResponse_dot())}, nil
|
||||||
})
|
})
|
||||||
a.addHandler("getSelf", []string{}, func(in admin_info) (admin_info, error) {
|
a.addHandler("getSelf", []string{}, func(in admin_info) (admin_info, error) {
|
||||||
return admin_info{"self": a.getData_getSelf().asMap()}, nil
|
self := a.getData_getSelf().asMap()
|
||||||
|
ip := fmt.Sprint(self["ip"])
|
||||||
|
delete(self, "ip")
|
||||||
|
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"
|
||||||
@ -133,14 +136,15 @@ 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{"name": "none"}
|
r = admin_info{ "tuntap": admin_info{ "none": admin_info{ } } }
|
||||||
e = nil
|
e = nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return admin_info{
|
return admin_info{
|
||||||
"name": a.core.tun.iface.Name(),
|
a.core.tun.iface.Name(): admin_info{
|
||||||
"tap_mode": a.core.tun.iface.IsTAP(),
|
"tap_mode": a.core.tun.iface.IsTAP(),
|
||||||
"mtu": a.core.tun.mtu,
|
"mtu": a.core.tun.mtu,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
a.addHandler("setTunTap", []string{"name", "[tap_mode]", "[mtu]"}, func(in admin_info) (admin_info, error) {
|
a.addHandler("setTunTap", []string{"name", "[tap_mode]", "[mtu]"}, func(in admin_info) (admin_info, error) {
|
||||||
@ -162,9 +166,10 @@ func (a *admin) init(c *Core, listenaddr string) {
|
|||||||
return admin_info{}, errors.New("Failed to configure adapter")
|
return admin_info{}, errors.New("Failed to configure adapter")
|
||||||
} else {
|
} else {
|
||||||
return admin_info{
|
return admin_info{
|
||||||
"name": a.core.tun.iface.Name(),
|
a.core.tun.iface.Name(): admin_info{
|
||||||
"tap_mode": a.core.tun.iface.IsTAP(),
|
"tap_mode": a.core.tun.iface.IsTAP(),
|
||||||
"mtu": ifmtu,
|
"mtu": ifmtu,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -4,6 +4,7 @@ import "flag"
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "net"
|
import "net"
|
||||||
|
import "sort"
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "os"
|
import "os"
|
||||||
@ -58,23 +59,77 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := decoder.Decode(&recv); err == nil {
|
if err := decoder.Decode(&recv); err == nil {
|
||||||
|
if recv["status"] == "error" {
|
||||||
|
if err, ok := recv["error"]; ok {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Unspecified error occured")
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
if _, ok := recv["request"]; !ok {
|
if _, ok := recv["request"]; !ok {
|
||||||
fmt.Println("Missing request")
|
fmt.Println("Missing request in response (malformed response?)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := recv["response"]; !ok {
|
if _, ok := recv["response"]; !ok {
|
||||||
fmt.Println("Missing response")
|
fmt.Println("Missing response body (malformed response?)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
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:
|
default:
|
||||||
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
|
maxWidths := make(map[string]int)
|
||||||
fmt.Println(string(json))
|
var keyOrder []string
|
||||||
|
keysOrdered := false
|
||||||
|
|
||||||
|
for _, tlv := range res {
|
||||||
|
for slk, slv := range tlv.(map[string]interface{}) {
|
||||||
|
if !keysOrdered {
|
||||||
|
for k := range slv.(map[string]interface{}) {
|
||||||
|
keyOrder = append(keyOrder, fmt.Sprint(k))
|
||||||
}
|
}
|
||||||
|
sort.Strings(keyOrder)
|
||||||
|
keysOrdered = true
|
||||||
|
}
|
||||||
|
for k, v := range slv.(map[string]interface{}) {
|
||||||
|
if len(fmt.Sprint(slk)) > maxWidths["key"] {
|
||||||
|
maxWidths["key"] = len(fmt.Sprint(slk))
|
||||||
|
}
|
||||||
|
if len(fmt.Sprint(v)) > maxWidths[k] {
|
||||||
|
maxWidths[k] = len(fmt.Sprint(v))
|
||||||
|
if maxWidths[k] < len(k) {
|
||||||
|
maxWidths[k] = len(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(keyOrder) > 0 {
|
||||||
|
fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", "")
|
||||||
|
for _, v := range keyOrder {
|
||||||
|
fmt.Printf("%-" + fmt.Sprint(maxWidths[v]) + "s ", v)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
for slk, slv := range tlv.(map[string]interface{}) {
|
||||||
|
fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", slk)
|
||||||
|
for _, k := range keyOrder {
|
||||||
|
fmt.Printf("%-" + fmt.Sprint(maxWidths[k]) + "s ", fmt.Sprint(slv.(map[string]interface{})[k]))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user