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

170 lines
4.1 KiB
Go
Raw Normal View History

package cli
import (
"context"
"fmt"
"time"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
2021-08-15 23:20:38 +02:00
"github.com/pterm/pterm"
"github.com/rs/zerolog/log"
"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)
2021-10-16 17:20:06 +02:00
namespaceCmd.AddCommand(renameNamespaceCmd)
2021-07-25 15:03:45 +02:00
}
var namespaceCmd = &cobra.Command{
2021-06-28 20:04:05 +02:00
Use: "namespaces",
Short: "Manage the namespaces of Headscale",
}
2021-07-25 15:03:45 +02:00
var createNamespaceCmd = &cobra.Command{
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) {
output, _ := cmd.Flags().GetString("output")
namespaceName := args[0]
2021-10-31 17:26:51 +01:00
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
log.Trace().Interface("client", client).Msg("Obtained gRPC client")
request := &v1.CreateNamespaceRequest{Name: namespaceName}
log.Trace().Interface("request", request).Msg("Sending CreateNamespace request")
response, err := client.CreateNamespace(ctx, request)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot create namespace: %s", err), output)
return
}
SuccessOutput(response.Namespace, "Namespace created", output)
},
}
2021-07-25 15:03:45 +02:00
var destroyNamespaceCmd = &cobra.Command{
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) {
output, _ := cmd.Flags().GetString("output")
namespaceName := args[0]
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
request := &v1.DeleteNamespaceRequest{Name: namespaceName}
response, err := client.DeleteNamespace(ctx, request)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot destroy namespace: %s", err), output)
return
}
SuccessOutput(response, "Namespace destroyed", output)
},
}
2021-07-25 15:03:45 +02:00
var listNamespacesCmd = &cobra.Command{
Use: "list",
Short: "List all the namespaces",
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
request := &v1.ListNamespacesRequest{}
response, err := client.ListNamespaces(ctx, request)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot get namespaces: %s", err), output)
return
}
if output != "" {
SuccessOutput(response.Namespaces, "", output)
return
}
2021-08-15 23:20:38 +02:00
d := pterm.TableData{{"ID", "Name", "Created"}}
for _, namespace := range response.GetNamespaces() {
d = append(
d,
[]string{
namespace.GetId(),
namespace.GetName(),
namespace.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
},
)
}
2021-08-15 23:35:03 +02:00
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
if err != nil {
ErrorOutput(err, fmt.Sprintf("Failed to render pterm table: %s", err), output)
return
2021-08-15 23:35:03 +02:00
}
},
}
2021-10-16 17:20:06 +02:00
var renameNamespaceCmd = &cobra.Command{
Use: "rename OLD_NAME NEW_NAME",
Short: "Renames a namespace",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
request := &v1.RenameNamespaceRequest{
OldName: args[0],
NewName: args[1],
2021-10-16 17:20:06 +02:00
}
response, err := client.RenameNamespace(ctx, request)
2021-10-16 17:20:06 +02:00
if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot rename namespace: %s", err), output)
2021-10-16 17:20:06 +02:00
return
}
SuccessOutput(response.Namespace, "Namespace renamed", output)
2021-10-16 17:20:06 +02:00
},
}