Archived
1
This repository has been archived on 2025-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
playbookctl/cmd/role.go
2024-12-19 23:43:01 +03:00

174 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"github.com/spf13/cobra"
"path/filepath"
sw "playbookctl/internal/space_worker"
"playbookctl/internal/utils/logger"
)
var (
flagBackupAdd bool
)
func NewCommandRole() *cobra.Command {
roleCmd := &cobra.Command{
Use: "role",
Short: "работа с ролями в пространстве",
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}
roleCmd.AddCommand(newCommandRoleCreate())
roleCmd.AddCommand(newCommandRoleRemove())
//roleCmd.AddCommand(newCommandRoleAdd())
roleCmd.AddCommand(newCommandRoleList())
//roleCmd.AddCommand(newCommandRoleExport())
roleCmd.AddCommand(newCommandRoleModify())
return roleCmd
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleCreate() *cobra.Command {
createCmd := &cobra.Command{
Use: "create <name>",
Aliases: []string{"new"},
Short: "создать роль в пространстве",
Args: cobra.ExactArgs(1),
RunE: roleCreateRunE,
}
return createCmd
}
func roleCreateRunE(_ *cobra.Command, args []string) error {
workDir, err := filepath.Abs(flagWorkdir)
if err != nil {
return err
}
spaceWorker := sw.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
return spaceWorker.CreateRole(args[0])
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleRemove() *cobra.Command {
createCmd := &cobra.Command{
Use: "remove <name>",
Aliases: []string{"delete", "rm", "del"},
Short: "удаляет роль из пространства",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ArgRoleCompletion,
RunE: roleRemoveRunE,
}
return createCmd
}
func roleRemoveRunE(_ *cobra.Command, args []string) error {
workDir, err := filepath.Abs(flagWorkdir)
if err != nil {
return err
}
spaceWorker := sw.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
return spaceWorker.RemoveRole(args[0])
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleList() *cobra.Command {
listCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "список ролей в пространстве",
RunE: roleListRunE,
}
return listCmd
}
func roleListRunE(_ *cobra.Command, _ []string) error {
workDir, err := filepath.Abs(flagWorkdir)
if err != nil {
return err
}
spaceWorker := sw.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
return spaceWorker.ListRoles()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleAdd() *cobra.Command {
createCmd := &cobra.Command{
Use: "add",
Short: "добавить роль в пространстве",
RunE: roleAddRunE,
}
return createCmd
}
func roleAddRunE(_ *cobra.Command, args []string) error {
fmt.Println("[dummy] add role")
for _, elm := range args {
fmt.Printf("- %s\n", elm)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleExport() *cobra.Command {
createCmd := &cobra.Command{
Use: "export",
Short: "архивировать роль",
RunE: roleExportRunE,
}
return createCmd
}
func roleExportRunE(_ *cobra.Command, args []string) error {
fmt.Println("[dummy] export role")
for _, elm := range args {
fmt.Printf("- %s\n", elm)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newCommandRoleModify() *cobra.Command {
modifyCmd := &cobra.Command{
Use: "modify <name>",
Short: "модифицировать роль",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ArgRoleCompletion,
RunE: roleModifyRunE,
}
modifyCmd.Flags().BoolVar(&flagBackupAdd, "backup-add", false, "добавить backup функцию")
return modifyCmd
}
func roleModifyRunE(_ *cobra.Command, args []string) error {
workDir, err := filepath.Abs(flagWorkdir)
if err != nil {
return err
}
spaceWorker := sw.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
if flagBackupAdd {
return spaceWorker.ModifyRoleBackupAdd(args[0])
}
return nil
}