-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
worflow.go
100 lines (81 loc) · 2.17 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
package exec
import (
"errors"
"fmt"
c "github.com/cloudposse/atmos/pkg/config"
g "github.com/cloudposse/atmos/pkg/globals"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/fatih/color"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
"path"
"path/filepath"
)
// ExecuteWorkflow executes a workflow
func ExecuteWorkflow(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("invalid arguments. The command requires one argument `workflow name`")
}
err := c.InitConfig()
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
}
var workflowPath string
if u.IsPathAbsolute(workflowFile) {
workflowPath = workflowFile
} else {
workflowPath = path.Join(c.Config.BasePath, c.Config.Workflows.BasePath, workflowFile)
}
// If the file is specified without an extension, use the default extension
ext := filepath.Ext(workflowPath)
if ext == "" {
ext = g.DefaultStackConfigFileExtension
workflowPath = workflowPath + ext
}
if !u.FileExists(workflowPath) {
return errors.New(fmt.Sprintf("File '%s' does not exist", workflowPath))
}
fileContent, err := ioutil.ReadFile(workflowPath)
if err != nil {
return err
}
var yamlContent c.WorkflowFile
var workflowConfig c.WorkflowConfig
var workflowDefinition c.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 errors.New(fmt.Sprintf("the file '%s' does not have the '%s' workflow defined", workflowPath, workflow))
} else {
workflowDefinition = i
}
color.Cyan("\nExecuting the workflow '%s' from '%s'\n", workflow, workflowPath)
fmt.Println()
err = u.PrintAsYAML(workflowDefinition)
if err != nil {
return err
}
err = executeWorkflowSteps(workflowDefinition, dryRun)
if err != nil {
return err
}
fmt.Println()
return nil
}