Skip to content

Commit f9b859f

Browse files
committed
Made run
1 parent 29383e4 commit f9b859f

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

cmd/run.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

executor/planner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ func (e *Executor) ensureAtLeastOneCommandRunning() error {
230230
}
231231
}
232232

233-
return fmt.Errorf("no tasks are running, potential deadlock detected. Pending tasks: %v", e.getPendingCommands())
233+
return fmt.Errorf("no tasks are running, potential deadlock detected. Pending tasks: %v", e.GetPendingCommands())
234234
}
235235

236-
// getPendingCommands returns a list of names of tasks that are still pending.
237-
func (e *Executor) getPendingCommands() []string {
236+
// GetPendingCommands returns a list of names of tasks that are still pending.
237+
func (e *Executor) GetPendingCommands() []string {
238238
var pendingTasks []string
239239
for name, cmdRun := range e.tasks {
240240
if cmdRun.status == pending {

0 commit comments

Comments
 (0)