-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) remove drone-yaml dependency. use docker compiler for lint
- Loading branch information
TP Honey
committed
Dec 22, 2021
1 parent
af5da0b
commit 9ed60f5
Showing
6 changed files
with
230 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,83 @@ | ||
package lint | ||
|
||
import ( | ||
"github.com/drone/drone-yaml/yaml" | ||
"github.com/drone/drone-yaml/yaml/linter" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/drone-runners/drone-runner-docker/engine/linter" | ||
"github.com/drone-runners/drone-runner-docker/engine/resource" | ||
"github.com/drone/drone-go/drone" | ||
"github.com/drone/envsubst" | ||
"github.com/drone/runner-go/manifest" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
// Command exports the linter command. | ||
var Command = cli.Command{ | ||
Name: "lint", | ||
Usage: "lint the yaml file", | ||
Usage: "lint the yaml file, checks for yaml errors", | ||
ArgsUsage: "<source>", | ||
Action: lint, | ||
Flags: []cli.Flag{ | ||
cli.BoolFlag{ | ||
Name: "trusted", | ||
Usage: "is the yaml trustable", | ||
Name: "trusted", | ||
Hidden: true, | ||
Usage: "is the yaml trustable", | ||
}, | ||
}, | ||
} | ||
|
||
type Flags struct { | ||
Build drone.Build | ||
Netrc drone.Netrc | ||
Repo drone.Repo | ||
Stage drone.Stage | ||
System drone.System | ||
} | ||
|
||
func lint(c *cli.Context) error { | ||
f := new(Flags) | ||
var envs map[string]string | ||
|
||
path := c.Args().First() | ||
if path == "" { | ||
path = ".drone.yml" | ||
} | ||
|
||
manifest, err := yaml.ParseFile(path) | ||
rawsource, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, resource := range manifest.Resources { | ||
if err := linter.Lint(resource, c.Bool("trusted")); err != nil { | ||
return err | ||
// string substitution function ensures that string replacement variables are escaped and quoted if they contain newlines. | ||
subf := func(k string) string { | ||
v := envs[k] | ||
if strings.Contains(v, "\n") { | ||
v = fmt.Sprintf("%q", v) | ||
} | ||
return v | ||
} | ||
// evaluates string replacement expressions and returns an update configuration. | ||
config, err := envsubst.Eval(string(rawsource), subf) | ||
if err != nil { | ||
return err | ||
} | ||
// parse and lint the configuration | ||
manifest, err := manifest.ParseString(config) | ||
if err != nil { | ||
return err | ||
} | ||
// a configuration can contain multiple pipelines. get a specific pipeline resource for execution. | ||
resource, err := resource.Lookup(f.Stage.Name, manifest) | ||
if err != nil { | ||
return err | ||
} | ||
// lint the pipeline and return an error if any linting rules are broken | ||
lint := linter.New() | ||
err = lint.Lint(resource, &f.Repo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.