|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "gitte/config" |
| 6 | + "gitte/internal" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +// actionsCmd represents the actions command |
| 12 | +var runCmd = &cobra.Command{ |
| 13 | + Use: "run <action> <group> [project]", |
| 14 | + Annotations: map[string]string{ |
| 15 | + "need-config": "true", |
| 16 | + }, |
| 17 | + Short: "Execute actions on projects in groups after running startup checks and gitops", |
| 18 | + Args: cobra.MinimumNArgs(2), |
| 19 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 20 | + ctx := cmd.Context() |
| 21 | + cfg := config.ConfigFromContext(ctx) |
| 22 | + |
| 23 | + // Parse and validate action argument |
| 24 | + if args[0] == "" { |
| 25 | + return fmt.Errorf("action cannot be empty") |
| 26 | + } |
| 27 | + actionString := args[0] |
| 28 | + |
| 29 | + // Parse and validate group argument |
| 30 | + if args[1] == "" { |
| 31 | + return fmt.Errorf("group cannot be empty") |
| 32 | + } |
| 33 | + groupString := args[1] |
| 34 | + |
| 35 | + // Parse and validate project argument. If project is not provided, use '*' to indicate all projects. |
| 36 | + projectString := "*" |
| 37 | + if len(args) > 2 { |
| 38 | + if args[2] == "" { |
| 39 | + return fmt.Errorf("project cannot be empty") |
| 40 | + } |
| 41 | + projectString = args[2] |
| 42 | + } |
| 43 | + |
| 44 | + fmt.Printf("Executing action [%s] for group [%s] on project [%s]\n", actionString, groupString, projectString) |
| 45 | + executor := internal.CreateActionExecutors(*cfg, true, actionString, groupString, projectString) |
| 46 | + |
| 47 | + // Check that executors are valid before running startup checks and gitops, to fail fast. |
| 48 | + // They are valid if there is at least one executor to run, and all executors have >0 tasks to execute. |
| 49 | + if len(executor) == 0 { |
| 50 | + return fmt.Errorf("no executors found for action '%s', group '%s', project '%s'", actionString, groupString, projectString) |
| 51 | + } |
| 52 | + for _, actionExecutor := range executor { |
| 53 | + if len(actionExecutor.Executor.GetPendingCommands()) == 0 { |
| 54 | + return fmt.Errorf("no tasks to execute for action '%s' on group '%s', project '%s'", actionExecutor.Action, groupString, projectString) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Run startup checks and gitops before executing actions |
| 59 | + if err := internal.PerformStartupChecks(ctx, config.CwdFromContext(ctx), *cfg); err != nil { |
| 60 | + return fmt.Errorf("startup checks failed: %w", err) |
| 61 | + } |
| 62 | + fmt.Println("Startup checks completed successfully.") |
| 63 | + if err := internal.GitOps(ctx, config.CwdFromContext(ctx), *cfg); err != nil { |
| 64 | + return fmt.Errorf("gitops failed: %w", err) |
| 65 | + } |
| 66 | + fmt.Println("GitOps completed successfully.") |
| 67 | + |
| 68 | + for _, actionExecutor := range executor { |
| 69 | + fmt.Printf("Running executor for action: %s\n", actionExecutor.Action) |
| 70 | + if err := actionExecutor.Executor.Execute(ctx); err != nil { |
| 71 | + return fmt.Errorf("failed to execute action '%s': %w", actionExecutor.Action, err) |
| 72 | + } |
| 73 | + fmt.Printf("Successfully executed action: %s\n", actionExecutor.Action) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +func init() { |
| 81 | + rootCmd.AddCommand(runCmd) |
| 82 | +} |
0 commit comments