diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index 278f5f3bdf3..23e35b140b0 100644 --- a/pkg/plugins/golang/v4/init.go +++ b/pkg/plugins/golang/v4/init.go @@ -22,7 +22,6 @@ import ( "os" "path/filepath" "strings" - "unicode" "github.com/spf13/pflag" @@ -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{ + ".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)