-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Template interface (phase 2) including insert code fragments logic
Signed-off-by: Adrian Orive <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,457 additions
and
814 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2018 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 file | ||
|
||
import ( | ||
"sigs.k8s.io/kubebuilder/pkg/model/resource" | ||
) | ||
|
||
// Builder defines the basic methods that any file builder must implement | ||
type Builder interface { | ||
// GetPath returns the path to the file location | ||
GetPath() string | ||
// GetIfExistsAction returns the behavior when creating a file that already exists | ||
GetIfExistsAction() IfExistsAction | ||
} | ||
|
||
// RequiresValidation is a file builder that requires validation | ||
type RequiresValidation interface { | ||
Builder | ||
// Validate returns true if the template has valid values | ||
Validate() error | ||
} | ||
|
||
// Template is file builder based on a file template | ||
type Template interface { | ||
Builder | ||
// GetBody returns the template body | ||
GetBody() string | ||
// SetTemplateDefaults returns the TemplateMixin for creating a scaffold file | ||
SetTemplateDefaults() error | ||
} | ||
|
||
// Inserter is a file builder that inserts code fragments in marked positions | ||
type Inserter interface { | ||
Builder | ||
// GetMarkers returns the different markers where code fragments will be inserted | ||
GetMarkers() []Marker | ||
// GetCodeFragments returns a map that binds markers to code fragments | ||
GetCodeFragments() CodeFragmentsMap | ||
} | ||
|
||
// HasDomain allows the domain to be used on a template | ||
type HasDomain interface { | ||
// InjectDomain sets the template domain | ||
InjectDomain(string) | ||
} | ||
|
||
// HasRepository allows the repository to be used on a template | ||
type HasRepository interface { | ||
// InjectRepository sets the template repository | ||
InjectRepository(string) | ||
} | ||
|
||
// HasMultiGroup allows the multi-group flag to be used on a template | ||
type HasMultiGroup interface { | ||
// InjectMultiGroup sets the template multi-group flag | ||
InjectMultiGroup(bool) | ||
} | ||
|
||
// HasBoilerplate allows a boilerplate to be used on a template | ||
type HasBoilerplate interface { | ||
// InjectBoilerplate sets the template boilerplate | ||
InjectBoilerplate(string) | ||
} | ||
|
||
// HasResource allows a resource to be used on a template | ||
type HasResource interface { | ||
// InjectResource sets the template resource | ||
InjectResource(*resource.Resource) | ||
} |
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,56 @@ | ||
/* | ||
Copyright 2019 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 file | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
) | ||
|
||
const prefix = "+kubebuilder:scaffold:" | ||
|
||
var commentsByExt = map[string]string{ | ||
// TODO(v3): machine-readable comments should not have spaces by Go convention. However, | ||
// this is a backwards incompatible change, and thus should be done for next project version. | ||
".go": "// ", | ||
".yaml": "# ", | ||
} | ||
|
||
// Marker represents a comment LoC that will be used to insert code fragments by update operations | ||
type Marker struct { | ||
comment string | ||
value string | ||
} | ||
|
||
// NewMarkerFor creates a new marker customized for the specific file | ||
func NewMarkerFor(path string, value string) Marker { | ||
ext := filepath.Ext(path) | ||
if comment, found := commentsByExt[ext]; found { | ||
return Marker{comment, value} | ||
} | ||
|
||
panic(fmt.Errorf("unknown file extension: '%s', expected '.go' or '.yaml'", ext)) | ||
} | ||
|
||
// String implements Stringer | ||
func (m Marker) String() string { | ||
return m.comment + prefix + m.value | ||
} | ||
|
||
type CodeFragments []string | ||
|
||
type CodeFragmentsMap map[Marker]CodeFragments |
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
Oops, something went wrong.