-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmd.go
More file actions
297 lines (258 loc) · 8.49 KB
/
Copy pathcmd.go
File metadata and controls
297 lines (258 loc) · 8.49 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
package cmd
import (
"fmt"
"os"
"github.com/AlexanderGrooff/spage/pkg/common"
"github.com/spf13/cobra"
"github.com/AlexanderGrooff/spage/pkg"
"github.com/AlexanderGrooff/spage/pkg/config"
_ "github.com/AlexanderGrooff/spage/pkg/modules" // Register modules
)
var (
playbookFile string
outputFile string
inventoryFile string
configFile string
tags []string
skipTags []string
cfg *config.Config // Store the loaded config
checkMode bool
diffMode bool
)
// LoadConfig loads the configuration and applies settings
var LoadConfig = func(configFile string) error {
configPaths := []string{}
if configFile == "" {
defaultConfig := "spage.yaml"
if _, err := os.Stat(defaultConfig); err == nil {
configPaths = append(configPaths, defaultConfig)
}
} else {
configPaths = append(configPaths, configFile)
}
var err error
cfg, err = config.Load(configPaths...)
if err != nil {
if configFile != "" || !os.IsNotExist(err) {
return fmt.Errorf("failed to load configuration from %v: %w", configPaths, err)
}
// If no specific config file found/specified, try loading defaults
cfg, err = config.Load() // Load defaults
if err != nil {
return fmt.Errorf("failed to load default configuration: %w", err)
}
}
// Apply logging configuration AFTER loading config
common.SetLogLevel(cfg.Logging.Level)
if cfg.Logging.File != "" {
if err := common.SetLogFile(cfg.Logging.File); err != nil {
return fmt.Errorf("error setting log file: %w", err)
}
}
// Call SetLogFormat with the loaded logging config
if err := common.SetLogFormat(cfg.Logging); err != nil {
return fmt.Errorf("error setting log format: %w", err)
}
return nil
}
var RootCmd = &cobra.Command{
Use: "spage",
Short: "Simple Playbook AGEnt",
Long: `A lightweight configuration management tool that compiles your playbooks into a Go program to run on a host.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return LoadConfig(configFile)
},
}
func GetGraph(playbookFile string, tags, skipTags []string, baseConfig *config.Config) (pkg.Graph, error) {
// Override config with command line flags if provided
if len(tags) > 0 {
baseConfig.Tags.Tags = tags
}
if len(skipTags) > 0 {
baseConfig.Tags.SkipTags = skipTags
}
graph, err := pkg.NewGraphFromFile(playbookFile)
if err != nil {
return pkg.Graph{}, fmt.Errorf("failed to generate graph from playbook: %w", err)
}
// Apply tag filtering to the graph
filteredGraph, err := applyTagFiltering(graph, baseConfig.Tags)
if err != nil {
return pkg.Graph{}, fmt.Errorf("failed to apply tag filtering: %w", err)
}
return filteredGraph, nil
}
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate a graph from a playbook and save it as Go code",
RunE: func(cmd *cobra.Command, args []string) error {
graph, err := GetGraph(playbookFile, tags, skipTags, cfg)
if err != nil {
common.LogError("Failed to generate graph", map[string]interface{}{
"error": err.Error(),
})
os.Exit(1)
}
if cfg.Executor == "temporal" {
err = graph.SaveToTemporalWorkflowFile(outputFile)
} else {
err = graph.SaveToFile(outputFile)
}
common.LogInfo("Compiled binary", map[string]interface{}{
"output_file": outputFile,
})
if err != nil {
common.LogError("Failed to generate graph", map[string]interface{}{
"error": err.Error(),
})
os.Exit(1)
}
return nil
},
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Run a playbook by compiling & executing it",
RunE: func(cmd *cobra.Command, args []string) error {
graph, err := GetGraph(playbookFile, tags, skipTags, cfg)
if err != nil {
common.LogError("Failed to generate graph", map[string]interface{}{
"error": err.Error(),
})
os.Exit(1)
}
if checkMode {
if cfg.Facts == nil {
cfg.Facts = make(map[string]interface{})
}
cfg.Facts["ansible_check_mode"] = true
}
if diffMode {
if cfg.Facts == nil {
cfg.Facts = make(map[string]interface{})
}
cfg.Facts["ansible_diff"] = true
}
if cfg.Executor == "temporal" {
err = StartTemporalExecutor(graph, inventoryFile, cfg)
} else {
err = StartLocalExecutor(graph, inventoryFile, cfg)
}
if err != nil {
common.LogError("Failed to run playbook", map[string]interface{}{
"error": err.Error(),
})
os.Exit(1)
}
return nil
},
}
func init() {
// Add config flag to root command so it's available to all subcommands
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file path (default: ./spage.yaml)")
generateCmd.Flags().StringVarP(&playbookFile, "playbook", "p", "", "Playbook file (required)")
generateCmd.Flags().StringVarP(&outputFile, "output", "o", "generated_tasks.go", "Output file (default: generated_tasks.go)")
generateCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, "Only include tasks with these tags (comma-separated)")
generateCmd.Flags().StringSliceVar(&skipTags, "skip-tags", []string{}, "Skip tasks with these tags (comma-separated)")
generateCmd.MarkFlagRequired("playbook")
runCmd.Flags().StringVarP(&playbookFile, "playbook", "p", "", "Playbook file (required)")
runCmd.Flags().StringVarP(&inventoryFile, "inventory", "i", "", "Inventory file (required)")
runCmd.Flags().StringVarP(&outputFile, "output", "o", "generated_tasks.go", "Output file (default: generated_tasks.go)")
runCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, "Only include tasks with these tags (comma-separated)")
runCmd.Flags().StringSliceVar(&skipTags, "skip-tags", []string{}, "Skip tasks with these tags (comma-separated)")
runCmd.Flags().BoolVar(&checkMode, "check", false, "Enable check mode (dry run)")
runCmd.Flags().BoolVar(&diffMode, "diff", false, "Enable diff mode")
runCmd.MarkFlagRequired("playbook")
RootCmd.AddCommand(generateCmd)
RootCmd.AddCommand(runCmd)
}
// GetConfig returns the loaded configuration
func GetConfig() *config.Config {
return cfg
}
// applyTagFiltering filters tasks based on tag configuration
func applyTagFiltering(graph pkg.Graph, tagsConfig config.TagsConfig) (pkg.Graph, error) {
// Always apply filtering to handle special tags like "never" correctly
// Even when no specific tags are requested, "never" tagged tasks should be excluded
filteredGraph := pkg.Graph{
RequiredInputs: graph.RequiredInputs, // Copy required inputs
Tasks: make([][]pkg.Task, 0),
Vars: graph.Vars,
}
for _, taskLayer := range graph.Tasks {
var filteredLayer []pkg.Task
for _, task := range taskLayer {
if shouldIncludeTask(task, tagsConfig) {
filteredLayer = append(filteredLayer, task)
}
}
// Only add the layer if it has tasks
if len(filteredLayer) > 0 {
filteredGraph.Tasks = append(filteredGraph.Tasks, filteredLayer)
}
}
return filteredGraph, nil
}
// shouldIncludeTask determines if a task should be included based on its tags
func shouldIncludeTask(task pkg.Task, tagsConfig config.TagsConfig) bool {
taskTags := task.Tags
// Special tag handling: "always" tag means always include (unless skipped)
hasAlwaysTag := contains(taskTags, "always")
// Special tag handling: "never" tag means never include (unless explicitly tagged)
hasNeverTag := contains(taskTags, "never")
// Check if task should be skipped
if len(tagsConfig.SkipTags) > 0 {
for _, skipTag := range tagsConfig.SkipTags {
if contains(taskTags, skipTag) {
return false // Skip this task
}
}
}
// Handle "never" tag: only include if explicitly requested
if hasNeverTag {
if len(tagsConfig.Tags) == 0 {
// No specific tags requested, exclude "never" tasks
return false
}
// Check if "never" tag is explicitly requested
hasMatchingTag := false
for _, wantedTag := range tagsConfig.Tags {
if contains(taskTags, wantedTag) {
hasMatchingTag = true
break
}
}
if !hasMatchingTag {
return false
}
}
// If task has "always" tag, include it (unless skipped above)
if hasAlwaysTag {
return true
}
// If no specific tags requested, include all tasks (except "never" which was handled above)
if len(tagsConfig.Tags) == 0 {
return true
}
// Check if task has any of the requested tags
for _, wantedTag := range tagsConfig.Tags {
if contains(taskTags, wantedTag) {
return true
}
}
// If task has no tags but we're filtering by tags, exclude it
if len(taskTags) == 0 {
return false
}
// Task doesn't match any wanted tags
return false
}
// contains checks if a slice contains a string
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}