2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-05-08 13:28:22 +02:00
|
|
|
"strings"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(namespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(createNamespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(listNamespacesCmd)
|
|
|
|
namespaceCmd.AddCommand(destroyNamespaceCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var namespaceCmd = &cobra.Command{
|
2021-06-28 20:04:05 +02:00
|
|
|
Use: "namespaces",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Manage the namespaces of Headscale",
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var createNamespaceCmd = &cobra.Command{
|
2021-04-28 16:15:45 +02:00
|
|
|
Use: "create NAME",
|
|
|
|
Short: "Creates a new namespace",
|
|
|
|
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) {
|
2021-05-08 13:28:22 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
namespace, err := h.CreateNamespace(args[0])
|
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(namespace, err, o)
|
2021-05-08 13:28:22 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-05-08 13:28:22 +02:00
|
|
|
fmt.Printf("Error creating namespace: %s\n", err)
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
fmt.Printf("Namespace created\n")
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var destroyNamespaceCmd = &cobra.Command{
|
2021-05-09 17:12:05 +02:00
|
|
|
Use: "destroy NAME",
|
|
|
|
Short: "Destroys a namespace",
|
|
|
|
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) {
|
|
|
|
o, _ := cmd.Flags().GetString("output")
|
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
|
|
|
err = h.DestroyNamespace(args[0])
|
|
|
|
if strings.HasPrefix(o, "json") {
|
|
|
|
JsonOutput(map[string]string{"Result": "Namespace destroyed"}, err, o)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error destroying namespace: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Namespace destroyed\n")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var listNamespacesCmd = &cobra.Command{
|
2021-04-28 16:15:45 +02:00
|
|
|
Use: "list",
|
|
|
|
Short: "List all the namespaces",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-05-08 13:28:22 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
namespaces, err := h.ListNamespaces()
|
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(namespaces, err, o)
|
2021-05-08 13:28:22 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("ID\tName\n")
|
2021-05-08 13:28:22 +02:00
|
|
|
for _, n := range *namespaces {
|
2021-04-28 16:15:45 +02:00
|
|
|
fmt.Printf("%d\t%s\n", n.ID, n.Name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|