Skip to content

Commit e624b6d

Browse files
(go/v4): Simplify init directory validation to only block PROJECT fil
Remove overly restrictive directory validation that was blocking legitimate files like mise.toml and other configuration files. The previous logic attempted to allow only specific file types but had inverted logic that blocked most files unnecessarily. At this stage, we don't have a maintainable way to accurately predict all possible file conflicts. The only file that definitively conflicts with scaffolding is the PROJECT file, which kubebuilder generates. This allows users to run kubebuilder init in directories with legitimate configuration files (mise.toml, .envrc, .tool-versions, etc.) without encountering false validation errors.
1 parent 78fbec7 commit e624b6d

File tree

1 file changed

+8
-38
lines changed

1 file changed

+8
-38
lines changed

pkg/plugins/golang/v4/init.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"path/filepath"
2424
"strings"
25-
"unicode"
2625

2726
"github.com/spf13/pflag"
2827

@@ -159,53 +158,24 @@ func (p *initSubcommand) PostScaffold() error {
159158
}
160159

161160
// checkDir will return error if the current directory has files which are not allowed.
162-
// Note that, it is expected that the directory to scaffold the project is cleaned.
163-
// Otherwise, it might face issues to do the scaffold.
161+
// Only the PROJECT file is disallowed as it will be generated by the scaffolding tool.
164162
func checkDir() error {
165163
err := filepath.Walk(".",
166164
func(path string, info os.FileInfo, err error) error {
167165
if err != nil {
168166
return fmt.Errorf("error walking path %q: %w", path, err)
169167
}
170-
// Allow directory trees starting with '.'
168+
// Skip dot directories
171169
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && info.Name() != "." {
172170
return filepath.SkipDir
173171
}
174-
// Allow files starting with '.'
175-
if strings.HasPrefix(info.Name(), ".") {
176-
return nil
172+
// Only block PROJECT file (reserved for scaffolding)
173+
if !info.IsDir() && info.Name() == "PROJECT" {
174+
return fmt.Errorf("PROJECT file found in target directory. " +
175+
"This file is reserved and will be generated by kubebuilder")
177176
}
178-
// Allow files ending with '.md' extension
179-
if strings.HasSuffix(info.Name(), ".md") && !info.IsDir() {
180-
return nil
181-
}
182-
// Allow capitalized files except PROJECT
183-
isCapitalized := true
184-
for _, l := range info.Name() {
185-
if !unicode.IsUpper(l) {
186-
isCapitalized = false
187-
break
188-
}
189-
}
190-
if isCapitalized && info.Name() != "PROJECT" {
191-
return nil
192-
}
193-
disallowedExtensions := []string{
194-
".go",
195-
".yaml",
196-
".mod",
197-
".sum",
198-
}
199-
// Deny files with .go or .yaml or .mod or .sum extensions
200-
for _, ext := range disallowedExtensions {
201-
if strings.HasSuffix(info.Name(), ext) {
202-
return nil
203-
}
204-
}
205-
// Do not allow any other file
206-
return fmt.Errorf("target directory is not empty and contains a disallowed file %q. "+
207-
"files with the following extensions [%s] are not allowed to avoid conflicts with the tooling",
208-
path, strings.Join(disallowedExtensions, ", "))
177+
// Allow everything else
178+
return nil
209179
})
210180
if err != nil {
211181
return fmt.Errorf("error walking directory: %w", err)

0 commit comments

Comments
 (0)