Debug messages. Squashed Add Route bugs and few others.
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package router
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultLocalMetric is default route cost for local network
|
||||
@@ -19,6 +24,18 @@ const (
|
||||
IgnoreIfExists
|
||||
)
|
||||
|
||||
// String returns human reprensentation of policy
|
||||
func (p AddPolicy) String() string {
|
||||
switch p {
|
||||
case OverrideIfExists:
|
||||
return "OVERRIDE"
|
||||
case IgnoreIfExists:
|
||||
return "IGNORE"
|
||||
default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
// RouteOption is used to set routing table entry options
|
||||
type RouteOption func(*RouteOptions)
|
||||
|
||||
@@ -34,8 +51,6 @@ type RouteOptions struct {
|
||||
Metric int
|
||||
// Policy defines route addition policy
|
||||
Policy AddPolicy
|
||||
// Context stores other arbitrary options
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
// DestAddr sets destination address
|
||||
@@ -100,3 +115,26 @@ func NewRoute(opts ...RouteOption) Route {
|
||||
func (r *route) Options() RouteOptions {
|
||||
return r.opts
|
||||
}
|
||||
|
||||
// String allows to print the route
|
||||
func (r *route) String() string {
|
||||
// this will help us build routing table string
|
||||
sb := &strings.Builder{}
|
||||
|
||||
// create nice table printing structure
|
||||
table := tablewriter.NewWriter(sb)
|
||||
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
|
||||
|
||||
strRoute := []string{
|
||||
r.opts.DestAddr,
|
||||
r.opts.Gateway.Address(),
|
||||
r.opts.Network,
|
||||
fmt.Sprintf("%d", r.opts.Metric),
|
||||
}
|
||||
table.Append(strRoute)
|
||||
|
||||
// render table into sb
|
||||
table.Render()
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
Reference in New Issue
Block a user