From 55fe5b0b41b8b722a8c7cb6f1b72412ef6d0c691 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Sun, 15 Aug 2021 23:10:39 +0200 Subject: [PATCH] Use pterm table in node list --- cmd/headscale/cli/nodes.go | 53 ++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 75000434..fdcaf760 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -8,7 +8,11 @@ import ( "time" survey "github.com/AlecAivazis/survey/v2" + "github.com/juanfont/headscale" + "github.com/pterm/pterm" "github.com/spf13/cobra" + "tailscale.com/tailcfg" + "tailscale.com/types/wgkey" ) func init() { @@ -33,7 +37,7 @@ var registerNodeCmd = &cobra.Command{ Short: "Registers a machine to your network", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return fmt.Errorf("Missing parameters") + return fmt.Errorf("missing parameters") } return nil }, @@ -85,19 +89,13 @@ var listNodesCmd = &cobra.Command{ log.Fatalf("Error getting nodes: %s", err) } - fmt.Printf("ID\tname\t\tlast seen\t\tephemeral\n") - for _, m := range *machines { - var ephemeral bool - if m.AuthKey != nil && m.AuthKey.Ephemeral { - ephemeral = true - } - var lastSeen time.Time - if m.LastSeen != nil { - lastSeen = *m.LastSeen - } - fmt.Printf("%d\t%s\t%s\t%t\n", m.ID, m.Name, lastSeen.Format("2006-01-02 15:04:05"), ephemeral) + d, err := nodesToPtables(*machines) + if err != nil { + log.Fatalf("Error converting to table: %s", err) } + pterm.DefaultTable.WithHasHeader().WithData(d).Render() + }, } @@ -106,7 +104,7 @@ var deleteNodeCmd = &cobra.Command{ Short: "Delete a node", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return fmt.Errorf("Missing parameters") + return fmt.Errorf("missing parameters") } return nil }, @@ -144,3 +142,32 @@ var deleteNodeCmd = &cobra.Command{ } }, } + +func nodesToPtables(m []headscale.Machine) (pterm.TableData, error) { + d := pterm.TableData{{"ID", "Name", "NodeKey", "IP address", "Ephemeral", "Last seen", "Online"}} + + for _, m := range m { + var ephemeral bool + if m.AuthKey != nil && m.AuthKey.Ephemeral { + ephemeral = true + } + var lastSeen time.Time + if m.LastSeen != nil { + lastSeen = *m.LastSeen + } + nKey, err := wgkey.ParseHex(m.NodeKey) + if err != nil { + return nil, err + } + nodeKey := tailcfg.NodeKey(nKey) + + var online string + if m.LastSeen.After(time.Now().Add(-5 * time.Minute)) { // TODO: Find a better way to reliably show if online + online = pterm.LightGreen("true") + } else { + online = pterm.LightRed("false") + } + d = append(d, []string{strconv.FormatUint(m.ID, 10), m.Name, nodeKey.ShortString(), m.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online}) + } + return d, nil +}