Skip to content

Commit

Permalink
feat(plugins): added possibility to run mods, reports plugins
Browse files Browse the repository at this point in the history
Yatas is now compatible with 3 types of plugins: Checks, reports and mods that completely change the
way app is ran
  • Loading branch information
StanGirard committed Sep 16, 2022
1 parent acb0683 commit 28d0caa
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 32 deletions.
138 changes: 106 additions & 32 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,120 @@ var (
install = flag.Bool("install", false, "install plugins")
)

func Execute() error {
configuration, err := commons.ParseConfig(".yatas.yml")
if err != nil {
return err
}
func initialisePlugins(configuration commons.Config) error {
for _, plugins := range configuration.Plugins {
plugins.Validate()
if *install {
plugins.Install()
return nil
_, err := plugins.Install()
if err != nil {
return err
}
}
}
var checks []commons.Tests
return nil
}

func RunChecksPlugins(configuration *commons.Config, checks *[]commons.Tests) {
for _, plugins := range configuration.Plugins {
if plugins.Type == "checks" || plugins.Type == "" {

latestVersion, _ := commons.GetLatestReleaseTag(plugins)

if plugins.Version != latestVersion {
fmt.Println("New version available for plugin " + plugins.Name + " : " + latestVersion)
}
checksFromPlugin := manager.RunPlugin(plugins, configuration)
*checks = append(*checks, checksFromPlugin...)
}
}
}

func parseConfig() (*commons.Config, error) {
configuration, err := commons.ParseConfig(".yatas.yml")
if err != nil {
return nil, err
}
return configuration, nil
}

func compareResults(configuration *commons.Config, checks *[]commons.Tests) {
// Compare with previous report
if *compare {
previous := report.ReadPreviousResults()
checksCompare := report.ComparePreviousWithNew(previous, *checks)
report.PrettyPrintChecks(checksCompare, configuration)
report.WriteChecksToFile(*checks, configuration)
checks = &checksCompare
} else {
report.PrettyPrintChecks(*checks, configuration)
report.WriteChecksToFile(*checks, configuration)

}
}

func ciReporting(checks []commons.Tests) {
if *ci {
os.Exit(report.ExitCode(checks))
}
}

func runModPlugins(configuration *commons.Config, checks *[]commons.Tests) bool {
mod := false
for _, plugins := range configuration.Plugins {
latestVersion, err := commons.GetLatestReleaseTag(plugins)
if err != nil {
return err
if plugins.Type == "mod" {
mod = true
latestVersion, _ := commons.GetLatestReleaseTag(plugins)

if plugins.Version != latestVersion {
fmt.Println("New version available for plugin " + plugins.Name + " : " + latestVersion)
}
checksFromPlugin := manager.RunPlugin(plugins, configuration)
*checks = append(*checks, checksFromPlugin...)
}
if plugins.Version != latestVersion {
fmt.Println("New version available for plugin " + plugins.Name + " : " + latestVersion)
}
return mod
}

func RunReportPlugins(configuration *commons.Config, checks *[]commons.Tests) {
for _, plugins := range configuration.Plugins {
if plugins.Type == "report" {
latestVersion, _ := commons.GetLatestReleaseTag(plugins)

if plugins.Version != latestVersion {
fmt.Println("New version available for plugin " + plugins.Name + " : " + latestVersion)
}
manager.RunPlugin(plugins, configuration)
}
checks = manager.RunPlugin(plugins, configuration)
}
}

// Execute YATAS
func Execute() error {
// Parse the config file
configuration, err := parseConfig()
if err != nil {
return err
}

// Initialise plugins by installing them if needed and checking if the config is valid
err = initialisePlugins(*configuration)
if err != nil {
return err
}

checks := []commons.Tests{}

// Run Mods plugins
if runModPlugins(configuration, &checks) {
return nil
}

// Run plugins
RunChecksPlugins(configuration, &checks)

// Clean results
checks = report.RemoveIgnored(configuration, checks)
// if !*progressflag {

// }
// Sort checks by ID
sort.Slice(checks, func(i, j int) bool {
return checks[i].Account < checks[j].Account
Expand All @@ -58,23 +141,14 @@ func Execute() error {
})
}

if *compare {
previous := report.ReadPreviousResults()
if err != nil {
return err
}
checksCompare := report.ComparePreviousWithNew(previous, checks)
report.PrettyPrintChecks(checksCompare, configuration)
report.WriteChecksToFile(checks, configuration)
checks = checksCompare
} else {
report.PrettyPrintChecks(checks, configuration)
report.WriteChecksToFile(checks, configuration)
// Compare with previous report
compareResults(configuration, &checks)

}
if *ci {
os.Exit(report.ExitCode(checks))
}
// CI reporting
ciReporting(checks)

// Run report plugins
RunReportPlugins(configuration, &checks)

return nil
}
5 changes: 5 additions & 0 deletions plugins/commons/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Plugin struct {
Name string `yaml:"name"`
Enabled bool `yaml:"enabled"`
Source string `yaml:"source"`
Type string `default:"checks" yaml:"type" `
Version string `yaml:"version"`
Description string `yaml:"description"`
Exclude []string `yaml:"exclude"`
Expand Down Expand Up @@ -56,6 +57,10 @@ func (c *Plugin) Validate() error {
return fmt.Errorf("plugin `%s`: `source` attribute cannot be omitted when specifying `version`", c.Name)
}

if c.Type != "checks" && c.Type != "" && c.Type != "reporting" && c.Type != "mod" {
return fmt.Errorf("plugin `%s`: `type` attribute must be either `checks` or `reporting` or `mod`", c.Name)
}

if c.Source != "" {
if c.Version == "" {
return fmt.Errorf("plugin `%s`: `version` attribute cannot be omitted when specifying `source`", c.Name)
Expand Down

0 comments on commit 28d0caa

Please sign in to comment.