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

242 lines
5.0 KiB
Go
Raw Normal View History

package cli
import (
"errors"
"fmt"
survey "github.com/AlecAivazis/survey/v2"
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"
"google.golang.org/grpc/status"
)
2021-07-25 15:03:45 +02:00
func init() {
rootCmd.AddCommand(userCmd)
userCmd.AddCommand(createUserCmd)
userCmd.AddCommand(listUsersCmd)
userCmd.AddCommand(destroyUserCmd)
userCmd.AddCommand(renameUserCmd)
2021-07-25 15:03:45 +02:00
}
var errMissingParameter = errors.New("missing parameters")
2021-11-15 20:18:14 +01:00
var userCmd = &cobra.Command{
Use: "users",
Short: "Manage the users of Headscale",
Aliases: []string{"user", "namespace", "namespaces", "ns"},
}
var createUserCmd = &cobra.Command{
Use: "create NAME",
Short: "Creates a new user",
Aliases: []string{"c", "new"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
2021-11-15 20:18:14 +01:00
return errMissingParameter
}
2021-11-14 16:46:09 +01:00
return nil
},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
userName := args[0]
ctx, client, conn, cancel := getHeadscaleCLIClient()
2021-10-31 17:26:51 +01:00
defer cancel()
defer conn.Close()
log.Trace().Interface("client", client).Msg("Obtained gRPC client")
request := &v1.CreateUserRequest{Name: userName}
log.Trace().Interface("request", request).Msg("Sending CreateUser request")
response, err := client.CreateUser(ctx, request)
if err != nil {
2021-11-13 09:36:45 +01:00
ErrorOutput(
err,
fmt.Sprintf(
"Cannot create user: %s",
2021-11-13 09:36:45 +01:00
status.Convert(err).Message(),
),
output,
)
2021-11-14 16:46:09 +01:00
return
}
SuccessOutput(response.GetUser(), "User created", output)
},
}
var destroyUserCmd = &cobra.Command{
Use: "destroy NAME",
Short: "Destroys a user",
Aliases: []string{"delete"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
2021-11-15 20:18:14 +01:00
return errMissingParameter
}
2021-11-14 16:46:09 +01:00
return nil
},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
userName := args[0]
request := &v1.GetUserRequest{
Name: userName,
}
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()
_, err := client.GetUser(ctx, request)
if err != nil {
2021-11-13 09:36:45 +01:00
ErrorOutput(
err,
2021-11-14 09:30:48 +01:00
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
2021-11-13 09:36:45 +01:00
output,
)
2021-11-14 16:46:09 +01:00
return
}
confirm := false
force, _ := cmd.Flags().GetBool("force")
if !force {
prompt := &survey.Confirm{
2021-11-14 09:30:48 +01:00
Message: fmt.Sprintf(
"Do you want to remove the user '%s' and any associated preauthkeys?",
userName,
2021-11-14 09:30:48 +01:00
),
}
err := survey.AskOne(prompt, &confirm)
if err != nil {
return
}
}
if confirm || force {
request := &v1.DeleteUserRequest{Name: userName}
response, err := client.DeleteUser(ctx, request)
if err != nil {
2021-11-14 09:30:48 +01:00
ErrorOutput(
err,
fmt.Sprintf(
"Cannot destroy user: %s",
2021-11-14 09:30:48 +01:00
status.Convert(err).Message(),
),
output,
)
2021-11-14 16:46:09 +01:00
return
}
SuccessOutput(response, "User destroyed", output)
} else {
SuccessOutput(map[string]string{"Result": "User not destroyed"}, "User not destroyed", output)
}
},
}
var listUsersCmd = &cobra.Command{
Use: "list",
Short: "List all the users",
Aliases: []string{"ls", "show"},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()
request := &v1.ListUsersRequest{}
response, err := client.ListUsers(ctx, request)
if err != nil {
2021-11-13 09:36:45 +01:00
ErrorOutput(
err,
fmt.Sprintf("Cannot get users: %s", status.Convert(err).Message()),
2021-11-13 09:36:45 +01:00
output,
)
2021-11-14 16:46:09 +01:00
return
}
if output != "" {
SuccessOutput(response.GetUsers(), "", output)
2021-11-14 16:46:09 +01:00
return
}
2021-08-15 23:20:38 +02:00
tableData := pterm.TableData{{"ID", "Name", "Created"}}
for _, user := range response.GetUsers() {
tableData = append(
tableData,
[]string{
user.GetId(),
user.GetName(),
user.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
},
)
}
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
2021-08-15 23:35:03 +02:00
if err != nil {
2021-11-13 09:36:45 +01:00
ErrorOutput(
err,
fmt.Sprintf("Failed to render pterm table: %s", err),
output,
)
2021-11-14 16:46:09 +01:00
return
2021-08-15 23:35:03 +02:00
}
},
}
2021-10-16 17:20:06 +02:00
var renameUserCmd = &cobra.Command{
Use: "rename OLD_NAME NEW_NAME",
Short: "Renames a user",
Aliases: []string{"mv"},
2021-10-16 17:20:06 +02:00
Args: func(cmd *cobra.Command, args []string) error {
expectedArguments := 2
if len(args) < expectedArguments {
2021-11-15 20:18:14 +01:00
return errMissingParameter
2021-10-16 17:20:06 +02:00
}
2021-11-14 16:46:09 +01:00
2021-10-16 17:20:06 +02:00
return nil
},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()
request := &v1.RenameUserRequest{
OldName: args[0],
NewName: args[1],
2021-10-16 17:20:06 +02:00
}
response, err := client.RenameUser(ctx, request)
2021-10-16 17:20:06 +02:00
if err != nil {
2021-11-13 09:36:45 +01:00
ErrorOutput(
err,
fmt.Sprintf(
"Cannot rename user: %s",
2021-11-13 09:36:45 +01:00
status.Convert(err).Message(),
),
output,
)
2021-11-14 16:46:09 +01:00
2021-10-16 17:20:06 +02:00
return
}
SuccessOutput(response.GetUser(), "User renamed", output)
2021-10-16 17:20:06 +02:00
},
}