Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions pkg/plugins/golang/v4/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path/filepath"
"strings"
"unicode"

"github.com/spf13/pflag"

Expand Down Expand Up @@ -167,45 +166,49 @@ func checkDir() error {
if err != nil {
return fmt.Errorf("error walking path %q: %w", path, err)
}
// Allow directory trees starting with '.'
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && info.Name() != "." {
return filepath.SkipDir
}
// Allow files starting with '.'
if strings.HasPrefix(info.Name(), ".") {
return nil
}
// Allow files ending with '.md' extension
if strings.HasSuffix(info.Name(), ".md") && !info.IsDir() {
return nil
}
// Allow capitalized files except PROJECT
isCapitalized := true
for _, l := range info.Name() {
if !unicode.IsUpper(l) {
isCapitalized = false
break
}
}
if isCapitalized && info.Name() != "PROJECT" {

// Only care about top-level files and directories
if strings.Count(path, string(os.PathSeparator)) > 0 {
return nil
}
disallowedExtensions := []string{
".go",
".yaml",
".mod",
".sum",

// Do not allow kubebuilder reserved directories or files
reservedNames := []string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is definitely an option.
I will need some time to think about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue here is how to maintain this list.
Moreover: In a multi-repository project, there will typically be a .github directory — and that’s probably fine. However, the tool would need to scaffold files inside that directory, which raises the question of how to manage and keep the list consistent across repositories.

".devcontainer",
".github",
"api",
"bin",
"cmd",
"config",
"dist",
"hack",
"internal",
"test",
".dockerignore",
".gitignore",
".golangci.yml",
"Dockerfile",
"Makefile",
"PROJECT",
"README.md",

// plugins
"grafana",

// The go.mod is allowed because user might run
// go mod init before use the plugin it for not be required inform
// the go module via the repo --flag.
// "go.mod",
}
// Deny files with .go or .yaml or .mod or .sum extensions
for _, ext := range disallowedExtensions {
if strings.HasSuffix(info.Name(), ext) {
return nil
for _, name := range reservedNames {
if info.Name() == name {
return fmt.Errorf("target directory contains kubebuilder reserved directory or file: %q (%s)",
path, strings.Join(reservedNames, ", "))
}
}
// Do not allow any other file
return fmt.Errorf("target directory is not empty and contains a disallowed file %q. "+
"files with the following extensions [%s] are not allowed to avoid conflicts with the tooling",
path, strings.Join(disallowedExtensions, ", "))

// Allow everything else
return nil
})
if err != nil {
return fmt.Errorf("error walking directory: %w", err)
Expand Down
Loading