Skip to content

Commit

Permalink
🐛 Run the shim on each directory. (#126) (#130)
Browse files Browse the repository at this point in the history
Ensure each _ruleDir_ (`--rules`) contains a `ruleset.yaml`.
Run the shim on all _ruleDir_. This includes rules/files which contains
uploaded individual rule files.
This is needed because the shim creates converted files in the root of
the output dir.

https://issues.redhat.com/browse/MTA-3163
https://issues.redhat.com/browse/MTA-3330

---------

Signed-off-by: Jeff Ortel <[email protected]>
Signed-off-by: Cherry Picker <[email protected]>

Signed-off-by: Jeff Ortel <[email protected]>
Signed-off-by: Cherry Picker <[email protected]>
Co-authored-by: Jeff Ortel <[email protected]>
  • Loading branch information
konveyor-ci-bot[bot] and jortel authored Aug 26, 2024
1 parent 658ecf4 commit d046d45
Showing 1 changed file with 68 additions and 71 deletions.
139 changes: 68 additions & 71 deletions cmd/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (r *Rules) Build() (err error) {
if err != nil {
return
}
err = r.ensureRuleSet()
if err != nil {
return
}
err = r.Labels.injectAlways(r.repositories)
if err != nil {
return
Expand Down Expand Up @@ -94,26 +98,8 @@ func (r *Rules) addFiles() (err error) {
if err != nil {
return
}
entries, err := os.ReadDir(ruleDir)
if err != nil {
return
}
for _, ent := range entries {
if ent.Name() == parser.RULE_SET_GOLDEN_FILE_NAME {
r.repositories = append(r.repositories, ruleDir)
r.append(ruleDir)
return
}
}
n := 0
for _, ent := range entries {
p := path.Join(ruleDir, ent.Name())
r.append(p)
n++
}
if n > 0 {
r.repositories = append(r.repositories, ruleDir)
}
r.rules = append(r.rules, ruleDir)
r.repositories = append(r.repositories, ruleDir)
return
}

Expand Down Expand Up @@ -205,22 +191,21 @@ func (r *Rules) addRules(ruleset *api.RuleSet) (err error) {
return
}
n := len(ruleset.Rules)
if n < 1 {
return
}
r.rules = append(r.rules, ruleDir)
for _, ruleset := range ruleset.Rules {
fileRef := ruleset.File
if fileRef == nil {
file := ruleset.File
if file == nil {
continue
}
path := path.Join(ruleDir, fileRef.Name)
err = addon.File.Get(ruleset.File.ID, path)
err = addon.File.Get(
ruleset.File.ID,
path.Join(ruleDir, file.Name))
if err != nil {
break
}
if n == 1 {
r.append(path)
}
}
if n > 1 {
r.append(ruleDir)
}
return
}
Expand Down Expand Up @@ -256,7 +241,7 @@ func (r *Rules) addRuleSetRepository(ruleset *api.RuleSet) (err error) {
}
ruleDir := path.Join(rootDir, ruleset.Repository.Path)
r.repositories = append(r.repositories, ruleDir)
r.append(ruleDir)
r.rules = append(r.rules, ruleDir)
return
}

Expand Down Expand Up @@ -289,7 +274,7 @@ func (r *Rules) addRepository() (err error) {
}
ruleDir := path.Join(rootDir, r.Repository.Path)
r.repositories = append(r.repositories, ruleDir)
r.append(ruleDir)
r.rules = append(r.rules, ruleDir)
return
}

Expand All @@ -304,31 +289,61 @@ func (r *Rules) addSelector(options *command.Options) (err error) {
}

// convert windup rules.
// Run the shim on all ruleset directories.
func (r *Rules) convert() (err error) {
cmd := command.New("/usr/bin/windup-shim")
cmd.Options.Add("convert")
cmd.Options.Add("--outputdir", RuleDir)
cmd.Options.Add(RuleDir)
err = cmd.Run()
if err != nil {
return

for _, ruleDir := range r.rules {

cmd := command.New("/usr/bin/windup-shim")
cmd.Options.Add("convert")
cmd.Options.Add("--outputdir", ruleDir)
cmd.Options.Add(ruleDir)
err = cmd.Run()
if err != nil {
return
}
}
return
}

// append path.
func (r *Rules) append(p string) {
for i := range r.rules {
if r.rules[i] == p {
// ensureRuleSet ensures each ruleDir in rules
// contains a ruleset.yaml file.
func (r *Rules) ensureRuleSet() (err error) {
create := func(p string) (err error) {
addon.Activity("[RULE] %s not-found;created.", p)
f, err := os.Create(p)
if err != nil {
return
}
defer func() {
_ = f.Close()
}()
en := yaml.NewEncoder(f)
p = path.Dir(p)
p = strings.TrimPrefix(p, RuleDir)
part := strings.Split(p, "/")
name := strings.Join(part[1:], "-")
err = en.Encode(map[string]any{"name": name})
return
}
switch strings.ToUpper(path.Ext(p)) {
case "",
".YAML",
".YML":
r.rules = append(r.rules, p)
for _, ruleDir := range r.rules {
p := path.Join(
ruleDir,
parser.RULE_SET_GOLDEN_FILE_NAME)
_, err = os.Stat(p)
if err == nil {
continue
}
if os.IsNotExist(err) {
err = create(p)
if err != nil {
return
}
} else {
return
}
}
return
}

// Labels collection.
Expand Down Expand Up @@ -374,8 +389,8 @@ func (r *Labels) ruleSetMap() (mp RuleSetMap, err error) {
return
}

// injectAlways - Replaces the labels in every rule file
// with konveyor.io/include=always.
// injectAlways - Replaces the labels in every ruleset.yaml
// file with konveyor.io/include=always.
func (r *Labels) injectAlways(paths []string) (err error) {
read := func(m any, p string) (err error) {
f, err := os.Open(p)
Expand Down Expand Up @@ -407,38 +422,20 @@ func (r *Labels) injectAlways(paths []string) (err error) {
addon.Log.Error(wErr, p)
return
}
switch strings.ToUpper(path.Ext(p)) {
case "",
".YAML",
".YML":
default:
return
}
key := "labels"
value := []string{"konveyor.io/include=always"}
if path.Base(p) == parser.RULE_SET_GOLDEN_FILE_NAME {
ruleSet := make(map[any]any)
err = read(&ruleSet, p)
if err != nil {
return
}
ruleSet[key] = []string{"konveyor.io/include=always"}
ruleSet[key] = value
addon.Activity("[RULE] inject(%s): %s.", value[0], p)
err = write(&ruleSet, p)
if err != nil {
return
}
} else {
rules := make([]map[any]any, 0)
err = read(&rules, p)
if err != nil {
return
}
for _, rule := range rules {
rule[key] = []string{"konveyor.io/include=always"}
}
err = write(&rules, p)
if err != nil {
return
}
}
return
}
Expand Down

0 comments on commit d046d45

Please sign in to comment.