-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathworkflow_utils.go
More file actions
718 lines (612 loc) · 24.4 KB
/
workflow_utils.go
File metadata and controls
718 lines (612 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package exec
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
"github.com/samber/lo"
"mvdan.cc/sh/v3/shell"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/internal/tui/templates/term"
uiutils "github.com/cloudposse/atmos/internal/tui/utils"
w "github.com/cloudposse/atmos/internal/tui/workflow"
"github.com/cloudposse/atmos/pkg/auth"
"github.com/cloudposse/atmos/pkg/auth/credentials"
"github.com/cloudposse/atmos/pkg/auth/validation"
"github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/dependencies"
envpkg "github.com/cloudposse/atmos/pkg/env"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/retry"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/telemetry"
"github.com/cloudposse/atmos/pkg/ui"
u "github.com/cloudposse/atmos/pkg/utils"
)
// Workflow error title for formatted output.
const WorkflowErrTitle = "Workflow Error"
// Local errors not in shared package (workflow-specific internal errors).
var (
ErrNoWorkflowFilesToSelect = errors.New("no workflow files to select from")
ErrNonTTYWorkflowSelection = errors.New("interactive workflow selection not available in non-TTY or CI environments")
)
// KnownWorkflowErrors contains all known workflow sentinel errors for error handling.
var KnownWorkflowErrors = []error{
errUtils.ErrWorkflowNoSteps,
errUtils.ErrInvalidWorkflowStepType,
errUtils.ErrInvalidFromStep,
errUtils.ErrWorkflowStepFailed,
errUtils.ErrWorkflowNoWorkflow,
errUtils.ErrWorkflowFileNotFound,
errUtils.ErrInvalidWorkflowManifest,
}
// workflowStepErrorContext contains context needed to build workflow step errors.
type workflowStepErrorContext struct {
WorkflowPath string
WorkflowBasePath string
Workflow string
StepName string
Command string
CommandType string
FinalStack string
}
// buildWorkflowStepError builds an error with resume hints when a workflow step fails.
func buildWorkflowStepError(err error, ctx *workflowStepErrorContext) error {
log.Debug("Workflow failed", "error", err)
// Remove the workflow base path, stacks/workflows.
workflowFileName := strings.TrimPrefix(filepath.ToSlash(ctx.WorkflowPath), filepath.ToSlash(ctx.WorkflowBasePath))
// Remove the leading slash.
workflowFileName = strings.TrimPrefix(workflowFileName, "/")
// Remove the file extension.
workflowFileName = strings.TrimSuffix(workflowFileName, filepath.Ext(workflowFileName))
resumeCommand := fmt.Sprintf(
"%s workflow %s -f %s --from-step '%s'",
config.AtmosCommand,
ctx.Workflow,
workflowFileName,
ctx.StepName,
)
// Add stack parameter to resume command if a stack was used.
if ctx.FinalStack != "" {
resumeCommand = fmt.Sprintf("%s -s '%s'", resumeCommand, ctx.FinalStack)
}
failedCmd := ctx.Command
if ctx.CommandType == config.AtmosCommand {
failedCmd = config.AtmosCommand + " " + ctx.Command
// Add stack parameter to failed command if a stack was used.
if ctx.FinalStack != "" {
failedCmd = fmt.Sprintf("%s -s '%s'", failedCmd, ctx.FinalStack)
}
}
// Build error with context about the failed command.
// Use fmt.Errorf with %w to wrap the underlying error while adding ErrWorkflowStepFailed to the chain.
// This preserves both the error sentinel for errors.Is() checks and the underlying error's exit code.
wrappedErr := fmt.Errorf("%w: %w", errUtils.ErrWorkflowStepFailed, err)
// Now build the error with hints using the wrapped error.
// This preserves the error chain while adding formatted hints.
// Commands are wrapped in code fences for proper formatting and copy-paste.
// Single quotes are used for shell safety (step names and stacks can contain spaces).
builder := errUtils.Build(wrappedErr).
WithTitle("Workflow Error").
WithHintf("The following command failed to execute:\n\n```shell\n%s\n```", failedCmd).
WithHintf("To resume the workflow from this step, run:\n\n```shell\n%s\n```", resumeCommand)
// Extract exit code from the underlying error if available.
if exitCode := errUtils.GetExitCode(err); exitCode != 0 {
builder = builder.WithExitCode(exitCode)
}
return builder.Err()
}
// prepareStepEnvironment prepares environment variables for a workflow step.
// Starts with system env + global env from atmos.yaml.
// If identity is specified, it authenticates and adds credentials to the environment.
// Returns the environment variables to use for the step.
func prepareStepEnvironment(
stepIdentity string,
stepName string,
authManager auth.AuthManager,
globalEnv map[string]string,
) ([]string, error) {
// Prepare base environment: system env + global env from atmos.yaml.
// Global env has lowest priority and can be overridden by identity auth env vars.
baseEnv := envpkg.MergeGlobalEnv(os.Environ(), globalEnv)
// No identity specified, use base environment (system + global env).
if stepIdentity == "" {
return baseEnv, nil
}
if authManager == nil {
return nil, errUtils.Build(errUtils.ErrAuthManager).
WithExplanation("auth manager is not initialized").
WithContext("identity", stepIdentity).
WithContext("step", stepName).
Err()
}
ctx := context.Background()
// Try to use cached credentials first (passive check, no prompts).
// Only authenticate if cached credentials are not available or expired.
if _, err := authManager.GetCachedCredentials(ctx, stepIdentity); err != nil {
log.Debug("No valid cached credentials found, authenticating", "identity", stepIdentity, "error", err)
// No valid cached credentials - perform full authentication.
if _, err = authManager.Authenticate(ctx, stepIdentity); err != nil {
// Check for user cancellation - return clean error without wrapping.
if errors.Is(err, errUtils.ErrUserAborted) {
return nil, errUtils.ErrUserAborted
}
return nil, fmt.Errorf("%w for identity %q in step %q: %w", errUtils.ErrAuthenticationFailed, stepIdentity, stepName, err)
}
}
// Prepare shell environment with authentication credentials.
// Start with base environment (system + global) and let PrepareShellEnvironment configure auth.
stepEnv, err := authManager.PrepareShellEnvironment(ctx, stepIdentity, baseEnv)
if err != nil {
return nil, fmt.Errorf("failed to prepare shell environment for identity %q in step %q: %w", stepIdentity, stepName, err)
}
log.Debug("Prepared environment with identity", "identity", stepIdentity, "step", stepName)
return stepEnv, nil
}
// IsKnownWorkflowError returns true if the error matches any known workflow error.
// This includes ExitCodeError which indicates a subcommand failure that's already been reported.
func IsKnownWorkflowError(err error) bool {
// Check if it's an ExitCodeError - these are already reported by the subcommand
var exitCodeErr errUtils.ExitCodeError
if errors.As(err, &exitCodeErr) {
return true
}
// Check known workflow errors
for _, knownErr := range KnownWorkflowErrors {
if errors.Is(err, knownErr) {
return true
}
}
return false
}
// checkAndMergeDefaultIdentity checks if there's a default identity configured in atmos.yaml or stack configs.
// If a default identity is found in stack configs, it merges it into atmosConfig.Auth.
// Stack defaults take precedence over atmos.yaml defaults (following Atmos inheritance model).
// Returns true if a default identity exists after merging.
func checkAndMergeDefaultIdentity(atmosConfig *schema.AtmosConfiguration) bool {
if len(atmosConfig.Auth.Identities) == 0 {
return false
}
// Always load stack configs - stack defaults take precedence over atmos.yaml.
stackDefaults, err := config.LoadStackAuthDefaults(atmosConfig)
if err != nil {
// On error, fall back to checking atmos.yaml defaults.
for _, identity := range atmosConfig.Auth.Identities {
if identity.Default {
return true
}
}
return false
}
// Merge stack defaults into auth config (stack takes precedence).
if len(stackDefaults) > 0 {
config.MergeStackAuthDefaults(&atmosConfig.Auth, stackDefaults)
}
// Check if we have a default after merging.
for _, identity := range atmosConfig.Auth.Identities {
if identity.Default {
return true
}
}
return false
}
// ExecuteWorkflow executes an Atmos workflow.
func ExecuteWorkflow(
atmosConfig schema.AtmosConfiguration,
workflow string,
workflowPath string,
workflowDefinition *schema.WorkflowDefinition,
dryRun bool,
commandLineStack string,
fromStep string,
commandLineIdentity string,
) error {
defer perf.Track(&atmosConfig, "exec.ExecuteWorkflow")()
steps := workflowDefinition.Steps
if len(steps) == 0 {
return errUtils.Build(errUtils.ErrWorkflowNoSteps).
WithTitle(WorkflowErrTitle).
WithExplanationf("Workflow `%s` is empty and requires at least one step to execute.", workflow).
WithContext("workflow", workflow).
WithExitCode(1).
Err()
}
// Check if the workflow steps have the `name` attribute
checkAndGenerateWorkflowStepNames(workflowDefinition)
log.Debug("Executing workflow", "workflow", workflow, "path", workflowPath)
if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug {
err := u.PrintAsYAMLToFileDescriptor(&atmosConfig, workflowDefinition)
if err != nil {
return err
}
}
// If `--from-step` is specified, skip all the previous steps
if fromStep != "" {
steps = lo.DropWhile[schema.WorkflowStep](steps, func(step schema.WorkflowStep) bool {
return step.Name != fromStep
})
if len(steps) == 0 {
stepNames := lo.Map(workflowDefinition.Steps, func(step schema.WorkflowStep, _ int) string { return step.Name })
return errUtils.Build(errUtils.ErrInvalidFromStep).
WithTitle(WorkflowErrTitle).
WithExplanationf("The `--from-step` flag was set to `%s`, but this step does not exist in workflow `%s`.\n\n### Available steps:\n\n%s", fromStep, workflow, u.FormatList(stepNames)).
WithContext("from_step", fromStep).
WithContext("workflow", workflow).
WithExitCode(1).
Err()
}
}
// Ensure toolchain dependencies are installed and build PATH for workflow steps.
toolchainPATH, err := ensureWorkflowToolchainDependencies(&atmosConfig, workflowDefinition)
if err != nil {
return err
}
// Create auth manager if any step has an identity or if command-line identity is specified.
// We check once upfront to avoid repeated initialization.
var authManager auth.AuthManager
var authStackInfo *schema.ConfigAndStacksInfo
needsAuth := commandLineIdentity != "" || lo.SomeBy(steps, func(step schema.WorkflowStep) bool {
return strings.TrimSpace(step.Identity) != ""
})
if needsAuth {
// Create a ConfigAndStacksInfo for the auth manager to populate with AuthContext.
// This enables YAML template functions to access authenticated credentials.
authStackInfo = &schema.ConfigAndStacksInfo{
AuthContext: &schema.AuthContext{},
}
credStore := credentials.NewCredentialStore()
validator := validation.NewValidator()
var err error
authManager, err = auth.NewAuthManager(&atmosConfig.Auth, credStore, validator, authStackInfo)
if err != nil {
return fmt.Errorf("%w: %w", errUtils.ErrFailedToInitializeAuthManager, err)
}
}
for stepIdx, step := range steps {
command := strings.TrimSpace(step.Command)
commandType := strings.TrimSpace(step.Type)
stepIdentity := strings.TrimSpace(step.Identity)
// If step doesn't specify identity, use command-line identity (if provided).
if stepIdentity == "" && commandLineIdentity != "" {
stepIdentity = commandLineIdentity
}
finalStack := ""
log.Debug("Executing workflow step", "step", stepIdx, "name", step.Name, "command", command)
if commandType == "" {
commandType = "atmos"
}
// Prepare environment variables: start with system env + global env from atmos.yaml.
// If identity is specified, also authenticate and add credentials.
stepEnv, err := prepareStepEnvironment(stepIdentity, step.Name, authManager, atmosConfig.GetCaseSensitiveEnvVars())
if err != nil {
return err
}
// Add toolchain PATH if dependencies were installed.
if toolchainPATH != "" {
stepEnv = append(stepEnv, fmt.Sprintf("PATH=%s", toolchainPATH))
}
switch commandType {
case "shell":
commandName := fmt.Sprintf("%s-step-%d", workflow, stepIdx)
err = ExecuteShell(&atmosConfig, command, commandName, ".", stepEnv, dryRun)
case "atmos":
// Parse command using shell.Fields for proper quote handling.
// This correctly handles arguments like -var="foo=bar" by stripping quotes.
args, parseErr := shell.Fields(command, nil)
if parseErr != nil {
log.Debug("Shell parsing failed, falling back to strings.Fields", "error", parseErr, "command", command)
args = strings.Fields(command)
}
workflowStack := strings.TrimSpace(workflowDefinition.Stack)
stepStack := strings.TrimSpace(step.Stack)
// The workflow `stack` attribute overrides the stack in the `command` (if specified)
// The step `stack` attribute overrides the stack in the `command` and the workflow `stack` attribute
// The stack defined on the command line (`atmos workflow <name> -f <file> -s <stack>`) has the highest priority,
// it overrides all other stacks attributes
if workflowStack != "" {
finalStack = workflowStack
}
if stepStack != "" {
finalStack = stepStack
}
if commandLineStack != "" {
finalStack = commandLineStack
}
if finalStack != "" {
if idx := slices.Index(args, "--"); idx != -1 {
// Insert before the "--"
// Take everything up to idx, then add "-s", finalStack, then tack on the rest
args = append(args[:idx], append([]string{"-s", finalStack}, args[idx:]...)...)
} else {
// just append at the end
args = append(args, []string{"-s", finalStack}...)
}
log.Debug("Using stack", "stack", finalStack)
}
_ = ui.Infof("Executing command: `atmos %s`", command)
err = retry.With7Params(context.Background(), step.Retry,
ExecuteShellCommand,
atmosConfig, "atmos", args, ".", stepEnv, dryRun, "")
default:
return errUtils.Build(errUtils.ErrInvalidWorkflowStepType).
WithTitle(WorkflowErrTitle).
WithHintf("Step type '%s' is not supported", commandType).
WithHint("Each step must specify a valid type: 'atmos' or 'shell'").
WithExitCode(1).
Err()
}
if err != nil {
return buildWorkflowStepError(err, &workflowStepErrorContext{
WorkflowPath: workflowPath,
WorkflowBasePath: atmosConfig.Workflows.BasePath,
Workflow: workflow,
StepName: step.Name,
Command: command,
CommandType: commandType,
FinalStack: finalStack,
})
}
}
return nil
}
// ExecuteDescribeWorkflows executes `atmos describe workflows` command.
func ExecuteDescribeWorkflows(
atmosConfig schema.AtmosConfiguration,
) ([]schema.DescribeWorkflowsItem, map[string][]string, map[string]schema.WorkflowManifest, error) {
defer perf.Track(&atmosConfig, "exec.ExecuteDescribeWorkflows")()
listResult := []schema.DescribeWorkflowsItem{}
mapResult := make(map[string][]string)
allResult := make(map[string]schema.WorkflowManifest)
if atmosConfig.Workflows.BasePath == "" {
return nil, nil, nil, errUtils.ErrWorkflowBasePathNotConfigured
}
// If `workflows.base_path` is a relative path, join it with `stacks.base_path`
var workflowsDir string
if u.IsPathAbsolute(atmosConfig.Workflows.BasePath) {
workflowsDir = atmosConfig.Workflows.BasePath
} else {
workflowsDir = filepath.Join(atmosConfig.BasePath, atmosConfig.Workflows.BasePath)
}
isDirectory, err := u.IsDirectory(workflowsDir)
if err != nil || !isDirectory {
return nil, nil, nil, fmt.Errorf("the workflow directory '%s' does not exist. Review 'workflows.base_path' in 'atmos.yaml'", workflowsDir)
}
files, err := u.GetAllYamlFilesInDir(workflowsDir)
if err != nil {
return nil, nil, nil, fmt.Errorf("error reading the directory '%s' defined in 'workflows.base_path' in 'atmos.yaml': %v",
atmosConfig.Workflows.BasePath, err)
}
for _, f := range files {
var workflowPath string
if u.IsPathAbsolute(atmosConfig.Workflows.BasePath) {
workflowPath = filepath.Join(atmosConfig.Workflows.BasePath, f)
} else {
workflowPath = filepath.Join(atmosConfig.BasePath, atmosConfig.Workflows.BasePath, f)
}
fileContent, err := os.ReadFile(workflowPath)
if err != nil {
// Skip files that can't be read (permission issues, etc.).
log.Warn("Skipping workflow file", "file", f, "error", err)
continue
}
workflowManifest, err := u.UnmarshalYAML[schema.WorkflowManifest](string(fileContent))
if err != nil {
// Skip files that can't be parsed as YAML.
log.Warn("Skipping invalid workflow file", "file", f, "error", err)
continue
}
if workflowManifest.Workflows == nil {
// Skip files without the workflows key.
log.Warn("Skipping workflow file without 'workflows:' key", "file", f)
continue
}
workflowConfig := workflowManifest.Workflows
allWorkflowsInFile := lo.Keys(workflowConfig)
sort.Strings(allWorkflowsInFile)
// Check if the workflow steps have the `name` attribute
lo.ForEach(allWorkflowsInFile, func(item string, _ int) {
workflowDefinition := workflowConfig[item]
checkAndGenerateWorkflowStepNames(&workflowDefinition)
})
mapResult[f] = allWorkflowsInFile
allResult[f] = workflowManifest
}
for k, v := range mapResult {
for _, w := range v {
listResult = append(listResult, schema.DescribeWorkflowsItem{
File: k,
Workflow: w,
})
}
}
return listResult, mapResult, allResult, nil
}
// WorkflowMatch represents a workflow found during auto-discovery.
type WorkflowMatch struct {
File string // Workflow file name (e.g., "networking.yaml")
Name string // Workflow name (e.g., "deploy-all")
Description string // Workflow description (if available)
}
// findWorkflowAcrossFiles searches for a workflow by name across all workflow files.
// Returns a list of matching workflows with their file locations.
func findWorkflowAcrossFiles(workflowName string, atmosConfig *schema.AtmosConfiguration) ([]WorkflowMatch, error) {
defer perf.Track(atmosConfig, "exec.findWorkflowAcrossFiles")()
listResult, _, allWorkflows, err := ExecuteDescribeWorkflows(*atmosConfig)
if err != nil {
return nil, err
}
var matches []WorkflowMatch
for _, item := range listResult {
if item.Workflow == workflowName {
// Get description if available.
description := ""
if manifest, ok := allWorkflows[item.File]; ok {
if workflowDef, ok := manifest.Workflows[workflowName]; ok {
description = workflowDef.Description
}
}
matches = append(matches, WorkflowMatch{
File: item.File,
Name: workflowName,
Description: description,
})
}
}
return matches, nil
}
// promptForWorkflowFile shows an interactive selector for choosing a workflow file.
// Uses the Huh library with Atmos theme (same pattern as identity selector).
func promptForWorkflowFile(matches []WorkflowMatch) (string, error) {
defer perf.Track(nil, "exec.promptForWorkflowFile")()
if len(matches) == 0 {
return "", ErrNoWorkflowFilesToSelect
}
// Check if we're in a TTY environment.
if !term.IsTTYSupportForStdin() || telemetry.IsCI() {
return "", ErrNonTTYWorkflowSelection
}
// Sort matches alphabetically by file name for consistent ordering.
sortedMatches := make([]WorkflowMatch, len(matches))
copy(sortedMatches, matches)
sort.Slice(sortedMatches, func(i, j int) bool {
return sortedMatches[i].File < sortedMatches[j].File
})
// Build options for the selector.
// Each option shows the file name with description if available.
options := make([]huh.Option[string], len(sortedMatches))
for i, match := range sortedMatches {
label := match.File
if match.Description != "" {
label = fmt.Sprintf("%s - %s", match.File, match.Description)
}
options[i] = huh.NewOption(label, match.File)
}
var selectedFile string
// Create custom keymap that adds ESC to quit keys.
keyMap := huh.NewDefaultKeyMap()
keyMap.Quit = key.NewBinding(
key.WithKeys("ctrl+c", "esc"),
key.WithHelp("ctrl+c/esc", "quit"),
)
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title(fmt.Sprintf("Multiple workflows found with name '%s'. Please choose:", sortedMatches[0].Name)).
Description("Press ctrl+c or esc to exit").
Options(options...).
Value(&selectedFile),
),
).WithKeyMap(keyMap).WithTheme(uiutils.NewAtmosHuhTheme())
if err := form.Run(); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
return "", errUtils.ErrUserAborted
}
return "", fmt.Errorf("workflow selection failed: %w", err)
}
return selectedFile, nil
}
func checkAndGenerateWorkflowStepNames(workflowDefinition *schema.WorkflowDefinition) {
steps := workflowDefinition.Steps
if steps == nil {
return
}
// Check if the steps have the `name` attribute.
// If not, generate a friendly name consisting of a prefix of `step` and followed by the index of the
// step (the index starts with 1, so the first generated step name would be `step1`)
for index, step := range steps {
if step.Name == "" {
// When iterating through a slice with a range loop, if elements need to be changed,
// changing the returned value from the range is not changing the original slice element.
// That return value is a copy of the element.
// So doing changes to it will not affect the original elements.
// We need to access the element with the index returned from the range iterator and change it there.
// https://medium.com/@nsspathirana/common-mistakes-with-go-slices-95f2e9b362a9
steps[index].Name = fmt.Sprintf("step%d", index+1)
}
}
}
// ensureWorkflowToolchainDependencies loads and installs toolchain dependencies for a workflow.
// It loads tools from .tool-versions, merges with workflow-specific dependencies, installs missing
// tools, and returns the PATH string with toolchain binaries prepended.
func ensureWorkflowToolchainDependencies(
atmosConfig *schema.AtmosConfiguration,
workflowDefinition *schema.WorkflowDefinition,
) (string, error) {
defer perf.Track(atmosConfig, "exec.ensureWorkflowToolchainDependencies")()
// Load project-wide tools from .tool-versions.
toolVersionsDeps, err := dependencies.LoadToolVersionsDependencies(atmosConfig)
if err != nil {
return "", errUtils.Build(errUtils.ErrDependencyResolution).
WithCause(err).
WithExplanation("Failed to load .tool-versions file").
Err()
}
// Get workflow-specific dependencies.
resolver := dependencies.NewResolver(atmosConfig)
workflowDeps, err := resolver.ResolveWorkflowDependencies(workflowDefinition)
if err != nil {
return "", errUtils.Build(errUtils.ErrDependencyResolution).
WithCause(err).
WithExplanation("Failed to resolve workflow dependencies").
Err()
}
// Merge: .tool-versions as base, workflow deps override.
deps, err := dependencies.MergeDependencies(toolVersionsDeps, workflowDeps)
if err != nil {
return "", errUtils.Build(errUtils.ErrDependencyResolution).
WithCause(err).
WithExplanation("Failed to merge dependencies").
Err()
}
if len(deps) == 0 {
return "", nil
}
log.Debug("Installing workflow dependencies", "tools", deps)
// Install missing tools.
installer := dependencies.NewInstaller(atmosConfig)
if err := installer.EnsureTools(deps); err != nil {
return "", errUtils.Build(errUtils.ErrToolInstall).
WithCause(err).
WithExplanation("Failed to install workflow dependencies").
Err()
}
// Build PATH with toolchain binaries.
toolchainPATH, err := dependencies.BuildToolchainPATH(atmosConfig, deps)
if err != nil {
return "", errUtils.Build(errUtils.ErrDependencyResolution).
WithCause(err).
WithExplanation("Failed to build toolchain PATH").
Err()
}
return toolchainPATH, nil
}
func ExecuteWorkflowUI(atmosConfig schema.AtmosConfiguration) (string, string, string, error) {
defer perf.Track(&atmosConfig, "exec.ExecuteWorkflowUI")()
_, _, allWorkflows, err := ExecuteDescribeWorkflows(atmosConfig)
if err != nil {
return "", "", "", err
}
// Start the UI
app, err := w.Execute(allWorkflows)
fmt.Println()
if err != nil {
return "", "", "", err
}
selectedWorkflowFile := app.GetSelectedWorkflowFile()
selectedWorkflow := app.GetSelectedWorkflow()
selectedWorkflowStep := app.GetSelectedWorkflowStep()
// If the user quit the UI, exit
if app.ExitStatusQuit() || selectedWorkflowFile == "" || selectedWorkflow == "" {
return "", "", "", nil
}
c := fmt.Sprintf("atmos workflow %s --file %s --from-step \"%s\"", selectedWorkflow, selectedWorkflowFile, selectedWorkflowStep)
log.Info("Executing", "command", c)
return selectedWorkflowFile, selectedWorkflow, selectedWorkflowStep, nil
}