From 1f70e53cda5756d546419871e26915e6a6f764b5 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 8 May 2019 10:09:33 -0700 Subject: [PATCH] cmd/operator-sdk/add/api.go: check if go file exists in pkg/apis/ before scaffolding stub --- cmd/operator-sdk/add/api.go | 37 +++++++++++++++++++++++++++++++++-- internal/pkg/scaffold/stub.go | 2 ++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cmd/operator-sdk/add/api.go b/cmd/operator-sdk/add/api.go index 47f20310f5b..692a9f30072 100644 --- a/cmd/operator-sdk/add/api.go +++ b/cmd/operator-sdk/add/api.go @@ -16,11 +16,15 @@ package add import ( "fmt" + "io/ioutil" + "os" + "path/filepath" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil" "github.com/operator-framework/operator-sdk/internal/pkg/scaffold" "github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" "github.com/operator-framework/operator-sdk/internal/util/projutil" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -102,10 +106,16 @@ func apiRun(cmd *cobra.Command, args []string) error { Repo: projutil.CheckAndGetProjectGoPkg(), AbsProjectPath: absProjectPath, } - s := &scaffold.Scaffold{} + + // Check if any package files for this API group dir exist, and if not + // scaffold a stub.go to prevent erroneous gengo parse errors. + stub := &scaffold.Stub{Resource: r} + if err := scaffoldIfNoPkgFileExists(s, cfg, stub); err != nil { + return errors.Wrap(err, "scaffold stub file") + } + err = s.Execute(cfg, - &scaffold.Stub{Resource: r}, &scaffold.Types{Resource: r}, &scaffold.AddToScheme{Resource: r}, &scaffold.Register{Resource: r}, @@ -135,3 +145,26 @@ func apiRun(cmd *cobra.Command, args []string) error { log.Info("API generation complete.") return nil } + +// scaffoldIfNoPkgFileExists executes f using s and cfg if no go files +// in f's directory exist. +func scaffoldIfNoPkgFileExists(s *scaffold.Scaffold, cfg *input.Config, f input.File) error { + i, err := f.GetInput() + if err != nil { + return errors.Wrapf(err, "error getting file %s input", i.Path) + } + groupDir := filepath.Dir(i.Path) + gdInfos, err := ioutil.ReadDir(groupDir) + if err != nil && !os.IsNotExist(err) { + return errors.Wrapf(err, "error reading dir %s", groupDir) + } + if err == nil { + for _, info := range gdInfos { + if !info.IsDir() && filepath.Ext(info.Name()) == ".go" { + return nil + } + } + } + // err must be a non-existence error or no go files exist, so execute f. + return s.Execute(cfg, f) +} diff --git a/internal/pkg/scaffold/stub.go b/internal/pkg/scaffold/stub.go index ee2a7b63d96..5bf47d876ea 100644 --- a/internal/pkg/scaffold/stub.go +++ b/internal/pkg/scaffold/stub.go @@ -28,6 +28,8 @@ type Stub struct { Resource *Resource } +var _ input.File = &Stub{} + func (s *Stub) GetInput() (input.Input, error) { if s.Path == "" { s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, StubFile)