authentricity/internal/admintool/group.go

72 lines
1.3 KiB
Go

package admintool
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.e43.eu/authentricity/internal/models"
"go.e43.eu/authentricity/internal/store"
)
var createGroupCmd = &cobra.Command{
Use: "create",
Short: "Interactive group creation tool",
RunE: func(_ *cobra.Command, args []string) error {
cst, err := store.NewConsulStore(zap.L())
if err != nil {
return err
}
groupname, err := query("Group Name", validateUsername)
if err != nil {
return err
}
description, err := query("Description", nil)
if err != nil {
return err
}
_, disp, err := (&promptui.Select{
Label: "Disposition",
Items: []models.Disposition{
models.DispositionSystem,
models.DispositionRegular,
},
}).Run()
if err != nil {
return err
}
group := &models.GroupRecord{
UUID: uuid.New(),
GroupName: groupname,
Description: description,
Disposition: models.Disposition(disp),
}
_, err = cst.CreateEntity(context.Background(), group)
if err != nil {
return err
}
fmt.Printf("Created (ID %s)\n", group.ID())
return nil
},
}
var groupCmd = &cobra.Command{
Use: "group",
Short: "Group tools",
}
func init() {
groupCmd.AddCommand(createGroupCmd)
rootCmd.AddCommand(groupCmd)
}