Skip to content

Commit

Permalink
Extract check to func
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Jun 12, 2024
1 parent 59f91c0 commit 84a150f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
44 changes: 24 additions & 20 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/topdown"

Expand Down Expand Up @@ -248,7 +249,7 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) {

// regal rules are loaded here and passed to the linter separately
// as the configuration is also used to determine feature toggles
// and the defaults from from the data.yaml here.
// and the defaults from the data.yaml here.
regalRules := rio.MustLoadRegalBundleFS(rbundle.Bundle)

regal := linter.NewEmptyLinter().
Expand Down Expand Up @@ -321,25 +322,7 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) {
m.Timer(regalmetrics.RegalConfigParse).Stop()
}

go func() {
mergedConfig, err := config.LoadConfigWithDefaultsFromBundle(&regalRules, &userConfig)
if err != nil {
if params.debug {
log.Printf("failed to merge user config with default config when checking version: %v", err)
}
return
}

if mergedConfig.Features.Remote.CheckVersion &&
os.Getenv(update.CheckVersionDisableEnvVar) != "false" {
update.CheckAndWarn(update.Options{
CurrentVersion: version.Version,
CurrentTime: time.Now().UTC(),
Debug: params.debug,
StateDir: config.GlobalDir(),
}, os.Stderr)
}
}()
go updateCheckAndWarn(params, regalRules, &userConfig)

result, err := regal.Lint(ctx)
if err != nil {
Expand All @@ -354,6 +337,27 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) {
return result, rep.Publish(ctx, result) //nolint:wrapcheck
}

func updateCheckAndWarn(params *lintCommandParams, regalRules bundle.Bundle, userConfig *config.Config) {
mergedConfig, err := config.LoadConfigWithDefaultsFromBundle(&regalRules, userConfig)
if err != nil {
if params.debug {
log.Printf("failed to merge user config with default config when checking version: %v", err)
}

return
}

if mergedConfig.Features.Remote.CheckVersion &&
os.Getenv(update.CheckVersionDisableEnvVar) != "" {
update.CheckAndWarn(update.Options{
CurrentVersion: version.Version,
CurrentTime: time.Now().UTC(),
Debug: params.debug,
StateDir: config.GlobalDir(),
}, os.Stderr)
}
}

func getReporter(format string, outputWriter io.Writer) (reporter.Reporter, error) {
switch format {
case formatPretty:
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (l *LanguageServer) StartConfigWorker(ctx context.Context) {
//nolint:contextcheck
go func() {
if l.loadedConfig.Features.Remote.CheckVersion &&
os.Getenv(update.CheckVersionDisableEnvVar) != "false" {
os.Getenv(update.CheckVersionDisableEnvVar) != "" {
update.CheckAndWarn(update.Options{
CurrentVersion: version.Version,
CurrentTime: time.Now().UTC(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewLinter() Linter {
}
}

// NewEmptyLinter creates a linter with no rule bundles
// NewEmptyLinter creates a linter with no rule bundles.
func NewEmptyLinter() Linter {
return Linter{}
}
Expand Down

0 comments on commit 84a150f

Please sign in to comment.