From e118f8d98047c1fcf40029f31bb220bbb8457351 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:22:09 +0200 Subject: [PATCH] Ignore warnings for cli exec (#3868) --- cli/exec/exec.go | 20 +++++++++++------ cli/lint/lint.go | 34 ++++------------------------ cli/lint/utils.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 cli/lint/utils.go diff --git a/cli/exec/exec.go b/cli/exec/exec.go index 2f588ee72..b299cb58b 100644 --- a/cli/exec/exec.go +++ b/cli/exec/exec.go @@ -29,13 +29,14 @@ import ( "github.com/urfave/cli/v2" "go.woodpecker-ci.org/woodpecker/v2/cli/common" + "go.woodpecker-ci.org/woodpecker/v2/cli/lint" "go.woodpecker-ci.org/woodpecker/v2/pipeline" "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend" "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker" "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/dummy" "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes" "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local" - backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" + backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter" @@ -179,12 +180,17 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error } // lint the yaml file - if err := linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{ + err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{ File: path.Base(file), RawConfig: confStr, Workflow: conf, - }}); err != nil { - return err + }}) + if err != nil { + str, err := lint.FormatLintError(file, err) + fmt.Print(str) + if err != nil { + return err + } } // compiles the yaml file @@ -224,8 +230,8 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error return err } - backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c) - backends := []backendTypes.Backend{ + backendCtx := context.WithValue(c.Context, backend_types.CliContext, c) + backends := []backend_types.Backend{ kubernetes.New(), docker.New(), local.New(), @@ -277,7 +283,7 @@ func convertPathForWindows(path string) string { } const maxLogLineLength = 1024 * 1024 // 1mb -var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.ReadCloser) error { +var defaultLogger = pipeline.Logger(func(step *backend_types.Step, rc io.ReadCloser) error { logWriter := NewLineWriter(step.Name, step.UUID) return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength) }) diff --git a/cli/lint/lint.go b/cli/lint/lint.go index e82ebf050..f77d6cfe0 100644 --- a/cli/lint/lint.go +++ b/cli/lint/lint.go @@ -15,18 +15,15 @@ package lint import ( - "errors" "fmt" "os" "path" "path/filepath" "strings" - term_env "github.com/muesli/termenv" "github.com/urfave/cli/v2" "go.woodpecker-ci.org/woodpecker/v2/cli/common" - pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter" ) @@ -72,8 +69,6 @@ func lintDir(c *cli.Context, dir string) error { } func lintFile(_ *cli.Context, file string) error { - output := term_env.NewOutput(os.Stdout) - fi, err := os.Open(file) if err != nil { return err @@ -101,34 +96,13 @@ func lintFile(_ *cli.Context, file string) error { // TODO: lint multiple files at once to allow checks for sth like "depends_on" to work err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{config}) if err != nil { - fmt.Printf("🔥 %s has warnings / errors:\n", output.String(config.File).Underline()) + str, err := FormatLintError(config.File, err) - hasErrors := false - for _, err := range pipeline_errors.GetPipelineErrors(err) { - line := " " - - if err.IsWarning { - line = fmt.Sprintf("%s ⚠️ ", line) - } else { - line = fmt.Sprintf("%s ❌", line) - hasErrors = true - } - - if data := pipeline_errors.GetLinterData(err); data != nil { - line = fmt.Sprintf("%s %s\t%s", line, output.String(data.Field).Bold(), err.Message) - } else { - line = fmt.Sprintf("%s %s", line, err.Message) - } - - // TODO: use table output - fmt.Printf("%s\n", line) + if str != "" { + fmt.Print(str) } - if hasErrors { - return errors.New("config has errors") - } - - return nil + return err } fmt.Println("✅ Config is valid") diff --git a/cli/lint/utils.go b/cli/lint/utils.go new file mode 100644 index 000000000..5a062ae3e --- /dev/null +++ b/cli/lint/utils.go @@ -0,0 +1,56 @@ +package lint + +import ( + "errors" + "fmt" + "os" + + term_env "github.com/muesli/termenv" + + pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors" +) + +func FormatLintError(file string, err error) (string, error) { + if err == nil { + return "", nil + } + + output := term_env.NewOutput(os.Stdout) + str := "" + + amountErrors := 0 + amountWarnings := 0 + linterErrors := pipeline_errors.GetPipelineErrors(err) + for _, err := range linterErrors { + line := " " + + if err.IsWarning { + line = fmt.Sprintf("%s ⚠️ ", line) + amountWarnings++ + } else { + line = fmt.Sprintf("%s ❌", line) + amountErrors++ + } + + if data := pipeline_errors.GetLinterData(err); data != nil { + line = fmt.Sprintf("%s %s\t%s", line, output.String(data.Field).Bold(), err.Message) + } else { + line = fmt.Sprintf("%s %s", line, err.Message) + } + + // TODO: use table output + str = fmt.Sprintf("%s%s\n", str, line) + } + + if amountErrors > 0 { + if amountWarnings > 0 { + str = fmt.Sprintf("🔥 %s has %d errors and warnings:\n%s", output.String(file).Underline(), len(linterErrors), str) + } else { + str = fmt.Sprintf("🔥 %s has %d errors:\n%s", output.String(file).Underline(), len(linterErrors), str) + } + return str, errors.New("config has errors") + } + + str = fmt.Sprintf("⚠️ %s has %d warnings:\n%s", output.String(file).Underline(), len(linterErrors), str) + return str, nil +}