137 lines
3.4 KiB
Go
137 lines
3.4 KiB
Go
package cmd
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/spf13/cobra"
|
||
"path/filepath"
|
||
sw "playbookctl/internal/space_worker"
|
||
"playbookctl/internal/utils/logger"
|
||
)
|
||
|
||
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())
|
||
|
||
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",
|
||
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
|
||
}
|