Skip to content

Commit e348157

Browse files
authored
Merge pull request #1325 from Adirio/scaffold-enhancement/config
Move configuration file related types and functions to separate package
2 parents 00d5551 + 9d7c064 commit e348157

File tree

22 files changed

+484
-295
lines changed

22 files changed

+484
-295
lines changed

cmd/api.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/spf13/cobra"
2828
flag "github.com/spf13/pflag"
2929

30+
"sigs.k8s.io/kubebuilder/cmd/internal"
3031
"sigs.k8s.io/kubebuilder/cmd/util"
3132
"sigs.k8s.io/kubebuilder/pkg/scaffold"
3233
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
@@ -76,7 +77,7 @@ func resourceForFlags(f *flag.FlagSet) *resource.Resource {
7677

7778
// APICmd represents the resource command
7879
func (o *apiOptions) runAddAPI() {
79-
dieIfNoProject()
80+
internal.DieIfNotConfigured()
8081

8182
switch strings.ToLower(o.pattern) {
8283
case "":
@@ -96,12 +97,12 @@ func (o *apiOptions) runAddAPI() {
9697
reader := bufio.NewReader(os.Stdin)
9798
if !o.resourceFlag.Changed {
9899
fmt.Println("Create Resource [y/n]")
99-
o.apiScaffolder.DoResource = util.Yesno(reader)
100+
o.apiScaffolder.DoResource = util.YesNo(reader)
100101
}
101102

102103
if !o.controllerFlag.Changed {
103104
fmt.Println("Create Controller [y/n]")
104-
o.apiScaffolder.DoController = util.Yesno(reader)
105+
o.apiScaffolder.DoController = util.YesNo(reader)
105106
}
106107

107108
fmt.Println("Writing scaffold for you to edit...")
@@ -171,10 +172,3 @@ After the scaffold is written, api will run make on the project.
171172

172173
return apiCmd
173174
}
174-
175-
// dieIfNoProject checks to make sure the command is run from a directory containing a project file.
176-
func dieIfNoProject() {
177-
if _, err := os.Stat("PROJECT"); os.IsNotExist(err) {
178-
log.Fatalf("Command must be run from a directory containing %s", "PROJECT")
179-
}
180-
}

cmd/create.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121

2222
"github.com/spf13/cobra"
23+
24+
"sigs.k8s.io/kubebuilder/cmd/internal"
2325
)
2426

2527
func newCreateCmd() *cobra.Command {
@@ -35,11 +37,7 @@ func newCreateCmd() *cobra.Command {
3537
newAPICommand(),
3638
)
3739

38-
foundProject, version := getProjectVersion()
39-
// It add webhook v2 command in the following 2 cases:
40-
// - There are no PROJECT file found.
41-
// - version == 2 is found in the PROJECT file.
42-
if !foundProject || version == "2" {
40+
if !internal.ConfiguredAndV1() {
4341
cmd.AddCommand(
4442
newWebhookV2Cmd(),
4543
)

cmd/edit.go

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"fmt"
21-
"io/ioutil"
2220
"log"
2321

2422
"github.com/spf13/cobra"
2523

26-
"sigs.k8s.io/kubebuilder/pkg/scaffold"
27-
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
28-
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
29-
"sigs.k8s.io/yaml"
24+
"sigs.k8s.io/kubebuilder/cmd/internal"
25+
"sigs.k8s.io/kubebuilder/internal/config"
3026
)
3127

3228
func newEditProjectCmd() *cobra.Command {
@@ -35,31 +31,33 @@ func newEditProjectCmd() *cobra.Command {
3531

3632
editProjectCmd := &cobra.Command{
3733
Use: "edit",
38-
Short: "This command will edit the configuration of the PROJECT file",
39-
Long: `This command will edit the configuration of the PROJECT file`,
34+
Short: "This command will edit the project configuration",
35+
Long: `This command will edit the project configuration`,
4036
Example: `
4137
# To enable the multigroup layout/support
4238
kubebuilder edit --multigroup
4339
4440
# To disable the multigroup layout/support
4541
kubebuilder edit --multigroup=false`,
4642
Run: func(cmd *cobra.Command, args []string) {
47-
dieIfNoProject()
43+
internal.DieIfNotConfigured()
4844

49-
projectInfo, err := scaffold.LoadProjectFile("PROJECT")
45+
projectConfig, err := config.Load()
5046
if err != nil {
51-
log.Fatalf("failed to read the PROJECT file: %v", err)
47+
log.Fatalf("failed to read the configuration file: %v", err)
5248
}
5349

54-
if projectInfo.Version != project.Version2 {
55-
log.Fatalf("kubebuilder multigroup is for project version: 2,"+
56-
" the version of this project is: %s \n", projectInfo.Version)
57-
}
50+
if opts.multigroup {
51+
if !projectConfig.IsV2() {
52+
log.Fatalf("kubebuilder multigroup is for project version: 2,"+
53+
" the version of this project is: %s \n", projectConfig.Version)
54+
}
5855

59-
// Set MultiGroup Option
60-
projectInfo.MultiGroup = opts.multigroup
56+
// Set MultiGroup Option
57+
projectConfig.MultiGroup = true
58+
}
6159

62-
err = saveProjectFile("PROJECT", &projectInfo)
60+
err = projectConfig.Save()
6361
if err != nil {
6462
log.Fatalf("error updating project file with resource information : %v", err)
6563
}
@@ -72,19 +70,6 @@ func newEditProjectCmd() *cobra.Command {
7270
return editProjectCmd
7371
}
7472

75-
// saveProjectFile saves the given ProjectFile at the given path.
76-
func saveProjectFile(path string, project *input.ProjectFile) error {
77-
content, err := yaml.Marshal(project)
78-
if err != nil {
79-
return fmt.Errorf("error marshalling project info %v", err)
80-
}
81-
err = ioutil.WriteFile(path, content, 0666)
82-
if err != nil {
83-
return fmt.Errorf("failed to save project file at %s %v", path, err)
84-
}
85-
return nil
86-
}
87-
8873
type editProjectCmdOptions struct {
8974
multigroup bool
9075
}

cmd/init_project.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929
"github.com/spf13/cobra"
3030
flag "github.com/spf13/pflag"
3131

32+
"sigs.k8s.io/kubebuilder/cmd/internal"
3233
"sigs.k8s.io/kubebuilder/cmd/util"
34+
"sigs.k8s.io/kubebuilder/pkg/model/config"
3335
"sigs.k8s.io/kubebuilder/pkg/scaffold"
3436
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
3537
)
@@ -44,7 +46,7 @@ func newInitProjectCmd() *cobra.Command {
4446
4547
Writes the following files:
4648
- a boilerplate license file
47-
- a PROJECT file with the domain and repo
49+
- a PROJECT file with the project configuration
4850
- a Makefile to build the project
4951
- a go.mod with project dependencies
5052
- a Kustomization.yaml for customizating manifests
@@ -117,15 +119,17 @@ func (o *projectOptions) bindCmdlineFlags(cmd *cobra.Command) {
117119
"name to use for go module, e.g. github.com/user/repo. "+
118120
"defaults to the go package of the current working directory.")
119121
cmd.Flags().StringVar(&o.project.Domain, "domain", "my.domain", "domain for groups")
120-
cmd.Flags().StringVar(&o.project.Version, "project-version", project.Version2, "project version")
122+
cmd.Flags().StringVar(&o.project.Version, "project-version", config.Version2, "project version")
121123
}
122124

123125
func (o *projectOptions) initializeProject() {
126+
internal.DieIfConfigured()
127+
124128
if err := o.validate(); err != nil {
125129
log.Fatal(err)
126130
}
127131

128-
if o.project.Version == project.Version1 {
132+
if o.project.IsV1() {
129133
printV1DeprecationWarning()
130134
}
131135

@@ -169,8 +173,8 @@ func (o *projectOptions) validate() error {
169173
o.project.Repo = repoPath
170174
}
171175

172-
switch o.project.Version {
173-
case project.Version1:
176+
switch {
177+
case o.project.IsV1():
174178
var defEnsure *bool
175179
if o.depFlag.Changed {
176180
defEnsure = &o.dep
@@ -182,7 +186,7 @@ func (o *projectOptions) validate() error {
182186
DepArgs: o.depArgs,
183187
DefinitelyEnsure: defEnsure,
184188
}
185-
case project.Version2:
189+
case o.project.IsV2():
186190
o.scaffolder = &scaffold.V2Project{
187191
Project: o.project,
188192
Boilerplate: o.boilerplate,
@@ -195,10 +199,6 @@ func (o *projectOptions) validate() error {
195199
return err
196200
}
197201

198-
if util.ProjectExist() {
199-
return fmt.Errorf("failed to initialize project because project is already initialized")
200-
}
201-
202202
return nil
203203
}
204204

cmd/internal/config.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package internal
18+
19+
import (
20+
"log"
21+
22+
"sigs.k8s.io/kubebuilder/internal/config"
23+
)
24+
25+
// isProjectConfigured checks for the existence of the configuration file
26+
func isProjectConfigured() bool {
27+
exists, err := config.Exists()
28+
if err != nil {
29+
log.Fatalf("Unable to check if configuration file exists: %v", err)
30+
}
31+
32+
return exists
33+
}
34+
35+
// DieIfConfigured exists if a configuration file was found
36+
func DieIfConfigured() {
37+
if isProjectConfigured() {
38+
log.Fatalf("Project is already initialized")
39+
}
40+
}
41+
42+
// DieIfNotConfigured exists if no configuration file was found
43+
func DieIfNotConfigured() {
44+
if !isProjectConfigured() {
45+
log.Fatalf("Command must be run after `kubebuilder init ...`")
46+
}
47+
}
48+
49+
// ConfiguredAndV1 returns true if the project is already configured and it is v1
50+
func ConfiguredAndV1() bool {
51+
if !isProjectConfigured() {
52+
return false
53+
}
54+
55+
projectConfig, err := config.Read()
56+
if err != nil {
57+
log.Fatalf("failed to read the configuration file: %v", err)
58+
}
59+
60+
return projectConfig.IsV1()
61+
}

cmd/main.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import (
2626
"github.com/spf13/cobra"
2727
"golang.org/x/tools/go/packages"
2828

29+
"sigs.k8s.io/kubebuilder/cmd/internal"
2930
"sigs.k8s.io/kubebuilder/cmd/version"
30-
"sigs.k8s.io/kubebuilder/pkg/scaffold"
31-
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
3231
)
3332

3433
const (
@@ -67,13 +66,7 @@ func findGoModulePath(forceModules bool) (string, error) {
6766
// findCurrentRepo attempts to determine the current repository
6867
// though a combination of go/packages and `go mod` commands/tricks.
6968
func findCurrentRepo() (string, error) {
70-
// easiest case: project file already exists
71-
projFile, err := scaffold.LoadProjectFile("PROJECT")
72-
if err == nil {
73-
return projFile.Repo, nil
74-
}
75-
76-
// next easy case: existing go module
69+
// easiest case: existing go module
7770
path, err := findGoModulePath(false)
7871
if err == nil {
7972
return path, nil
@@ -118,8 +111,7 @@ func main() {
118111
version.NewVersionCmd(),
119112
)
120113

121-
foundProject, projectVersion := getProjectVersion()
122-
if foundProject && projectVersion == project.Version1 {
114+
if internal.ConfiguredAndV1() {
123115
printV1DeprecationWarning()
124116

125117
rootCmd.AddCommand(
@@ -187,19 +179,6 @@ After the scaffold is written, api will run make on the project.
187179
}
188180
}
189181

190-
// getProjectVersion tries to load PROJECT file and returns if the file exist
191-
// and the version string
192-
func getProjectVersion() (bool, string) {
193-
if _, err := os.Stat("PROJECT"); os.IsNotExist(err) {
194-
return false, ""
195-
}
196-
projectInfo, err := scaffold.LoadProjectFile("PROJECT")
197-
if err != nil {
198-
log.Fatalf("failed to read the PROJECT file: %v", err)
199-
}
200-
return true, projectInfo.Version
201-
}
202-
203182
func printV1DeprecationWarning() {
204183
fmt.Printf(NoticeColor, "[Deprecation Notice] The v1 projects are deprecated and will not be supported "+
205184
"beyond Feb 1, 2020.\nSee how to upgrade your project to v2:"+

cmd/util/stdin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"strings"
2424
)
2525

26-
// Yesno reads from stdin looking for one of "y", "yes", "n", "no" and returns
26+
// YesNo reads from stdin looking for one of "y", "yes", "n", "no" and returns
2727
// true for "y" and false for "n"
28-
func Yesno(reader *bufio.Reader) bool {
28+
func YesNo(reader *bufio.Reader) bool {
2929
for {
3030
text := readstdin(reader)
3131
switch text {

cmd/util/util.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)