diff --git a/main.go b/main.go index 876d56e..7c8d8a5 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,7 @@ import ( // Config holds the application configuration type Config struct { RepositoryPath string - CheckPaths bool `envconfig:"default=true"` - CheckOwners bool `envconfig:"default=true"` + Checks []string `envconfig:"optional"` Github struct { AccessToken string `envconfig:"optional"` BaseURL string `envconfig:"optional"` @@ -35,8 +34,6 @@ type Config struct { func main() { var cfg Config - var checks []check.Checker - var ghClient *github.Client err := envconfig.Init(&cfg) fatalOnError(err) @@ -46,40 +43,19 @@ func main() { defer cancelFunc() cancelOnInterrupt(ctx, cancelFunc) - // check are the both checks disabled - if !cfg.CheckPaths && !cfg.CheckOwners { - log.Error("No checks requested. Please set CHECK_PATHS or CHECK_OWNERS. Aborting...") - fatalOnError(err) - } - fatalOnError(err) - // init codeowners entries codeownersEntries, err := codeowners.NewFromPath(cfg.RepositoryPath) fatalOnError(err) -<<<<<<< HEAD -<<<<<<< HEAD - if cfg.CheckPaths { -======= - if cfg.CheckPaths == true { ->>>>>>> Adding more checks for team permissions. Adding more ENV variables -======= - if cfg.CheckPaths { ->>>>>>> Fixed comparison to bool constant - checks = []check.Checker{ - check.NewFileExist(), - } + // init checks + var checks []check.Checker + + if isEnabled(cfg.Checks, "files") { + checks = append(checks, check.NewFileExist()) } -<<<<<<< HEAD -<<<<<<< HEAD - if cfg.CheckOwners { -======= - if cfg.CheckOwners == true { ->>>>>>> Adding more checks for team permissions. Adding more ENV variables -======= - if cfg.CheckOwners { ->>>>>>> Fixed comparison to bool constant + if isEnabled(cfg.Checks, "owners") { + // init GitHub client httpClient := http.DefaultClient if cfg.Github.AccessToken != "" { httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource( @@ -87,14 +63,10 @@ func main() { )) } - ghClient, err = newGithubClient(cfg, httpClient) + ghClient, err := newGithubClient(cfg, httpClient) fatalOnError(err) - checks = []check.Checker{ - check.NewFileExist(), - check.NewValidOwner(cfg.OwnerChecker, ghClient), - } - + checks = append(checks, check.NewValidOwner(cfg.OwnerChecker, ghClient)) } // run check runner @@ -113,6 +85,27 @@ func main() { } } +func isEnabled(checks []string, name string) bool { + // if a user does not specify concrete checks then all checks are enabled + if len(checks) == 0 { + return true + } + + if contains(checks, name) { + return true + } + return false +} + +func contains(checks []string, name string) bool { + for _, c := range checks { + if c == name { + return true + } + } + return false +} + func newGithubClient(cfg Config, httpClient *http.Client) (ghClient *github.Client, err error) { baseURL, uploadURL := cfg.Github.BaseURL, cfg.Github.UploadURL