From f513f9ecf23d053ee9b89b3b5e4888d2b6878d2e Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Thu, 22 Oct 2020 11:59:48 -0300 Subject: [PATCH] :sparkles: not allow to init the project in a directory that is not cleaned --- pkg/plugin/v3/init.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/plugin/v3/init.go b/pkg/plugin/v3/init.go index 11adb7ddef5..4a68da2a336 100644 --- a/pkg/plugin/v3/init.go +++ b/pkg/plugin/v3/init.go @@ -18,6 +18,7 @@ package v3 import ( "fmt" + "io/ioutil" "os" "path/filepath" "strings" @@ -111,6 +112,11 @@ func (p *initPlugin) Validate() error { } } + // Check if the current directory has not files which does not allow init the project + if err := checkDir(); err != nil { + return err + } + // Check if the project name is a valid k8s namespace (DNS 1123 label). if p.config.ProjectName == "" { dir, err := os.Getwd() @@ -167,3 +173,24 @@ func (p *initPlugin) PostScaffold() error { fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName) return nil } + +// checkDir will return error if the current directory has files which are +// not the .gitignore and the go.mod. Note that, it is expected that the +// directory to scaffold the project is cleaned. Otherwise, it might face issues +// to do the scaffold. 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. +func checkDir() error { + files, err := ioutil.ReadDir(".") + if err != nil { + return err + } + + for _, file := range files { + if file.Name() != "go.mod" && file.Name() != ".gitignore" { + return fmt.Errorf("this directory has files already. " + + "Only the go.mod and .gitignore are allowed before the init.") + } + } + return nil +}