forked from cloudposse/atmos
/
worflow.go
111 lines (89 loc) · 2.47 KB
/
worflow.go
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
package exec
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
cfg "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
// ExecuteWorkflowCmd executes a workflow
func ExecuteWorkflowCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("invalid arguments. The command requires one argument `workflow name`")
}
info, err := processCommandLineArgs("terraform", cmd, args)
if err != nil {
return err
}
// InitCliConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}
flags := cmd.Flags()
workflowFile, err := flags.GetString("file")
if err != nil {
return err
}
dryRun, err := flags.GetBool("dry-run")
if err != nil {
return err
}
commandLineStack, err := flags.GetString("stack")
if err != nil {
return err
}
var workflowPath string
if u.IsPathAbsolute(workflowFile) {
workflowPath = workflowFile
} else {
workflowPath = path.Join(cliConfig.BasePath, cliConfig.Workflows.BasePath, workflowFile)
}
// If the file is specified without an extension, use the default extension
ext := filepath.Ext(workflowPath)
if ext == "" {
ext = cfg.DefaultStackConfigFileExtension
workflowPath = workflowPath + ext
}
if !u.FileExists(workflowPath) {
return fmt.Errorf("file '%s' does not exist", workflowPath)
}
fileContent, err := os.ReadFile(workflowPath)
if err != nil {
return err
}
var yamlContent cfg.WorkflowFile
var workflowConfig cfg.WorkflowConfig
var workflowDefinition cfg.WorkflowDefinition
if err = yaml.Unmarshal(fileContent, &yamlContent); err != nil {
return err
}
if i, ok := yamlContent["workflows"]; !ok {
return errors.New("a workflow file must be a map with top-level 'workflows:' key")
} else {
workflowConfig = i
}
workflow := args[0]
if i, ok := workflowConfig[workflow]; !ok {
return fmt.Errorf("the file '%s' does not have the '%s' workflow defined", workflowPath, workflow)
} else {
workflowDefinition = i
}
u.PrintInfo(fmt.Sprintf("\nExecuting the workflow '%s' from '%s'\n", workflow, workflowPath))
fmt.Println()
err = u.PrintAsYAML(workflowDefinition)
if err != nil {
return err
}
err = executeWorkflowSteps(workflow, workflowDefinition, dryRun, commandLineStack)
if err != nil {
return err
}
fmt.Println()
return nil
}