-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathworkflow_adapters.go
More file actions
134 lines (102 loc) · 4.43 KB
/
workflow_adapters.go
File metadata and controls
134 lines (102 loc) · 4.43 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
package exec
import (
"context"
"os"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/pkg/auth"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/workflow"
)
// Compile-time interface compliance checks.
var (
_ workflow.CommandRunner = (*WorkflowCommandRunner)(nil)
_ workflow.AuthProvider = (*WorkflowAuthProvider)(nil)
_ workflow.UIProvider = (*WorkflowUIProvider)(nil)
)
// WorkflowCommandRunner implements workflow.CommandRunner using existing exec functions.
type WorkflowCommandRunner struct{}
// NewWorkflowCommandRunner creates a new WorkflowCommandRunner.
// Note: Retry logic should be handled at a higher level if needed.
func NewWorkflowCommandRunner(_ *schema.RetryConfig) *WorkflowCommandRunner {
defer perf.Track(nil, "exec.NewWorkflowCommandRunner")()
return &WorkflowCommandRunner{}
}
// RunShell executes a shell command using ExecuteShell.
// Note: atmosConfig is nil here because workflows call RunAtmos for atmos commands,
// and shell commands don't need atmosConfig for basic execution.
func (r *WorkflowCommandRunner) RunShell(command, name, dir string, env []string, dryRun bool) error {
defer perf.Track(nil, "exec.WorkflowCommandRunner.RunShell")()
return ExecuteShell(nil, command, name, dir, env, dryRun)
}
// RunAtmos executes an atmos command using ExecuteShellCommand.
// Note: Retry logic should be handled at a higher level if needed.
func (r *WorkflowCommandRunner) RunAtmos(params *workflow.AtmosExecParams) error {
defer perf.Track(nil, "exec.WorkflowCommandRunner.RunAtmos")()
if params == nil || params.AtmosConfig == nil {
return errUtils.ErrNilParam
}
return ExecuteShellCommand(*params.AtmosConfig, "atmos", params.Args, params.Dir, params.Env, params.DryRun, "")
}
// WorkflowAuthProvider implements workflow.AuthProvider using auth.AuthManager.
type WorkflowAuthProvider struct {
manager auth.AuthManager
}
// NewWorkflowAuthProvider creates a new WorkflowAuthProvider with the given auth manager.
func NewWorkflowAuthProvider(manager auth.AuthManager) *WorkflowAuthProvider {
defer perf.Track(nil, "exec.NewWorkflowAuthProvider")()
return &WorkflowAuthProvider{
manager: manager,
}
}
// NeedsAuth returns true if authentication is needed for the given steps.
func (p *WorkflowAuthProvider) NeedsAuth(steps []schema.WorkflowStep, commandLineIdentity string) bool {
defer perf.Track(nil, "exec.WorkflowAuthProvider.NeedsAuth")()
if commandLineIdentity != "" {
return true
}
for _, step := range steps {
if step.Identity != "" {
return true
}
}
return false
}
// Authenticate performs authentication for the given identity.
func (p *WorkflowAuthProvider) Authenticate(ctx context.Context, identity string) error {
defer perf.Track(nil, "exec.WorkflowAuthProvider.Authenticate")()
_, err := p.manager.Authenticate(ctx, identity)
return err
}
// GetCachedCredentials returns cached credentials for the identity.
func (p *WorkflowAuthProvider) GetCachedCredentials(ctx context.Context, identity string) (any, error) {
defer perf.Track(nil, "exec.WorkflowAuthProvider.GetCachedCredentials")()
return p.manager.GetCachedCredentials(ctx, identity)
}
// PrepareEnvironment prepares environment variables for the authenticated identity.
func (p *WorkflowAuthProvider) PrepareEnvironment(ctx context.Context, identity string, baseEnv []string) ([]string, error) {
defer perf.Track(nil, "exec.WorkflowAuthProvider.PrepareEnvironment")()
// If no base env provided, use current OS environment.
if baseEnv == nil {
baseEnv = os.Environ()
}
return p.manager.PrepareShellEnvironment(ctx, identity, baseEnv)
}
// WorkflowUIProvider implements workflow.UIProvider using TUI utilities.
type WorkflowUIProvider struct{}
// NewWorkflowUIProvider creates a new WorkflowUIProvider.
func NewWorkflowUIProvider() *WorkflowUIProvider {
defer perf.Track(nil, "exec.NewWorkflowUIProvider")()
return &WorkflowUIProvider{}
}
// PrintMessage prints a message to the TUI.
func (p *WorkflowUIProvider) PrintMessage(format string, args ...any) {
defer perf.Track(nil, "exec.WorkflowUIProvider.PrintMessage")()
u.PrintfMessageToTUI(format, args...)
}
// PrintError prints an error using the error utilities.
func (p *WorkflowUIProvider) PrintError(err error, title, explanation string) {
defer perf.Track(nil, "exec.WorkflowUIProvider.PrintError")()
errUtils.CheckErrorAndPrint(err, title, explanation)
}