Skip to content

Commit

Permalink
cmd/operator-sdk/add/api.go: check if go file exists in pkg/apis/<gro…
Browse files Browse the repository at this point in the history
…up> before scaffolding stub
  • Loading branch information
estroz committed May 8, 2019
1 parent 5695c8e commit 1f70e53
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 35 additions & 2 deletions cmd/operator-sdk/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions internal/pkg/scaffold/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1f70e53

Please sign in to comment.