From 84a150fa390d85f2d52dfdccfcf72e911290859e Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Wed, 12 Jun 2024 13:49:17 +0100 Subject: [PATCH] Extract check to func --- cmd/lint.go | 44 +++++++++++++++++++++++------------------- internal/lsp/server.go | 2 +- pkg/linter/linter.go | 2 +- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/cmd/lint.go b/cmd/lint.go index 18483c1a..392ae044 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -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" @@ -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(). @@ -321,25 +322,7 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) { m.Timer(regalmetrics.RegalConfigParse).Stop() } - go func() { - mergedConfig, err := config.LoadConfigWithDefaultsFromBundle(®alRules, &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 { @@ -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(®alRules, 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: diff --git a/internal/lsp/server.go b/internal/lsp/server.go index d7f9f20e..93255226 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -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(), diff --git a/pkg/linter/linter.go b/pkg/linter/linter.go index 389e891b..07362f28 100644 --- a/pkg/linter/linter.go +++ b/pkg/linter/linter.go @@ -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{} }