TUN-3809: Allow routes ip show to output as JSON or YAML

It also fixes the marshelling of CIDR into JSON since otherwise
it would show garbled characters as the mask.
This commit is contained in:
Nuno Diegues
2021-01-26 11:58:56 +00:00
parent 2146f71b45
commit 6681d179dc
3 changed files with 33 additions and 10 deletions

View File

@@ -16,9 +16,9 @@ import (
// network, and says that eyeballs can reach that route using the corresponding
// tunnel.
type Route struct {
Network CIDR
Network CIDR `json:"network"`
TunnelID uuid.UUID `json:"tunnel_id"`
Comment string
Comment string `json:"comment"`
CreatedAt time.Time `json:"created_at"`
DeletedAt time.Time `json:"deleted_at"`
}
@@ -26,11 +26,20 @@ type Route struct {
// CIDR is just a newtype wrapper around net.IPNet. It adds JSON unmarshalling.
type CIDR net.IPNet
func (c *CIDR) String() string {
n := net.IPNet(*c)
func (c CIDR) String() string {
n := net.IPNet(c)
return n.String()
}
func (c CIDR) MarshalJSON() ([]byte, error) {
str := c.String()
json, err := json.Marshal(str)
if err != nil {
return nil, errors.Wrap(err, "error serializing CIDR into JSON")
}
return json, nil
}
// UnmarshalJSON parses a JSON string into net.IPNet
func (c *CIDR) UnmarshalJSON(data []byte) error {
var s string
@@ -68,9 +77,9 @@ func (r NewRoute) MarshalJSON() ([]byte, error) {
// DetailedRoute is just a Route with some extra fields, e.g. TunnelName.
type DetailedRoute struct {
Network CIDR
Network CIDR `json:"network"`
TunnelID uuid.UUID `json:"tunnel_id"`
Comment string
Comment string `json:"comment"`
CreatedAt time.Time `json:"created_at"`
DeletedAt time.Time `json:"deleted_at"`
TunnelName string `json:"tunnel_name"`