Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-63) Allow additional tool args to be set within validate.yml
Browse files Browse the repository at this point in the history
```
---
tools:
  - name: "group/modules"
  - name: "puppetlabs/rubocop"
    args: ["--auto-correct"]
  - name: "puppetlabs/epp"

```
  • Loading branch information
da-ar committed Dec 12, 2021
1 parent 9aac438 commit 58a5c0e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
5 changes: 2 additions & 3 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,15 @@ func execute(cmd *cobra.Command, args []string) error {
log.Info().Msgf("Found tools: %v ", toolList)

for _, tool := range toolList {
cachedTool, ok := prmApi.IsToolAvailable(tool)
cachedTool, ok := prmApi.IsToolAvailable(tool.Name)
if !ok {
return fmt.Errorf("Tool %s not found in cache", tool)
}
err := prmApi.Exec(cachedTool, additionalToolArgs) // todo: do we want to allow folk to specify args from validate.yml?
err := prmApi.Exec(cachedTool, tool.Args)
if err != nil {
return err
}
}

}

return nil
Expand Down
34 changes: 20 additions & 14 deletions pkg/prm/prm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type PuppetVersion struct {
}

type ValidateYmlContent struct {
Tools []string `yaml:"tools"`
Tools []ToolInst `yaml:"tools"`
}

// checkGroups takes a slice of tool names and iterates through each
// checkGroups takes a map of tool names and additional args and iterates through each
// checking against a map of toolGroups. If a toolGroup name is found
// the toolGroup is expanded and the list of tools is updated.
func (*Prm) checkGroups(tools []string) []string {
func (*Prm) checkGroups(tools []ToolInst) []ToolInst {
for index, toolName := range tools {
if toolGroup, ok := ToolGroups[toolName]; ok {
if toolGroup, ok := ToolGroups[toolName.Name]; ok {
// remove the group from the list
tools = append(tools[:index], tools[index+1:]...)
// add the expanded toolgroup to the list
Expand All @@ -55,12 +55,18 @@ func (*Prm) checkGroups(tools []string) []string {
}

// remove duplicates
allKeys := make(map[string]bool)
clean := []string{}
for _, item := range tools {
if _, value := allKeys[item]; !value {
allKeys[item] = true
clean = append(clean, item)
clean := []ToolInst{}

for index1, item1 := range tools {
found := false
for _, item2 := range clean {
if compareToolInst(item1, item2) {
found = true
}
}
if !found {
// add to the clean list
clean = append(clean, tools[index1])
}
}

Expand All @@ -71,27 +77,27 @@ func (*Prm) checkGroups(tools []string) []string {
// list of tool names from validate.yml into a list. Pass the list of
// tool names to flattenToolList to expand out any groups. Then return
// the complete list.
func (p *Prm) CheckLocalConfig() ([]string, error) {
func (p *Prm) CheckLocalConfig() ([]ToolInst, error) {
// check if validate.yml exits in the codeDir
validateFile := filepath.Join(p.CodeDir, "validate.yml")
if _, err := p.AFS.Stat(validateFile); err != nil {
log.Error().Msgf("validate.yml not found in %s", p.CodeDir)
return []string{}, err
return []ToolInst{}, err
}

// read in validate.yml
contents, err := p.AFS.ReadFile(validateFile)
if err != nil {
log.Error().Msgf("Error reading validate.yml: %s", err)
return []string{}, err
return []ToolInst{}, err
}

// parse validate.yml to our temporary struct
var userList ValidateYmlContent
err = yaml.Unmarshal(contents, &userList)
if err != nil {
log.Error().Msgf("validate.yml is not formated correctly: %s", err)
return []string{}, err
return []ToolInst{}, err
}

return p.checkGroups(userList.Tools), nil
Expand Down
39 changes: 33 additions & 6 deletions pkg/prm/tool_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,43 @@ package prm
execute against a larger list of tools, without needing to define
or understand what is being called.
*/
type ToolInst struct {
Name string `yaml:"name"`
Args []string `yaml:"args"`
}

var (
ToolGroups = map[string][]string{
ToolGroups = map[string][]ToolInst{
// TODO: we may need to define group as a reserved word
"group/modules": {
"puppetlabs/rubocop",
"puppetlabs/rspec-puppet",
"puppetlabs/puppet-lint",
"puppetlabs/puppet-syntax",
"puppetlabs/puppet-strings",
{Name: "puppetlabs/spec_cache"},
{
Name: "puppetlabs/spec_puppet",
Args: []string{
"spec_prep",
},
},
{Name: "puppetlabs/spec_puppet"},
{Name: "puppetlabs/rubocop"},
{Name: "puppetlabs/puppet-lint"},
{Name: "puppetlabs/puppet-syntax"},
{Name: "puppetlabs/puppet-strings"},
},
}
)

func compareToolInst(t1 ToolInst, t2 ToolInst) bool {
return t1.Name == t2.Name && equal(t1.Args, t2.Args)
}

func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

0 comments on commit 58a5c0e

Please sign in to comment.