Skip to content

Commit

Permalink
Refactor main.go:run
Browse files Browse the repository at this point in the history
Update the `run` function in `main.go` to reduce its cognitive
complexity as well reduce code duplication (in terms of printing
problems as well as parsing and processing files). Additionally, the
argument name has been updated from the old and outdated `wd` (short for
"working directory") to "target" (aligning it with how it's called).

Signed-off-by: Eric Cornelissen <ericornelissen@gmail.com>
  • Loading branch information
ericcornelissen committed Aug 24, 2023
1 parent ffc6306 commit b13c922
Showing 1 changed file with 67 additions and 68 deletions.
135 changes: 67 additions & 68 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,98 +92,97 @@ func main() {
}
}

func run(wd string) (hasProblems bool, err error) {
if data, err := os.ReadFile(wd); err == nil {
manifest, err := parseManifest(data)
if err != nil {
return hasProblems, err
}

problems := processManifest(&manifest)
if cnt := len(problems); cnt > 0 {
fmt.Printf("Detected %d problem(s) in '%s':\n", cnt, wd)
for _, problem := range problems {
fmt.Println(" ", problem)
}
}
func run(target string) (hasProblems bool, err error) {
stat, err := os.Stat(target)
if err != nil {
return hasProblems, fmt.Errorf("could not process %s: %v", target, err)
}

if len(problems) > 0 {
// don't try to parse the file as an Actions workflow if it was already
// successfully analyzed as an Action manifest.
return true, nil
if stat.IsDir() {
if problems, err := tryManifest(path.Join(target, "action.yml")); err != nil {
return hasProblems, err
} else {
hasProblems = len(problems) > 0
printProblems("action.yml", problems)
}

workflow, err := parseWorkflow(data)
workflowsDir := path.Join(target, ".github", "workflows")
workflows, err := os.ReadDir(workflowsDir)
if err != nil {
return hasProblems, err
return hasProblems, fmt.Errorf("could not read workflows directory: %v", err)
}

problems = processWorkflow(&workflow)
if cnt := len(problems); cnt > 0 {
fmt.Printf("Detected %d problem(s) in '%s':\n", cnt, wd)
for _, problem := range problems {
fmt.Println(" ", problem)
for _, entry := range workflows {
if entry.Type().IsDir() {
continue
}
}

return len(problems) > 0, nil
}
if path.Ext(entry.Name()) != ".yml" {
continue
}

if data, err := os.ReadFile(path.Join(wd, "action.yml")); err == nil {
manifest, err := parseManifest(data)
if err != nil {
workflowPath := path.Join(workflowsDir, entry.Name())
if problems, err := tryWorkflow(workflowPath); err != nil {
fmt.Printf("Could not process workflow %s: %v\n", entry.Name(), err)
} else {
hasProblems = len(problems) > 0
printProblems(entry.Name(), problems)
}
}
} else {
if problems, err := tryManifest(target); err != nil {
return hasProblems, err
} else {
hasProblems = len(problems) > 0
printProblems(target, problems)
}

problems := processManifest(&manifest)
if cnt := len(problems); cnt > 0 {
hasProblems = true
fmt.Printf("Detected %d problem(s) in 'action.yml':\n", cnt)
for _, problem := range problems {
fmt.Println(" ", problem)
}
if problems, err := tryWorkflow(target); err != nil {
return hasProblems, err
} else {
hasProblems = len(problems) > 0
printProblems(target, problems)
}
}

workflowsDir := path.Join(wd, ".github", "workflows")
workflows, err := os.ReadDir(workflowsDir)
return hasProblems, nil
}

func tryManifest(manifestPath string) (problems []string, err error) {
data, err := os.ReadFile(manifestPath)
if err != nil {
return nil, nil
}

manifest, err := parseManifest(data)
if err != nil {
return hasProblems, fmt.Errorf("could not read workflows directory: %v", err)
return nil, err
}

for _, entry := range workflows {
if entry.Type().IsDir() {
continue
}
return processManifest(&manifest), nil
}

if path.Ext(entry.Name()) != ".yml" {
continue
}
func tryWorkflow(workflowPath string) (problems []string, err error) {
data, err := os.ReadFile(workflowPath)
if err != nil {
return nil, err
}

workflowPath := path.Join(workflowsDir, entry.Name())
data, err := os.ReadFile(workflowPath)
if err != nil {
fmt.Printf("Could not read %s: %v\n", entry.Name(), err)
continue
}
workflow, err := parseWorkflow(data)
if err != nil {
return nil, err
}

workflow, err := parseWorkflow(data)
if err != nil {
fmt.Printf("Could not parse %s: %v\n", entry.Name(), err)
continue
}
return processWorkflow(&workflow), nil
}

problems := processWorkflow(&workflow)
if cnt := len(problems); cnt > 0 {
hasProblems = true
fmt.Printf("Detected %d problem(s) in '%s':\n", cnt, entry.Name())
for _, problem := range problems {
fmt.Println(" ", problem)
}
func printProblems(file string, problems []string) {
if cnt := len(problems); cnt > 0 {
fmt.Printf("Detected %d problem(s) in '%s':\n", cnt, file)
for _, problem := range problems {
fmt.Println(" ", problem)
}
}

return hasProblems, nil
}

func getTargets(argv []string) ([]string, error) {
Expand Down

0 comments on commit b13c922

Please sign in to comment.