-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
263 lines (217 loc) · 7.36 KB
/
main.go
File metadata and controls
263 lines (217 loc) · 7.36 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
package main
import (
"flag"
"fmt"
"io"
"os"
"regexp"
"runtime"
"runtime/debug"
"strings"
"golang.org/x/term"
"github.com/UnitVectorY-Labs/YAMLtecture/internal/common"
c "github.com/UnitVectorY-Labs/YAMLtecture/internal/configuration"
m "github.com/UnitVectorY-Labs/YAMLtecture/internal/mermaid"
q "github.com/UnitVectorY-Labs/YAMLtecture/internal/query"
)
var (
// In and Out
inFlag = flag.String("in", "", "Input file to load")
outFlag = flag.String("out", "", "Output file to write")
// Explicitly set the query and config files
configFlag = flag.String("configIn", "", "Input file for the Config YAML architecture file")
queryFlag = flag.String("queryIn", "", "Input file for the Query YAML architecture file")
mermaidFlag = flag.String("mermaidIn", "", "Input file for the Mermaid settings")
// The various commands to run
validateConfigFlag = flag.Bool("validateConfig", false, "Validate the Config YAML architecture file")
validateQueryFlag = flag.Bool("validateQuery", false, "Validate the Query YAML architecture file")
validateMermaidFlag = flag.Bool("validateMermaid", false, "Validate the Mermaid settings")
mergeConfigFlag = flag.Bool("mergeConfig", false, "Merge the Config YAML architecture file")
executeQueryFlag = flag.Bool("executeQuery", false, "Execute the Query YAML architecture file")
generateMermaidFlag = flag.Bool("generateMermaid", false, "Generate a Mermaid diagram from the Config YAML architecture file")
// Modifiers
debugFlag = flag.Bool("debug", false, "Enable debug output")
)
var Version = "dev" // This will be set by the build systems to the release version
var semverRe = regexp.MustCompile(`^\d+\.\d+\.\d+`)
func buildVersionOutput(version string) string {
normalized := version
if semverRe.MatchString(normalized) && !strings.HasPrefix(normalized, "v") {
normalized = "v" + normalized
}
return fmt.Sprintf("%s (%s, %s/%s)", normalized, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
func main() {
// Set the build version from the build info if not set by the build system
if Version == "dev" || Version == "" {
if bi, ok := debug.ReadBuildInfo(); ok {
if bi.Main.Version != "" && bi.Main.Version != "(devel)" {
Version = bi.Main.Version
}
}
}
showVersion := flag.Bool("version", false, "Print version")
flag.Parse()
if *showVersion {
fmt.Printf("yamltecture version %s\n", buildVersionOutput(Version))
return
}
// First determine what we are doing
checkMultipleCommands(*validateConfigFlag, *validateQueryFlag, *validateMermaidFlag, *mergeConfigFlag, *executeQueryFlag, *generateMermaidFlag)
if *validateConfigFlag {
// Validate the config file
content := readFileContent(*configFlag, true, *inFlag, true, "")
config, err := c.ParseYAML(content)
if err != nil {
common.PrintError("Error parsing YAML", err)
}
err = config.Validate()
if err != nil {
common.PrintError("Error validating configuration", err)
}
} else if *validateQueryFlag {
// Validate the query file
content := readFileContent(*queryFlag, true, *inFlag, true, "")
query, err := q.ParseQuery(content)
if err != nil {
common.PrintError("Error loading query", err)
}
err = query.Validate()
if err != nil {
common.PrintError("Error validating query", err)
}
} else if *mergeConfigFlag {
// Validate that inFlag was set and is a folder
if *inFlag == "" {
common.PrintError("No input folder specified", nil)
}
config, err := c.LoadFolder(*inFlag)
if err != nil {
common.PrintError("Error loading folder", err)
}
err = config.Validate()
if err != nil {
common.PrintError("Error validating configuration", err)
}
writeOutput(config.YamlString(), *outFlag)
} else if *validateMermaidFlag {
// Validate the mermaid file
content := readFileContent(*mermaidFlag, true, *inFlag, true, "")
mermaid, err := m.ParseYAML(content)
if err != nil {
common.PrintError("Error parsing YAML", err)
}
err = mermaid.Validate()
if err != nil {
common.PrintError("Error validating mermaid", err)
}
} else if *executeQueryFlag {
// Execute the query file
configContent := readFileContent(*configFlag, false, *inFlag, true, "")
queryContent := readFileContent(*queryFlag, false, *inFlag, false, "")
config, err := c.ParseYAML(configContent)
if err != nil {
common.PrintError("Error parsing YAML", err)
}
err = config.Validate()
if err != nil {
common.PrintError("Error validating configuration", err)
}
query, err := q.ParseQuery(queryContent)
if err != nil {
common.PrintError("Error loading query", err)
}
err = query.Validate()
if err != nil {
common.PrintError("Error validating query", err)
}
result, err := q.ExecuteQuery(query, config)
if err != nil {
common.PrintError("Error executing query", err)
}
writeOutput(result.YamlString(), *outFlag)
} else if *generateMermaidFlag {
// Generate the Mermaid diagram
queryContent := readFileContent(*configFlag, false, *inFlag, true, "")
mermaidContent := readFileContent(*mermaidFlag, false, *inFlag, false, "\n")
config, err := c.ParseYAML(queryContent)
if err != nil {
common.PrintError("Error parsing YAML", err)
}
err = config.Validate()
if err != nil {
common.PrintError("Error validating configuration", err)
}
mermaid, err := m.ParseYAML(mermaidContent)
if err != nil {
common.PrintError("Error parsing YAML", err)
}
err = mermaid.Validate()
if err != nil {
common.PrintError("Error validating mermaid", err)
}
mermaidDiagram, err := m.GenerateMermaid(config, mermaid)
if err != nil {
common.PrintError("Error generating Mermaid diagram", err)
}
writeOutput(mermaidDiagram, *outFlag)
} else {
// Write error to error output
common.PrintError("No command specified", nil)
}
}
// writeOutput will take the string content and if the -out flag is set, write it to the file, if not it writes to STDOUT
func writeOutput(content string, outFlag string) {
if outFlag != "" {
err := os.WriteFile(outFlag, []byte(content), 0644)
if err != nil {
common.PrintError(fmt.Sprintf("Error writing to file %s", outFlag), err)
}
} else {
fmt.Println(content)
}
}
// Read the content of a file based on the flags provided
func readFileContent(specificFlag string, allowGenericFlag bool, genericFlag string, allowStdin bool, defaultValue string) string {
if specificFlag != "" {
// Read from the specific flag first
content, err := os.ReadFile(specificFlag)
if err != nil {
common.PrintError(fmt.Sprintf("Error reading file %s", specificFlag), err)
}
return string(content)
} else if allowGenericFlag && genericFlag != "" {
content, err := os.ReadFile(genericFlag)
if err != nil {
common.PrintError(fmt.Sprintf("Error reading file %s", genericFlag), err)
}
return string(content)
} else if allowStdin {
if !term.IsTerminal(int(os.Stdin.Fd())) {
content, err := io.ReadAll(os.Stdin)
if err != nil {
common.PrintError("Error reading from STDIN", err)
}
return string(content)
} else {
common.PrintError("No input provided via STDIN", nil)
return ""
}
} else if defaultValue != "" {
return defaultValue
} else {
common.PrintError("No input file specified and reading from STDIN is not allowed", nil)
return ""
}
}
func checkMultipleCommands(commands ...bool) {
count := 0
for _, cmd := range commands {
if cmd {
count++
}
}
if count > 1 {
common.PrintError("Multiple commands specified", nil)
}
}