Skip to content

Commit 678e1d0

Browse files
(go/v4): Simplify init validation to only block kubebuilder-scaffolded files
Allow existing config files; only block PROJECT, Makefile, cmd/main.go, and config/default/kustomization.yaml to prevent conflicts.
1 parent dd84707 commit 678e1d0

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

pkg/plugins/golang/v4/init.go

Lines changed: 49 additions & 37 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

@@ -158,57 +157,70 @@ func (p *initSubcommand) PostScaffold() error {
158157
return nil
159158
}
160159

161-
// 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.
160+
// checkDir checks the target directory before scaffolding:
161+
// 1. Returns error if key kubebuilder files already exist (prevents re-initialization)
162+
// 2. Warns if directory is not empty (but allows scaffolding to continue)
164163
func checkDir() error {
164+
// Files scaffolded by 'kubebuilder init' that indicate the directory is already initialized.
165+
// Blocking these prevents accidental re-initialization and file conflicts.
166+
// Note: go.mod and go.sum are NOT blocked because:
167+
// - They may exist in pre-existing Go projects
168+
// - Kubebuilder will overwrite them (machinery.OverwriteFile)
169+
// - Testdata generation creates go.mod before running init
170+
scaffoldedFiles := []string{
171+
"PROJECT", // Kubebuilder project config (key indicator)
172+
"Makefile", // Build automation
173+
filepath.Join("cmd", "main.go"), // Controller manager entry point
174+
}
175+
176+
// Check for existing scaffolded files
177+
for _, file := range scaffoldedFiles {
178+
if _, err := os.Stat(file); err == nil {
179+
return fmt.Errorf("target directory is already initialized. "+
180+
"Found existing kubebuilder file %q. "+
181+
"Please run this command in a new directory or remove existing scaffolded files", file)
182+
}
183+
}
184+
185+
// Check if directory has any other files (warn only)
186+
// Note: We ignore go.mod and go.sum because:
187+
// - Users may run `go mod init` before `kubebuilder init`
188+
// - Testdata generation explicitly runs `go mod init` before scaffolding
189+
// - These files will be safely overwritten by kubebuilder
190+
var hasFiles bool
165191
err := filepath.Walk(".",
166192
func(path string, info os.FileInfo, err error) error {
167193
if err != nil {
168194
return fmt.Errorf("error walking path %q: %w", path, err)
169195
}
170-
// Allow directory trees starting with '.'
171-
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && info.Name() != "." {
172-
return filepath.SkipDir
173-
}
174-
// Allow files starting with '.'
175-
if strings.HasPrefix(info.Name(), ".") {
196+
// Skip the current directory itself
197+
if path == "." {
176198
return nil
177199
}
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-
}
200+
// Skip dot directories
201+
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
202+
return filepath.SkipDir
189203
}
190-
if isCapitalized && info.Name() != "PROJECT" {
204+
// Skip go.mod and go.sum (expected files that will be overwritten)
205+
if info.Name() == "go.mod" || info.Name() == "go.sum" {
191206
return nil
192207
}
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-
}
208+
// Track if any non-hidden files/directories exist
209+
if !strings.HasPrefix(info.Name(), ".") {
210+
hasFiles = true
204211
}
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, ", "))
212+
return nil
209213
})
210214
if err != nil {
211215
return fmt.Errorf("error walking directory: %w", err)
212216
}
217+
218+
// Warn if directory is not empty (but don't block)
219+
if hasFiles {
220+
log.Warn("The target directory is not empty. " +
221+
"Scaffolding may overwrite existing files or cause conflicts. " +
222+
"It is recommended to initialize in an empty directory.")
223+
}
224+
213225
return nil
214226
}

0 commit comments

Comments
 (0)