2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var RegisterCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "register machineID",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Registers a machine to your network",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-04-30 00:23:26 +02:00
|
|
|
if len(args) < 1 {
|
2021-04-28 16:15:45 +02:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-04-30 00:23:26 +02:00
|
|
|
err = h.RegisterMachine(args[0], n)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Ook.")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
|
|
|
machines, err := h.ListMachinesInNamespace(n)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting nodes: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-01 20:04:03 +02:00
|
|
|
fmt.Printf("name\t\tlast seen\n")
|
2021-05-01 20:00:25 +02:00
|
|
|
for _, m := range *machines {
|
|
|
|
fmt.Printf("%s\t%s\n", m.Name, m.LastSeen.Format("2006-01-02 15:04:05"))
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
var NodeCmd = &cobra.Command{
|
|
|
|
Use: "node",
|
|
|
|
Short: "Manage the nodes of Headscale",
|
|
|
|
}
|