Skip to content

Commit 13f50c3

Browse files
committed
🐛 help/version command fails when default repo is not initialized
This change: - kills global variables domain and repo - initialized repo to commands where needed
1 parent c5e9e22 commit 13f50c3

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

cmd/init_project.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,19 @@ type projectOptions struct {
7373
skipGoVersionCheck bool
7474

7575
boilerplate project.Boilerplate
76-
project project.Project
76+
project project.Project
7777

7878
// deprecated flags
79-
dep bool
80-
depFlag *flag.Flag
81-
depArgs []string
79+
dep bool
80+
depFlag *flag.Flag
81+
depArgs []string
8282

8383
// final result
8484
scaffolder scaffold.ProjectScaffolder
8585
}
8686

8787
func (o *projectOptions) bindCmdlineFlags(cmd *cobra.Command) {
88+
8889
cmd.Flags().BoolVar(&o.skipGoVersionCheck, "skip-go-version-check", false, "if specified, skip checking the Go version")
8990

9091
// dependency args
@@ -103,9 +104,9 @@ func (o *projectOptions) bindCmdlineFlags(cmd *cobra.Command) {
103104
cmd.Flags().StringVar(&o.boilerplate.Owner, "owner", "", "Owner to add to the copyright")
104105

105106
// project args
106-
cmd.Flags().StringVar(&o.project.Repo, "repo", util.Repo, "name of the github repo. "+
107+
cmd.Flags().StringVar(&o.project.Repo, "repo", "", "name of the github repo. "+
107108
"defaults to the go package of the current working directory.")
108-
cmd.Flags().StringVar(&o.project.Domain, "domain", "k8s.io", "domain for groups")
109+
cmd.Flags().StringVar(&o.project.Domain, "domain", "my.domain", "domain for groups")
109110
cmd.Flags().StringVar(&o.project.Version, "project-version", project.Version2, "project version")
110111
}
111112

@@ -132,6 +133,13 @@ func (o *projectOptions) validate() error {
132133
return err
133134
}
134135
}
136+
if o.project.Repo == "" {
137+
repoPath, err := findCurrentRepo()
138+
if err != nil {
139+
return fmt.Errorf("error finding current repository: %v", err)
140+
}
141+
o.project.Repo = repoPath
142+
}
135143

136144
switch o.project.Version {
137145
case project.Version1:
@@ -140,15 +148,15 @@ func (o *projectOptions) validate() error {
140148
defEnsure = &o.dep
141149
}
142150
o.scaffolder = &scaffold.V1Project{
143-
Project: o.project,
151+
Project: o.project,
144152
Boilerplate: o.boilerplate,
145153

146-
DepArgs: o.depArgs,
154+
DepArgs: o.depArgs,
147155
DefinitelyEnsure: defEnsure,
148156
}
149157
case project.Version2:
150158
o.scaffolder = &scaffold.V2Project{
151-
Project: o.project,
159+
Project: o.project,
152160
Boilerplate: o.boilerplate,
153161
}
154162
default:

cmd/main.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/spf13/cobra"
2727
"golang.org/x/tools/go/packages"
2828

29-
"sigs.k8s.io/kubebuilder/cmd/util"
3029
"sigs.k8s.io/kubebuilder/cmd/version"
3130
"sigs.k8s.io/kubebuilder/pkg/scaffold"
3231
)
@@ -104,13 +103,6 @@ func findCurrentRepo() (string, error) {
104103
}
105104

106105
func main() {
107-
repoPath, err := findCurrentRepo()
108-
if err != nil {
109-
log.Fatal(fmt.Errorf("error finding current repository: %v", err))
110-
}
111-
112-
util.Repo = repoPath
113-
114106
rootCmd := defaultCommand()
115107

116108
rootCmd.AddCommand(
@@ -146,7 +138,7 @@ Typical project lifecycle:
146138
147139
- initialize a project:
148140
149-
kubebuilder init --domain k8s.io --license apache2 --owner "The Kubernetes authors"
141+
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"
150142
151143
- create one or more a new resource APIs and add your code to them:
152144

cmd/util/util.go

-18
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28-
"regexp"
2928
"strings"
3029
"text/template"
3130

3231
"github.com/gobuffalo/flect"
3332
)
3433

35-
var Domain string
36-
var Repo string
37-
3834
// writeIfNotFound returns true if the file was created and false if it already exists
3935
func WriteIfNotFound(path, templateName, templateValue string, data interface{}) bool {
4036
// Make sure the directory exists
@@ -108,20 +104,6 @@ func GetCopyright(file string) string {
108104
return string(cr)
109105
}
110106

111-
func GetDomain() string {
112-
b, err := ioutil.ReadFile(filepath.Join("pkg", "apis", "doc.go"))
113-
if err != nil {
114-
log.Fatalf("Could not find pkg/apis/doc.go. First run `kubebuilder init --domain <domain>`.")
115-
}
116-
r := regexp.MustCompile("\\+domain=(.*)")
117-
l := r.FindSubmatch(b)
118-
if len(l) < 2 {
119-
log.Fatalf("pkg/apis/doc.go does not contain the domain (// +domain=.*)")
120-
}
121-
Domain = string(l[1])
122-
return Domain
123-
}
124-
125107
func create(path string) {
126108
f, err := os.Create(path)
127109
if err != nil {

0 commit comments

Comments
 (0)