2021-09-27 02:38:15 +02:00
|
|
|
package lint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-27 02:38:15 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the info command.
|
2021-10-27 21:03:14 +02:00
|
|
|
var Command = &cli.Command{
|
2021-09-27 02:38:15 +02:00
|
|
|
Name: "lint",
|
|
|
|
Usage: "lint a pipeline configuration file",
|
|
|
|
ArgsUsage: "[path/to/.woodpecker.yml]",
|
|
|
|
Action: lint,
|
2021-10-27 21:03:14 +02:00
|
|
|
Flags: common.GlobalFlags,
|
2021-09-27 02:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func lint(c *cli.Context) error {
|
2021-12-13 20:51:53 +02:00
|
|
|
return common.RunPipelineFunc(c, lintFile, lintDir)
|
|
|
|
}
|
2021-09-27 02:38:15 +02:00
|
|
|
|
2021-12-13 20:51:53 +02:00
|
|
|
func lintDir(c *cli.Context, dir string) error {
|
2022-07-17 18:25:56 +02:00
|
|
|
var errorStrings []string
|
|
|
|
if err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
2021-09-27 02:38:15 +02:00
|
|
|
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())
|
2022-07-17 18:25:56 +02:00
|
|
|
if err := lintFile(c, path); err != nil {
|
|
|
|
errorStrings = append(errorStrings, err.Error())
|
|
|
|
}
|
2021-09-27 02:38:15 +02:00
|
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-07-17 18:25:56 +02:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errorStrings) != 0 {
|
|
|
|
return fmt.Errorf("ERRORS: %s", strings.Join(errorStrings, "; "))
|
|
|
|
}
|
|
|
|
return nil
|
2021-09-27 02:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 20:51:53 +02:00
|
|
|
func lintFile(_ *cli.Context, file string) error {
|
2022-04-19 10:40:48 +02:00
|
|
|
fi, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
configErrors, err := schema.Lint(fi)
|
2021-09-27 02:38:15 +02:00
|
|
|
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
|
|
|
|
}
|