func listAddresses(ctx context.Context) { conn, err := grpc.Dial("172.18.0.1:8080") assert(err) defer conn.Close() client := protocol.NewHostsClient(conn) in := protocol.HostListReq{} out, err := client.List(ctx, &in) assert(err) tabw := tabwriter.NewWriter(os.Stdout, 8, 8, 2, ' ', 0) defer tabw.Flush() fmt.Fprintf(tabw, "%s\t%s\t%s\t%s\n", "HOST", "NAME", "IP", "VERSION") for _, host := range out.Hosts { sort.Strings(host.Ipv4) sort.Strings(host.Ipv6) for _, ip := range host.Ipv4 { fmt.Fprintf(tabw, "%s\t%s\t%s\t%s\n", host.Id[:8], host.Name, ip, "ipv4") } for _, ip := range host.Ipv6 { fmt.Fprintf(tabw, "%s\t%s\t%s\t%s\n", host.Id[:8], host.Name, ip, "ipv6") } } }
func Run(handler HandlerFunc) { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) go quitSignal(cancel) defer cancel() u, err := url.Parse(os.Getenv("SWITCHBOARD_URL")) if err != nil { log.Fatal(err) } if u.Scheme != "tcp" { log.Fatal("SWITCHBOARD_URL must start with tcp://") } conn, err := grpc.Dial(u.Host) if err != nil { log.Fatal(err) } defer conn.Close() plugin := &Plugin{ conn: conn, hosts: protocol.NewHostsClient(conn), rules: protocol.NewRulesClient(conn), config: os.Getenv("SWITCHBOARD_CONFIG"), } err = handler(ctx, plugin) if err != nil { log.Fatal(err) } }
func listHosts(ctx context.Context) { conn, err := grpc.Dial("172.18.0.1:8080") assert(err) defer conn.Close() client := protocol.NewHostsClient(conn) in := protocol.HostListReq{} out, err := client.List(ctx, &in) assert(err) tabw := tabwriter.NewWriter(os.Stdout, 8, 8, 2, ' ', 0) defer tabw.Flush() fmt.Fprintf(tabw, "%s\t%s\t%s\n", "ID", "NAME", "STATE") for _, host := range out.Hosts { state := "down" if host.Up { state = "up" } fmt.Fprintf(tabw, "%s\t%s\t%v\n", host.Id[:8], host.Name, state) } }