Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update files as part of the Scaffold.Execute #1375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions pkg/model/file/interfaces.go
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)
}
56 changes: 56 additions & 0 deletions pkg/model/file/marker.go
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create an issue to track everything related to TODO(v3)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1382, #1383, and #1384 raised. Could you add them to the corresponding milestone?

// 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
Adirio marked this conversation as resolved.
Show resolved Hide resolved

type CodeFragmentsMap map[Marker]CodeFragments
129 changes: 129 additions & 0 deletions pkg/model/file/mixins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
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"
)

// PathMixin provides file builders with a path field
type PathMixin struct {
// Path is the of the file
Path string
}

// GetPath implements Builder
func (t *PathMixin) GetPath() string {
return t.Path
}

// IfExistsActionMixin provides file builders with a if-exists-action field
type IfExistsActionMixin struct {
// IfExistsAction determines what to do if the file exists
IfExistsAction IfExistsAction
}

// GetIfExistsAction implements Builder
func (t *IfExistsActionMixin) GetIfExistsAction() IfExistsAction {
return t.IfExistsAction
}

// TemplateMixin is the mixin that should be embedded in Template builders
type TemplateMixin struct {
PathMixin
IfExistsActionMixin

// TemplateBody is the template body to execute
TemplateBody string
}

func (t *TemplateMixin) GetBody() string {
return t.TemplateBody
}

// InserterMixin is the mixin that should be embedded in Inserter builders
type InserterMixin struct {
PathMixin
}

// GetIfExistsAction implements Builder
func (t *InserterMixin) GetIfExistsAction() IfExistsAction {
// Inserter builders always need to overwrite previous files
return Overwrite
}

// DomainMixin provides templates with a injectable domain field
type DomainMixin struct {
// Domain is the domain for the APIs
Domain string
}

// InjectDomain implements HasDomain
func (m *DomainMixin) InjectDomain(domain string) {
if m.Domain == "" {
m.Domain = domain
}
}

// RepositoryMixin provides templates with a injectable repository field
type RepositoryMixin struct {
// Repo is the go project package path
Repo string
}

// InjectRepository implements HasRepository
func (m *RepositoryMixin) InjectRepository(repository string) {
if m.Repo == "" {
m.Repo = repository
}
}

// MultiGroupMixin provides templates with a injectable multi-group flag field
type MultiGroupMixin struct {
// MultiGroup is the multi-group flag
MultiGroup bool
}

// InjectMultiGroup implements HasMultiGroup
func (m *MultiGroupMixin) InjectMultiGroup(flag bool) {
m.MultiGroup = flag
}

// BoilerplateMixin provides templates with a injectable boilerplate field
type BoilerplateMixin struct {
// Boilerplate is the contents of a Boilerplate go header file
Boilerplate string
}

// InjectBoilerplate implements HasBoilerplate
func (m *BoilerplateMixin) InjectBoilerplate(boilerplate string) {
if m.Boilerplate == "" {
m.Boilerplate = boilerplate
}
}

// ResourceMixin provides templates with a injectable resource field
type ResourceMixin struct {
Resource *resource.Resource
}

// InjectResource implements HasResource
func (m *ResourceMixin) InjectResource(res *resource.Resource) {
if m.Resource == nil {
m.Resource = res
}
}
104 changes: 0 additions & 104 deletions pkg/model/file/template.go

This file was deleted.

Loading