generated from konveyor-ecosystem/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathshimconvert.go
235 lines (215 loc) · 6.3 KB
/
shimconvert.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
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
package cmd
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-logr/logr"
"github.com/konveyor-ecosystem/kantra/pkg/container"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"github.com/fabianvf/windup-rulesets-yaml/pkg/conversion"
"github.com/fabianvf/windup-rulesets-yaml/pkg/windup"
)
type windupShimCommand struct {
input []string
output string
log logr.Logger
cleanup bool
}
func NewWindupShimCommand(log logr.Logger) *cobra.Command {
windupShimCmd := &windupShimCommand{
log: log,
cleanup: true,
}
windupShimCommand := &cobra.Command{
Use: "rules",
Short: "Convert XML rules to YAML",
PreRunE: func(cmd *cobra.Command, args []string) error {
cmd.MarkFlagRequired("input")
cmd.MarkFlagRequired("output")
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}
err := windupShimCmd.Validate()
if err != nil {
log.Error(err, "failed to validate flags")
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if val, err := cmd.Flags().GetBool(noCleanupFlag); err == nil {
windupShimCmd.cleanup = !val
}
err := windupShimCmd.Run(cmd.Context())
if err != nil {
log.Error(err, "failed to execute windup shim")
return err
}
return nil
},
}
windupShimCommand.Flags().StringArrayVarP(&windupShimCmd.input, "input", "i", []string{}, "path to XML rule file(s) or directory")
windupShimCommand.Flags().StringVarP(&windupShimCmd.output, "output", "o", "", "path to output directory")
return windupShimCommand
}
func (w *windupShimCommand) Validate() error {
outputStat, err := os.Stat(w.output)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(w.output, os.ModePerm)
if err != nil {
return fmt.Errorf("%w failed to create output dir %s", err, w.output)
}
} else {
return fmt.Errorf("failed to stat output directory %s", w.output)
}
}
if outputStat != nil && !outputStat.IsDir() {
return fmt.Errorf("output path %s is not a directory", w.output)
}
if w.input == nil || len(w.input) == 0 {
return fmt.Errorf("input for rule file or directory must not be empty")
}
for _, r := range w.input {
if filepath.Clean(r) == filepath.Clean(w.output) {
return fmt.Errorf("input rule directory and output directory must be different")
}
}
// try to get abs path, if not, continue with relative path
if absPath, err := filepath.Abs(w.output); err == nil {
w.output = absPath
}
for idx := range w.input {
if absPath, err := filepath.Abs(w.input[idx]); err == nil {
w.input[idx] = absPath
}
}
return nil
}
func (w *windupShimCommand) getRulesVolumes(tempRuleDir string) (map[string]string, error) {
rulesVolumes := make(map[string]string)
mountTempDir := false
for _, r := range w.input {
stat, err := os.Stat(r)
if err != nil {
w.log.V(1).Error(err, "failed to stat rules")
return nil, err
}
// move xml rule files from user into dir to mount
if !stat.IsDir() {
mountTempDir = true
xmlFileName := filepath.Base(r)
destFile := filepath.Join(tempRuleDir, xmlFileName)
err := copyFileContents(r, destFile)
if err != nil {
w.log.V(1).Error(err, "failed to move rules file from source to destination", "src", r, "dest", destFile)
return nil, err
}
} else {
rulesVolumes[r] = path.Join(XMLRulePath, filepath.Base(r))
}
}
if mountTempDir {
rulesVolumes[tempRuleDir] = XMLRulePath
}
return rulesVolumes, nil
}
func (w *windupShimCommand) Run(ctx context.Context) error {
tempDir, err := os.MkdirTemp("", "transform-rules-")
if err != nil {
w.log.V(1).Error(err, "failed to create temp dir for rules")
return err
}
w.log.V(1).Info("created temp directory for XML rules", "dir", tempDir)
if w.cleanup {
defer os.RemoveAll(tempDir)
}
volumes := map[string]string{
w.output: ShimOutputPath,
}
ruleVols, err := w.getRulesVolumes(tempDir)
if err != nil {
w.log.V(1).Error(err, "failed to get xml rules for conversion")
return err
}
maps.Copy(volumes, ruleVols)
shimLogPath := filepath.Join(w.output, "shim.log")
shimLog, err := os.Create(shimLogPath)
if err != nil {
return fmt.Errorf("failed creating shim log file %s", shimLogPath)
}
defer shimLog.Close()
args := []string{"convert",
fmt.Sprintf("--outputdir=%v", ShimOutputPath),
XMLRulePath,
}
w.log.Info("running windup-shim convert command",
"args", strings.Join(args, " "), "volumes", volumes, "output", w.output, "inputs", strings.Join(w.input, ","))
w.log.Info("generating shim log in file", "file", shimLogPath)
err = container.NewContainer().Run(
ctx,
container.WithImage(Settings.RunnerImage),
container.WithLog(w.log.V(1)),
container.WithVolumes(volumes),
container.WithStdout(shimLog),
container.WithStderr(shimLog),
container.WithEntrypointArgs(args...),
container.WithEntrypointBin("/usr/local/bin/windup-shim"),
container.WithContainerToolBin(Settings.ContainerBinary),
container.WithCleanup(w.cleanup),
)
if err != nil {
w.log.V(1).Error(err, "failed to run convert command")
return err
}
return nil
}
func (a *analyzeCommand) ConvertXMLContainerless() (string, error) {
shimLogFilePath := filepath.Join(a.output, "shim.log")
shimLog, err := os.Create(shimLogFilePath)
if err != nil {
return "", err
}
defer shimLog.Close()
os.Stdout = shimLog
tempDir, err := os.MkdirTemp("", "analyze-rules-")
if err != nil {
a.log.V(1).Error(err, "failed creating temp dir", "dir", tempDir)
return "", err
}
a.log.V(7).Info("created temp directory for xml rules", "dir", tempDir)
for _, location := range a.rules {
rulesets := []windup.Ruleset{}
ruletests := []windup.Ruletest{}
err := filepath.WalkDir(location, walkXML(location, &rulesets, &ruletests))
if err != nil {
a.log.V(1).Error(err, "failed to get get xml rule")
}
_, err = conversion.ConvertWindupRulesetsToAnalyzer(rulesets, location, tempDir, true, false)
if err != nil {
log.Fatal(err)
}
}
return tempDir, nil
}
func walkXML(root string, rulesets *[]windup.Ruleset, rulestest *[]windup.Ruletest) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if !strings.HasSuffix(path, ".xml") {
return nil
}
if rulesets != nil {
ruleset := windup.ProcessWindupRuleset(path)
if ruleset != nil {
*rulesets = append(*rulesets, *ruleset)
}
}
return err
}
}