Skip to content

Commit

Permalink
feat(internal): improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed Apr 9, 2023
1 parent 5ef60b3 commit 71850f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ var (
// initialisePlugins installs plugins if needed and validates their configuration.
func initialisePlugins(configuration commons.Config) error {
for _, plugin := range configuration.Plugins {
plugin.Validate()
err := plugin.Validate()
if err != nil {
logger.Error("Error validating plugin", "plugin_name", plugin.Name, "error", err)
return err
}
if *install {
_, err := plugin.Install()
if err != nil {
Expand Down Expand Up @@ -83,7 +87,10 @@ func runModPlugins(configuration *commons.Config, checks *[]commons.Tests) bool
for _, plugin := range configuration.Plugins {
if plugin.Type == "mod" {
mod = true
latestVersion, _ := commons.GetLatestReleaseTag(plugin)
latestVersion, err := commons.GetLatestReleaseTag(plugin)
if err != nil {
logger.Error("Error getting latest release tag", "plugin_name", plugin.Name, "error", err)
}
if plugin.Version != latestVersion {
logger.Info("New version available for plugin", "plugin_name", plugin.Name, "latest_version", latestVersion)
}
Expand All @@ -98,18 +105,21 @@ func runModPlugins(configuration *commons.Config, checks *[]commons.Tests) bool
func runReportPlugins(configuration *commons.Config, checks *[]commons.Tests) {
for _, plugin := range configuration.Plugins {
if plugin.Type == "report" {
logger.Debug("Running report plugin", "plugin_name", plugin.Name)
latestVersion, _ := commons.GetLatestReleaseTag(plugin)

if plugin.Version != latestVersion {
logger.Info("New version available for plugin", "plugin_name", plugin.Name, "latest_version", latestVersion)
}
logger.Debug("Running report plugin", "plugin_name", plugin.Name)
manager.RunPlugin(plugin, configuration)
}
}
}

// Execute runs YATAS.
func Execute() error {

// Parse the config file
configuration, err := parseConfig()
if err != nil {
Expand All @@ -128,13 +138,16 @@ func Execute() error {

// Run Mods plugins
if runModPlugins(configuration, &checks) {
logger.Debug("Mod plugins executed, skipping checks")
return nil
}

// Run checks plugins
logger.Debug("Running checks plugins")
runChecksPlugins(configuration, &checks)

// Clean results
logger.Debug("Cleaning results")
checks = report.RemoveIgnored(configuration, checks)

// Sort checks by ID
Expand All @@ -150,13 +163,17 @@ func Execute() error {
configuration.Tests = checks

// Compare with previous report
logger.Debug("Comparing with previous report")
compareResults(configuration, &checks)

// CI reporting
logger.Debug("CI reporting")
ciReporting(checks)

// Run report plugins
logger.Debug("Running report plugins")
runReportPlugins(configuration, &configuration.Tests)

logger.Debug("Done")
return nil
}
9 changes: 9 additions & 0 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
onlyFailure = flag.Bool("only-failure", false, "print only failed checks")
)

// countResultOkOverall counts the number of OK and total results.
func countResultOkOverall(results []commons.Result) (int, int) {
var ok int
var all int
Expand All @@ -36,6 +37,7 @@ func countResultOkOverall(results []commons.Result) (int, int) {
return ok, all
}

// IsIgnored checks if a result is ignored based on the configuration.
func IsIgnored(c *commons.Config, r commons.Result, check commons.Check) bool {
for _, ignored := range c.Ignore {
if ignored.ID == check.Id {
Expand All @@ -51,6 +53,7 @@ func IsIgnored(c *commons.Config, r commons.Result, check commons.Check) bool {
return false
}

// RemoveIgnored removes ignored checks from the given tests based on the configuration.
func RemoveIgnored(c *commons.Config, tests []commons.Tests) []commons.Tests {
resultsTmp := []commons.Tests{}
for _, test := range tests {
Expand All @@ -74,6 +77,7 @@ func RemoveIgnored(c *commons.Config, tests []commons.Tests) []commons.Tests {
return resultsTmp
}

// CountChecksPassedOverall counts the number of passed and total checks.
func CountChecksPassedOverall(checks []commons.Check) (int, int) {
var ok int
var all int
Expand All @@ -86,6 +90,7 @@ func CountChecksPassedOverall(checks []commons.Check) (int, int) {
return ok, all
}

// PrettyPrintChecks prints the checks in a human-readable format.
func PrettyPrintChecks(checks []commons.Tests, c *commons.Config) {
flag.Parse()
for _, tests := range checks {
Expand Down Expand Up @@ -127,6 +132,7 @@ func PrettyPrintChecks(checks []commons.Tests, c *commons.Config) {
}
}

// ComparePreviousWithNew compares the previous test results with the new ones and returns the difference.
func ComparePreviousWithNew(previous []commons.Tests, new []commons.Tests) []commons.Tests {
returnedResults := []commons.Tests{}
for _, tests := range new {
Expand Down Expand Up @@ -161,6 +167,7 @@ func ComparePreviousWithNew(previous []commons.Tests, new []commons.Tests) []com
return returnedResults
}

// ReadPreviousResults reads the previous test results from the results.yaml file.
func ReadPreviousResults() []commons.Tests {
d, err := ioutil.ReadFile("results.yaml")
if err != nil {
Expand All @@ -174,6 +181,7 @@ func ReadPreviousResults() []commons.Tests {
return checks
}

// WriteChecksToFile writes the test results to the results.yaml file.
func WriteChecksToFile(checks []commons.Tests, c *commons.Config) {
for _, tests := range checks {
var checksToWrite []commons.Check
Expand All @@ -199,6 +207,7 @@ func WriteChecksToFile(checks []commons.Tests, c *commons.Config) {

}

// ExitCode returns the exit code for the CI based on the test results.
func ExitCode(checks []commons.Tests) int {
var exitCode int
for _, tests := range checks {
Expand Down

0 comments on commit 71850f2

Please sign in to comment.