From 90b515f4f77c03b22484c7ca1816e0a166709a71 Mon Sep 17 00:00:00 2001 From: Robin LIORET Date: Thu, 23 Oct 2025 18:34:25 +0200 Subject: [PATCH 1/5] feat: add allowed files about tools version management files in go init v4 Signed-off-by: Robin LIORET --- pkg/plugins/golang/v4/init.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index 278f5f3bdf3..a7d7aa84ee8 100644 --- a/pkg/plugins/golang/v4/init.go +++ b/pkg/plugins/golang/v4/init.go @@ -190,6 +190,17 @@ func checkDir() error { if isCapitalized && info.Name() != "PROJECT" { return nil } + allowedFiles := []string{ + // User might use tool versions management tools to set up the environment including kubebuilder and go version + "mise.toml", // mise-en-place configuration file + ".tool-versions", // asdf configuration file + } + // Allow files used by tool versions management + for _, ext := range allowedFiles { + if info.Name() == ext { + return nil + } + } disallowedExtensions := []string{ ".go", ".yaml", From 8d571bd5f19848d70a42c6fd1d9e872b14c1fdc5 Mon Sep 17 00:00:00 2001 From: Robin LIORET Date: Thu, 23 Oct 2025 22:22:47 +0200 Subject: [PATCH 2/5] feat: rework plguin golang/v4 init directory checking Signed-off-by: Robin LIORET --- pkg/plugins/golang/v4/init.go | 78 ++++++++++++++--------------------- 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index a7d7aa84ee8..eda03804492 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,56 +166,43 @@ 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)) > 1 { return nil } - allowedFiles := []string{ - // User might use tool versions management tools to set up the environment including kubebuilder and go version - "mise.toml", // mise-en-place configuration file - ".tool-versions", // asdf configuration file - } - // Allow files used by tool versions management - for _, ext := range allowedFiles { - if info.Name() == ext { - 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", } - // 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", path) } } - // 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) From 97251cf47ca6264caa5519b321d33a55fbe60c8c Mon Sep 17 00:00:00 2001 From: Robin LIORET Date: Thu, 23 Oct 2025 22:36:12 +0200 Subject: [PATCH 3/5] feat: add more useful information to the plugin golang/v4 checkDir Signed-off-by: Robin LIORET --- pkg/plugins/golang/v4/init.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index eda03804492..b300b6ce42f 100644 --- a/pkg/plugins/golang/v4/init.go +++ b/pkg/plugins/golang/v4/init.go @@ -194,10 +194,16 @@ func checkDir() error { // 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", } for _, name := range reservedNames { if info.Name() == name { - return fmt.Errorf("target directory contains kubebuilder reserved directory or file: %q", path) + return fmt.Errorf("target directory contains kubebuilder reserved directory or file: %q (%s)", + path, strings.Join(reservedNames, ", ")) } } From 82ff3740dfc6363c5e83bb1173eabe990f12da05 Mon Sep 17 00:00:00 2001 From: Robin LIORET Date: Thu, 23 Oct 2025 22:36:58 +0200 Subject: [PATCH 4/5] chore: pass linting Signed-off-by: Robin LIORET --- pkg/plugins/golang/v4/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index b300b6ce42f..eab4c2bd3e4 100644 --- a/pkg/plugins/golang/v4/init.go +++ b/pkg/plugins/golang/v4/init.go @@ -198,7 +198,7 @@ func checkDir() error { // 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", + // "go.mod", } for _, name := range reservedNames { if info.Name() == name { From 17978ef75d09579a16441b1b6510a72d38e67383 Mon Sep 17 00:00:00 2001 From: Robin LIORET Date: Thu, 23 Oct 2025 22:45:44 +0200 Subject: [PATCH 5/5] fix: nested files Signed-off-by: Robin LIORET --- pkg/plugins/golang/v4/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/plugins/golang/v4/init.go b/pkg/plugins/golang/v4/init.go index eab4c2bd3e4..23e35b140b0 100644 --- a/pkg/plugins/golang/v4/init.go +++ b/pkg/plugins/golang/v4/init.go @@ -168,7 +168,7 @@ func checkDir() error { } // Only care about top-level files and directories - if strings.Count(path, string(os.PathSeparator)) > 1 { + if strings.Count(path, string(os.PathSeparator)) > 0 { return nil }