|
| 1 | +/* |
| 2 | +Copyright 2020 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 model |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/gobuffalo/flect" |
| 24 | + |
| 25 | + internalconfig "sigs.k8s.io/kubebuilder/internal/config" |
| 26 | + "sigs.k8s.io/kubebuilder/pkg/model/config" |
| 27 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/resource" |
| 28 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/util" |
| 29 | +) |
| 30 | + |
| 31 | +// Universe describes the entire state of file generation |
| 32 | +type Universe struct { |
| 33 | + // Config stores the project configuration |
| 34 | + Config *config.Config `json:"config,omitempty"` |
| 35 | + |
| 36 | + // Boilerplate is the copyright comment added at the top of scaffolded files |
| 37 | + Boilerplate string `json:"boilerplate,omitempty"` |
| 38 | + |
| 39 | + // Resource contains the information of the API that is being scaffolded |
| 40 | + Resource *Resource `json:"resource,omitempty"` |
| 41 | + |
| 42 | + // Files contains the model of the files that are being scaffolded |
| 43 | + Files []*File `json:"files,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +// NewUniverse creates a new Universe |
| 47 | +func NewUniverse(options ...UniverseOption) (*Universe, error) { |
| 48 | + universe := &Universe{} |
| 49 | + |
| 50 | + // Apply options |
| 51 | + for _, option := range options { |
| 52 | + if err := option(universe); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return universe, nil |
| 58 | +} |
| 59 | + |
| 60 | +// UniverseOption configure Universe |
| 61 | +type UniverseOption func(*Universe) error |
| 62 | + |
| 63 | +// WithConfigFrom loads the project configuration from the provided path |
| 64 | +func WithConfigFrom(path string) UniverseOption { |
| 65 | + return func(universe *Universe) error { |
| 66 | + projectConfig, err := internalconfig.ReadFrom(path) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + universe.Config = projectConfig |
| 72 | + return nil |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// WithConfig stores the already loaded project configuration |
| 77 | +func WithConfig(projectConfig *config.Config) UniverseOption { |
| 78 | + return func(universe *Universe) error { |
| 79 | + universe.Config = projectConfig |
| 80 | + return nil |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// WithBoilerplateFrom loads the boilerplate from the provided path |
| 85 | +func WithBoilerplateFrom(path string) UniverseOption { |
| 86 | + return func(universe *Universe) error { |
| 87 | + boilerplate, err := ioutil.ReadFile(path) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + universe.Boilerplate = string(boilerplate) |
| 93 | + return nil |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// WithBoilerplate stores the already loaded project configuration |
| 98 | +func WithBoilerplate(boilerplate string) UniverseOption { |
| 99 | + return func(universe *Universe) error { |
| 100 | + universe.Boilerplate = string(boilerplate) |
| 101 | + return nil |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// WithoutBoilerplate is used for files that do not require a boilerplate |
| 106 | +func WithoutBoilerplate(universe *Universe) error { |
| 107 | + universe.Boilerplate = "-" |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// WithResource stores the provided resource |
| 112 | +func WithResource(resource *resource.Resource, project *config.Config) UniverseOption { |
| 113 | + return func(universe *Universe) error { |
| 114 | + resourceModel := &Resource{ |
| 115 | + Namespaced: resource.Namespaced, |
| 116 | + Group: resource.Group, |
| 117 | + Version: resource.Version, |
| 118 | + Kind: resource.Kind, |
| 119 | + Resource: resource.Resource, |
| 120 | + Plural: flect.Pluralize(strings.ToLower(resource.Kind)), |
| 121 | + } |
| 122 | + |
| 123 | + resourceModel.GoPackage, resourceModel.GroupDomain = util.GetResourceInfo( |
| 124 | + resource, |
| 125 | + project.Repo, |
| 126 | + project.Domain, |
| 127 | + project.MultiGroup, |
| 128 | + ) |
| 129 | + |
| 130 | + universe.Resource = resourceModel |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
0 commit comments