-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
138 lines (132 loc) · 4.4 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/konveyor/windup-shim/pkg/conversion"
"github.com/konveyor/windup-shim/pkg/execution"
"github.com/konveyor/windup-shim/pkg/windup"
)
func main() {
var outputDir, data, appPath, targets string
var failFast, flattenRulesets bool
convertCmd := flag.NewFlagSet("convert", flag.ExitOnError)
convertCmd.StringVar(&outputDir, "outputdir", "analyzer-lsp-rules", "The output location for converted rules")
convertCmd.BoolVar(&flattenRulesets, "flatten", true, "Set to false to preserve original ruleset structure of windup rules")
testCmd := flag.NewFlagSet("test", flag.ExitOnError)
testCmd.BoolVar(&failFast, "fail-fast", false, "If true, fail on first test failure")
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
runCmd.StringVar(&data, "data", "", "The location of the source code to run the rules against")
reportTestCmd := flag.NewFlagSet("report-test", flag.ExitOnError)
reportTestCmd.StringVar(&appPath, "app", "/example-applications/example-1/", "Example application to run the test on")
reportTestCmd.StringVar(&targets, "targets", "cloud-readiness", "Comma separated list of targets")
help := "Supported subcommands are convert, run, test and report-test"
if len(os.Args) < 2 {
fmt.Println(help)
return
}
switch os.Args[1] {
case "convert":
if err := convertCmd.Parse(os.Args[2:]); err == nil {
if convertCmd.NArg() < 1 {
fmt.Println("The location of one or more windup XML files is required")
return
}
for _, location := range convertCmd.Args() {
rulesets := []windup.Ruleset{}
ruletests := []windup.Ruletest{}
err := filepath.WalkDir(location, walkXML(location, &rulesets, &ruletests, false))
if err != nil {
fmt.Println(err)
}
analyzerRulesets, err := conversion.ConvertWindupRulesetsToAnalyzer(rulesets, location, outputDir, flattenRulesets, false)
if err != nil {
log.Fatal(err)
}
err = conversion.ConvertWindupRuletestsToAnalyzer(ruletests, rulesets, analyzerRulesets)
if err != nil {
log.Fatal(err)
}
}
}
case "test":
if err := testCmd.Parse(os.Args[2:]); err == nil {
if testCmd.NArg() < 1 {
log.Fatal("The location of one or more windup XML files is required")
}
for _, location := range testCmd.Args() {
rulesets := []windup.Ruleset{}
ruletests := []windup.Ruletest{}
err := filepath.WalkDir(location, walkXML(location, &rulesets, &ruletests, false))
if err != nil {
fmt.Println(err)
}
totalSuccesses := 0
totalTests := 0
for _, test := range ruletests {
fmt.Println("Executing " + test.SourceFile)
successes, total, err := execution.ExecuteTest(test, location, true)
if err != nil {
// TODO should we exit here?
fmt.Println(err)
}
totalSuccesses += successes
totalTests += total
fmt.Printf("Overall success rate: %.2f%% (%d/%d)\n", float32(totalSuccesses)/float32(totalTests)*100, totalSuccesses, totalTests)
if successes != total && failFast {
break
}
}
if totalSuccesses != totalTests {
os.Exit(1)
}
}
}
case "run":
if err := runCmd.Parse(os.Args[2:]); err == nil {
if runCmd.NArg() < 1 {
fmt.Println("The location of one or more windup XML files is required")
return
}
if data == "" {
fmt.Println("The location of a data directory is required (-data option)")
return
}
rulesets := []windup.Ruleset{}
for _, location := range runCmd.Args() {
err := filepath.WalkDir(location, walkXML(location, &rulesets, nil, false))
if err != nil {
fmt.Println(err)
}
output, dir, err := execution.ExecuteRulesets(rulesets, location, data, false)
fmt.Println(output, dir, err)
}
}
default:
fmt.Println(help)
}
}
func walkXML(root string, rulesets *[]windup.Ruleset, ruletests *[]windup.Ruletest, writeYAML bool) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if !strings.HasSuffix(path, ".xml") {
// fmt.Printf("Skipping %s because it is not a ruleset\n", path)
return nil
}
if strings.HasSuffix(path, ".test.xml") && ruletests != nil {
ruletest := windup.ProcessWindupRuletest(path)
if ruletest != nil {
*ruletests = append(*ruletests, *ruletest)
}
} else if rulesets != nil {
ruleset := windup.ProcessWindupRuleset(path)
if ruleset != nil {
*rulesets = append(*rulesets, *ruleset)
}
}
return err
}
}