Skip to content

Commit 1f70e53

Browse files
committed
cmd/operator-sdk/add/api.go: check if go file exists in pkg/apis/<group> before scaffolding stub
1 parent 5695c8e commit 1f70e53

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cmd/operator-sdk/add/api.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ package add
1616

1717
import (
1818
"fmt"
19+
"io/ioutil"
20+
"os"
21+
"path/filepath"
1922

2023
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil"
2124
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
2225
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
2326
"github.com/operator-framework/operator-sdk/internal/util/projutil"
27+
"github.com/pkg/errors"
2428

2529
log "github.com/sirupsen/logrus"
2630
"github.com/spf13/cobra"
@@ -102,10 +106,16 @@ func apiRun(cmd *cobra.Command, args []string) error {
102106
Repo: projutil.CheckAndGetProjectGoPkg(),
103107
AbsProjectPath: absProjectPath,
104108
}
105-
106109
s := &scaffold.Scaffold{}
110+
111+
// Check if any package files for this API group dir exist, and if not
112+
// scaffold a stub.go to prevent erroneous gengo parse errors.
113+
stub := &scaffold.Stub{Resource: r}
114+
if err := scaffoldIfNoPkgFileExists(s, cfg, stub); err != nil {
115+
return errors.Wrap(err, "scaffold stub file")
116+
}
117+
107118
err = s.Execute(cfg,
108-
&scaffold.Stub{Resource: r},
109119
&scaffold.Types{Resource: r},
110120
&scaffold.AddToScheme{Resource: r},
111121
&scaffold.Register{Resource: r},
@@ -135,3 +145,26 @@ func apiRun(cmd *cobra.Command, args []string) error {
135145
log.Info("API generation complete.")
136146
return nil
137147
}
148+
149+
// scaffoldIfNoPkgFileExists executes f using s and cfg if no go files
150+
// in f's directory exist.
151+
func scaffoldIfNoPkgFileExists(s *scaffold.Scaffold, cfg *input.Config, f input.File) error {
152+
i, err := f.GetInput()
153+
if err != nil {
154+
return errors.Wrapf(err, "error getting file %s input", i.Path)
155+
}
156+
groupDir := filepath.Dir(i.Path)
157+
gdInfos, err := ioutil.ReadDir(groupDir)
158+
if err != nil && !os.IsNotExist(err) {
159+
return errors.Wrapf(err, "error reading dir %s", groupDir)
160+
}
161+
if err == nil {
162+
for _, info := range gdInfos {
163+
if !info.IsDir() && filepath.Ext(info.Name()) == ".go" {
164+
return nil
165+
}
166+
}
167+
}
168+
// err must be a non-existence error or no go files exist, so execute f.
169+
return s.Execute(cfg, f)
170+
}

internal/pkg/scaffold/stub.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Stub struct {
2828
Resource *Resource
2929
}
3030

31+
var _ input.File = &Stub{}
32+
3133
func (s *Stub) GetInput() (input.Input, error) {
3234
if s.Path == "" {
3335
s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, StubFile)

0 commit comments

Comments
 (0)