1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/cmd/headscale/cli/nodes.go

89 lines
1.9 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"log"
"strings"
"time"
"github.com/spf13/cobra"
)
2021-06-28 20:04:05 +02:00
var NodeCmd = &cobra.Command{
Use: "nodes",
Short: "Manage the nodes of Headscale",
}
var RegisterCmd = &cobra.Command{
Use: "register machineID",
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 nil
},
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
m, err := h.RegisterMachine(args[0], n)
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(m, err, o)
return
}
if err != nil {
fmt.Printf("Cannot register machine: %s\n", err)
return
}
fmt.Printf("Machine registered\n")
},
}
2021-05-01 20:00:25 +02:00
var ListNodesCmd = &cobra.Command{
Use: "list",
Short: "List the nodes in a given namespace",
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 13:58:51 +02:00
o, _ := cmd.Flags().GetString("output")
2021-05-01 20:00:25 +02:00
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
machines, err := h.ListMachinesInNamespace(n)
2021-05-08 13:58:51 +02:00
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(machines, err, o)
2021-05-08 13:58:51 +02:00
return
}
2021-05-01 20:00:25 +02:00
if err != nil {
log.Fatalf("Error getting nodes: %s", err)
}
fmt.Printf("name\t\tlast seen\t\tephemeral\n")
2021-05-01 20:00:25 +02:00
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("%s\t%s\t%t\n", m.Name, lastSeen.Format("2006-01-02 15:04:05"), ephemeral)
2021-05-01 20:00:25 +02:00
}
},
}