mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-29 11:50:37 +00:00
ad509fd86f
close #443 * add support to exec * auto detect multi-pipeline for lint and exec * de-duplicate code
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package lint
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/schema"
|
|
)
|
|
|
|
// Command exports the info command.
|
|
var Command = &cli.Command{
|
|
Name: "lint",
|
|
Usage: "lint a pipeline configuration file",
|
|
ArgsUsage: "[path/to/.woodpecker.yml]",
|
|
Action: lint,
|
|
Flags: common.GlobalFlags,
|
|
}
|
|
|
|
func lint(c *cli.Context) error {
|
|
return common.RunPipelineFunc(c, lintFile, lintDir)
|
|
}
|
|
|
|
func lintDir(c *cli.Context, dir string) error {
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
// check if it is a regular file (not dir)
|
|
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".yml") {
|
|
fmt.Println("#", info.Name())
|
|
_ = lintFile(c, path) // TODO: should we drop errors or store them and report back?
|
|
fmt.Println("")
|
|
return nil
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func lintFile(_ *cli.Context, file string) error {
|
|
configErrors, err := schema.Lint(file)
|
|
if err != nil {
|
|
fmt.Println("❌ Config is invalid")
|
|
for _, configError := range configErrors {
|
|
fmt.Println("In", configError.Field()+":", configError.Description())
|
|
}
|
|
return err
|
|
}
|
|
|
|
fmt.Println("✅ Config is valid")
|
|
return nil
|
|
}
|