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
46 changes: 8 additions & 38 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 @@ -159,53 +158,24 @@ func (p *initSubcommand) PostScaffold() error {
}

// checkDir will return error if the current directory has files which are not allowed.
// Note that, it is expected that the directory to scaffold the project is cleaned.
// Otherwise, it might face issues to do the scaffold.
// Only the PROJECT file is disallowed as it will be generated by the scaffolding tool.
func checkDir() error {
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking path %q: %w", path, err)
}
// Allow directory trees starting with '.'
// Skip dot directories
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && info.Name() != "." {
return filepath.SkipDir
}
// Allow files starting with '.'
if strings.HasPrefix(info.Name(), ".") {
return nil
// Only block PROJECT file (reserved for scaffolding)
if !info.IsDir() && info.Name() == "PROJECT" {
return fmt.Errorf("PROJECT file found in target directory. " +
"This file is reserved and will be generated by kubebuilder")
}
// 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" {
return nil
}
disallowedExtensions := []string{
".go",
".yaml",
".mod",
".sum",
}
// Deny files with .go or .yaml or .mod or .sum extensions
for _, ext := range disallowedExtensions {
if strings.HasSuffix(info.Name(), ext) {
return nil
}
}
// 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