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-23 01:55:48 +03:00

131 lines
3.4 KiB
Go
Raw Permalink 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 (
"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(newCommandRoleList())
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 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
}