mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-23 18:15:26 +00:00
Use pterm table in node list
This commit is contained in:
parent
8eb7d47072
commit
55fe5b0b41
@ -8,7 +8,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
survey "github.com/AlecAivazis/survey/v2"
|
survey "github.com/AlecAivazis/survey/v2"
|
||||||
|
"github.com/juanfont/headscale"
|
||||||
|
"github.com/pterm/pterm"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"tailscale.com/tailcfg"
|
||||||
|
"tailscale.com/types/wgkey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -33,7 +37,7 @@ var registerNodeCmd = &cobra.Command{
|
|||||||
Short: "Registers a machine to your network",
|
Short: "Registers a machine to your network",
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return fmt.Errorf("Missing parameters")
|
return fmt.Errorf("missing parameters")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -85,19 +89,13 @@ var listNodesCmd = &cobra.Command{
|
|||||||
log.Fatalf("Error getting nodes: %s", err)
|
log.Fatalf("Error getting nodes: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("ID\tname\t\tlast seen\t\tephemeral\n")
|
d, err := nodesToPtables(*machines)
|
||||||
for _, m := range *machines {
|
if err != nil {
|
||||||
var ephemeral bool
|
log.Fatalf("Error converting to table: %s", err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pterm.DefaultTable.WithHasHeader().WithData(d).Render()
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +104,7 @@ var deleteNodeCmd = &cobra.Command{
|
|||||||
Short: "Delete a node",
|
Short: "Delete a node",
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return fmt.Errorf("Missing parameters")
|
return fmt.Errorf("missing parameters")
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user