forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include PROJECT file information into model.Universe
Signed-off-by: Adrian Orive <[email protected]>
- Loading branch information
Showing
10 changed files
with
356 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"sigs.k8s.io/kubebuilder/pkg/scaffold/input" | ||
) | ||
|
||
// File describes a file that will be written | ||
type File struct { | ||
// Path is the file to write | ||
Path string `json:"path,omitempty"` | ||
|
||
// Contents is the generated output | ||
Contents string `json:"contents,omitempty"` | ||
|
||
// TODO: Move input.IfExistsAction into model | ||
// IfExistsAction determines what to do if the file exists | ||
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/gobuffalo/flect" | ||
|
||
internalconfig "sigs.k8s.io/kubebuilder/internal/config" | ||
"sigs.k8s.io/kubebuilder/pkg/model/config" | ||
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource" | ||
"sigs.k8s.io/kubebuilder/pkg/scaffold/util" | ||
) | ||
|
||
// Universe describes the entire state of file generation | ||
type Universe struct { | ||
// Config stores the project configuration | ||
Config *config.Config `json:"config,omitempty"` | ||
|
||
// Boilerplate is the copyright comment added at the top of scaffolded files | ||
Boilerplate string `json:"boilerplate,omitempty"` | ||
|
||
// Resource contains the information of the API that is being scaffolded | ||
Resource *Resource `json:"resource,omitempty"` | ||
|
||
// Files contains the model of the files that are being scaffolded | ||
Files []*File `json:"files,omitempty"` | ||
} | ||
|
||
// NewUniverse creates a new Universe | ||
func NewUniverse(options ...UniverseOption) (*Universe, error) { | ||
universe := &Universe{} | ||
|
||
// Apply options | ||
for _, option := range options { | ||
if err := option(universe); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return universe, nil | ||
} | ||
|
||
// UniverseOption configure Universe | ||
type UniverseOption func(*Universe) error | ||
|
||
// WithConfigFrom loads the project configuration from the provided path | ||
func WithConfigFrom(path string) UniverseOption { | ||
return func(universe *Universe) error { | ||
projectConfig, err := internalconfig.ReadFrom(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
universe.Config = projectConfig | ||
return nil | ||
} | ||
} | ||
|
||
// WithConfig stores the already loaded project configuration | ||
func WithConfig(projectConfig *config.Config) UniverseOption { | ||
return func(universe *Universe) error { | ||
universe.Config = projectConfig | ||
return nil | ||
} | ||
} | ||
|
||
// WithBoilerplateFrom loads the boilerplate from the provided path | ||
func WithBoilerplateFrom(path string) UniverseOption { | ||
return func(universe *Universe) error { | ||
boilerplate, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
universe.Boilerplate = string(boilerplate) | ||
return nil | ||
} | ||
} | ||
|
||
// WithBoilerplate stores the already loaded project configuration | ||
func WithBoilerplate(boilerplate string) UniverseOption { | ||
return func(universe *Universe) error { | ||
universe.Boilerplate = string(boilerplate) | ||
return nil | ||
} | ||
} | ||
|
||
// WithoutBoilerplate is used for files that do not require a boilerplate | ||
func WithoutBoilerplate(universe *Universe) error { | ||
universe.Boilerplate = "-" | ||
return nil | ||
} | ||
|
||
// WithResource stores the provided resource | ||
func WithResource(resource *resource.Resource, project *config.Config) UniverseOption { | ||
return func(universe *Universe) error { | ||
resourceModel := &Resource{ | ||
Namespaced: resource.Namespaced, | ||
Group: resource.Group, | ||
Version: resource.Version, | ||
Kind: resource.Kind, | ||
Resource: resource.Resource, | ||
Plural: flect.Pluralize(strings.ToLower(resource.Kind)), | ||
} | ||
|
||
resourceModel.GoPackage, resourceModel.GroupDomain = util.GetResourceInfo( | ||
resource, | ||
project.Repo, | ||
project.Domain, | ||
project.MultiGroup, | ||
) | ||
|
||
universe.Resource = resourceModel | ||
return nil | ||
} | ||
} |
Oops, something went wrong.