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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petergmurphy committed Jun 15, 2022
1 parent 7928adf commit 0e3312e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
27 changes: 17 additions & 10 deletions pkg/prm/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package prm
import (
"errors"
"fmt"
"github.com/puppetlabs/prm/pkg/utils"
"os"
"path"
"regexp"
Expand All @@ -23,6 +24,12 @@ const (
VALIDATION_ERROR
)

type ValidationOutput struct {
err error
exitCode ValidateExitCode
stdout string
}

var (
toolLogOutputPaths map[string]string // Key = toolName, Value = logFilePath, stores each tool's log file path
)
Expand All @@ -39,7 +46,7 @@ func (p *Prm) Validate(toolsInfo []ToolInfo, workerCount int, settings OutputSet

tasks := p.createTasks(toolsInfo)

pool := CreateWorkerPool(tasks, workerCount)
pool := utils.CreateWorkerPool(tasks, workerCount)
pool.Run()

err := p.outputResults(tasks, settings)
Expand Down Expand Up @@ -69,7 +76,7 @@ func (p Prm) taskFunc(tool ToolInfo) func() ValidationOutput {
}
}

func (p *Prm) outputResults(tasks []*Task[ValidationOutput], settings OutputSettings) error {
func (p *Prm) outputResults(tasks []*utils.Task[ValidationOutput], settings OutputSettings) error {
err := p.writeOutputLogs(tasks, settings)
if err != nil {
return err
Expand All @@ -89,7 +96,7 @@ func (p *Prm) outputResults(tasks []*Task[ValidationOutput], settings OutputSett
return nil
}

func writeOutputToTerminal(tasks []*Task[ValidationOutput]) {
func writeOutputToTerminal(tasks []*utils.Task[ValidationOutput]) {
for _, task := range tasks {
output := task.Output
if output.err == nil {
Expand Down Expand Up @@ -126,10 +133,10 @@ func renderTable(headers []string, data [][]string) {
table.Render()
}

func (p *Prm) createTasks(toolsInfo []ToolInfo) []*Task[ValidationOutput] {
tasks := make([]*Task[ValidationOutput], len(toolsInfo))
func (p *Prm) createTasks(toolsInfo []ToolInfo) []*utils.Task[ValidationOutput] {
tasks := make([]*utils.Task[ValidationOutput], len(toolsInfo))
for i, info := range toolsInfo {
tasks[i] = CreateTask[ValidationOutput](info.Tool.Cfg.Plugin.Id, p.taskFunc(info), ValidationOutput{})
tasks[i] = utils.CreateTask[ValidationOutput](info.Tool.Cfg.Plugin.Id, p.taskFunc(info), ValidationOutput{})
}
return tasks
}
Expand All @@ -155,7 +162,7 @@ func createLogFilePath(outputDir string, toolId string) string {
return fullPath
}

func (p *Prm) writeOutputToFile(tasks []*Task[ValidationOutput], outputDir string) error {
func (p *Prm) writeOutputToFile(tasks []*utils.Task[ValidationOutput], outputDir string) error {
for _, task := range tasks {
err := p.checkAndCreateDir(outputDir)
if err != nil {
Expand Down Expand Up @@ -199,7 +206,7 @@ func writeStringToFile(file afero.File, output ValidationOutput) error {
return nil
}

func (p *Prm) writeOutputLogs(tasks []*Task[ValidationOutput], settings OutputSettings) (err error) {
func (p *Prm) writeOutputLogs(tasks []*utils.Task[ValidationOutput], settings OutputSettings) (err error) {
if settings.ResultsView == "terminal" {
writeOutputToTerminal(tasks)
return nil
Expand All @@ -213,7 +220,7 @@ func (p *Prm) writeOutputLogs(tasks []*Task[ValidationOutput], settings OutputSe
return fmt.Errorf("invalid --resultsView flag specified")
}

func getErrorCount(tasks []*Task[ValidationOutput]) (count int) {
func getErrorCount(tasks []*utils.Task[ValidationOutput]) (count int) {
for _, task := range tasks {
output := task.Output
if output.err != nil {
Expand All @@ -223,7 +230,7 @@ func getErrorCount(tasks []*Task[ValidationOutput]) (count int) {
return count
}

func createTableContents(tasks []*Task[ValidationOutput], resultsView string) (tableContents [][]string) {
func createTableContents(tasks []*utils.Task[ValidationOutput], resultsView string) (tableContents [][]string) {
for _, task := range tasks {
output := task.Output
if resultsView == "file" { // Will also include the path to each
Expand Down
8 changes: 1 addition & 7 deletions pkg/prm/worker_pool.go → pkg/utils/worker_pool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prm
package utils

import (
"sync"
Expand Down Expand Up @@ -79,9 +79,3 @@ func CreateTask[T any](name string, f func() T, output T) *Task[T] {
f: f,
}
}

type ValidationOutput struct {
err error
exitCode ValidateExitCode
stdout string
}

0 comments on commit 0e3312e

Please sign in to comment.