Skip to content

Commit

Permalink
improve globbing in addlicense workflow
Browse files Browse the repository at this point in the history
Enumerating Go files via `go list` ignores the vendor/ directory
entirely, thus avoiding annoying logspam from addlicense about how it
ignores every individual vendored file.
  • Loading branch information
majewsky committed Jan 9, 2024
1 parent ff7243e commit d7737b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,31 +445,20 @@ securityChecks:

#### `githubWorkflow.license`

This workflow ensures that all your source code files have a license header. It
uses [`addlicense`][addlicense] for this.
This workflow uses [`addlicense`][addlicense`] to ensure that all your Go source code files have a license header.
If vendoring is enabled, the `vendor/` directory is always entirely ignored by this workflow.

```yaml
license:
enabled: true
patterns:
- "**/*.go"
ignorePatterns:
- "vendor/**"
```

`patterns` specifies a list of file patterns to check. For convenience, the `globstar`
option is enabled for the workflow's shell session therefore you can use `**` in your file
patterns. Additionally, `addlicense` will scan directory patterns recursively. See
`addlicense`'s [README][addlicense] for more info. Default value for this is `**/*.go`,
i.e. check all Go files.
`ignorePatterns` specifies a list of file patterns to check. You can use any pattern
[supported by doublestar][doublestar-pattern]. See `addlicense`'s [README][addlicense] for
more info. Default value for this is `vendor/**`, i.e. exclude everything under `vendor`
directory.
[supported by doublestar][doublestar-pattern]. See `addlicense`'s [README][addlicense] for more info.

**Hint**: you can also use `addlicense` to add license headers to all Go files excluding
`vendor` directory by running `make license-headers`.
**Hint**: You can also use `addlicense` to add license headers to all unignored Go files by running `make license-headers`.

[codeql]: https://codeql.github.com/
[coveralls]: https://coveralls.io
Expand Down
1 change: 0 additions & 1 deletion internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ type CIWorkflowConfig struct {
// LicenseWorkflowConfig appears in type Configuration.
type LicenseWorkflowConfig struct {
Enabled bool `yaml:"enabled"`
Patterns []string `yaml:"patterns"`
IgnorePatterns []string `yaml:"ignorePatterns"`
}

Expand Down
33 changes: 13 additions & 20 deletions internal/makefile/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,44 +246,37 @@ endif
}

if isSAPCC {
// Default behavior is to check all Go files excluding the vendor directory.
patterns := []string{"**/*.go"}
if len(cfg.GitHubWorkflow.License.Patterns) > 0 {
patterns = cfg.GitHubWorkflow.License.Patterns
}
allGoFilesExpr := `$(patsubst $(shell go list .)%,.%/*.go,$(shell go list ./...))`

ignorePatterns := []string{"vendor/**"}
if len(cfg.GitHubWorkflow.License.IgnorePatterns) > 0 {
ignorePatterns = append(ignorePatterns, cfg.GitHubWorkflow.License.IgnorePatterns...)
}
// Each ignore pattern is quoted to avoid glob expansion and prefixed with the `-ignore` flag.
for i, v := range ignorePatterns {
ignorePatterns[i] = fmt.Sprintf("-ignore %q", v)
ignoreOptions := make([]string, len(cfg.GitHubWorkflow.License.IgnorePatterns))
for idx, pattern := range cfg.GitHubWorkflow.License.IgnorePatterns {
//quoting avoids glob expansion
ignoreOptions[idx] = fmt.Sprintf("-ignore %q", pattern)
}

dev.addRule(rule{
description: "Add license headers to all .go files excluding the vendor directory.",
description: "Add license headers to all non-vendored .go files.",
target: "license-headers",
phony: true,
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@printf "\e[1;36m>> addlicense\e[0m\n"`,
fmt.Sprintf(`@addlicense -c "SAP SE" %s -- %s`,
strings.Join(ignorePatterns, " "),
strings.Join(patterns, " "),
strings.Join(ignoreOptions, " "),
allGoFilesExpr,
)},
})

dev.addRule(rule{
description: "Check license headers in all .go files excluding the vendor directory.",
description: "Check license headers in all non-vendored .go files.",
target: "check-license-headers",
phony: true,
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@printf "\e[1;36m>> addlicense\e[0m\n"`,
fmt.Sprintf(`@bash -c 'shopt -s globstar; addlicense --check %s -- %s'`,
strings.Join(ignorePatterns, " "),
strings.Join(patterns, " "),
`@printf "\e[1;36m>> addlicense --check\e[0m\n"`,
fmt.Sprintf(`@addlicense --check %s -- %s`,
strings.Join(ignoreOptions, " "),
allGoFilesExpr,
)},
})

Expand Down

0 comments on commit d7737b1

Please sign in to comment.