From e1e9897c44e9c2a943888ea74b25e3ed55b1f95d Mon Sep 17 00:00:00 2001 From: prafull01 Date: Fri, 16 Oct 2020 08:08:34 +0530 Subject: [PATCH] :bug: Add validation for the main.go file present in root directory --- pkg/plugin/v2/api.go | 8 ++++++++ pkg/plugin/v3/api.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/plugin/v2/api.go b/pkg/plugin/v2/api.go index fed17977572..4bc778c0133 100644 --- a/pkg/plugin/v2/api.go +++ b/pkg/plugin/v2/api.go @@ -38,6 +38,9 @@ import ( "sigs.k8s.io/kubebuilder/plugins/addon" ) +// DefaultMainPath is default file path of main.go +const DefaultMainPath = "main.go" + type createAPIPlugin struct { config *config.Config @@ -131,6 +134,11 @@ func (p *createAPIPlugin) Validate() error { return err } + // check if main.go is present in the root directory + if _, err := os.Stat(DefaultMainPath); os.IsNotExist(err) { + return fmt.Errorf("%s file should present in the root directory", DefaultMainPath) + } + reader := bufio.NewReader(os.Stdin) if !p.resourceFlag.Changed { fmt.Println("Create Resource [y/n]") diff --git a/pkg/plugin/v3/api.go b/pkg/plugin/v3/api.go index e80e73921ee..879992443be 100644 --- a/pkg/plugin/v3/api.go +++ b/pkg/plugin/v3/api.go @@ -43,6 +43,9 @@ import ( // TODO: remove this when a better solution for using addons is implemented. const KbDeclarativePatternVersion = "v0.0.0-20200522144838-848d48e5b073" +// DefaultMainPath is default file path of main.go +const DefaultMainPath = "main.go" + type createAPIPlugin struct { config *config.Config @@ -141,6 +144,11 @@ func (p *createAPIPlugin) Validate() error { return fmt.Errorf("can not have group and domain both empty") } + // check if main.go is present in the root directory + if _, err := os.Stat(DefaultMainPath); os.IsNotExist(err) { + return fmt.Errorf("%s file should present in the root directory", DefaultMainPath) + } + // TODO: re-evaluate whether y/n input still makes sense. We should probably always // scaffold the resource and controller. reader := bufio.NewReader(os.Stdin)