github.com/joelanford/operator-sdk@v0.8.2/internal/pkg/scaffold/input/input.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Modified from github.com/kubernetes-sigs/controller-tools/pkg/scaffold/input/input.go
    16  
    17  package input
    18  
    19  import "text/template"
    20  
    21  // IfExistsAction determines what to do if the scaffold file already exists
    22  type IfExistsAction int
    23  
    24  const (
    25  	// Overwrite truncates and overwrites the existing file (default)
    26  	Overwrite IfExistsAction = iota
    27  
    28  	// Error returns an error and stops processing
    29  	Error
    30  
    31  	// Skip skips the file and moves to the next one
    32  	Skip
    33  )
    34  
    35  // Input is the input for scaffoldig a file
    36  type Input struct {
    37  	// Path is the file to write
    38  	Path string
    39  
    40  	// IfExistsAction determines what to do if the file exists
    41  	IfExistsAction IfExistsAction
    42  
    43  	// IsExec indicates whether the file should be written with executable
    44  	// permissions.
    45  	// Defaults to false
    46  	IsExec bool
    47  
    48  	// TemplateBody is the template body to execute
    49  	TemplateBody string
    50  
    51  	// TemplateFuncs are any funcs used in the template. These funcs must be
    52  	// registered before execution.
    53  	TemplateFuncs template.FuncMap
    54  
    55  	// Repo is the go project package
    56  	Repo string
    57  
    58  	// AbsProjectPath is the absolute path to the project root, including the project directory.
    59  	AbsProjectPath string
    60  
    61  	// ProjectName is the operator's name, ex. app-operator
    62  	ProjectName string
    63  
    64  	// Delims is a slice of two strings representing the left and right delimiter
    65  	// defaults to {{ }}
    66  	Delims [2]string
    67  }
    68  
    69  // Repo allows a repo to be set on an object
    70  type Repo interface {
    71  	// SetRepo sets the repo
    72  	SetRepo(string)
    73  }
    74  
    75  // SetRepo sets the repo
    76  func (i *Input) SetRepo(r string) {
    77  	if i.Repo == "" {
    78  		i.Repo = r
    79  	}
    80  }
    81  
    82  // AbsProjectPath allows the absolute project path to be set on an object
    83  type AbsProjectPath interface {
    84  	// SetAbsProjectPath sets the project file location
    85  	SetAbsProjectPath(string)
    86  }
    87  
    88  // SetAbsProjectPath sets the absolute project path
    89  func (i *Input) SetAbsProjectPath(p string) {
    90  	if i.AbsProjectPath == "" {
    91  		i.AbsProjectPath = p
    92  	}
    93  }
    94  
    95  // ProjectName allows the project name to be set on an object
    96  type ProjectName interface {
    97  	// SetProjectName sets the project name
    98  	SetProjectName(string)
    99  }
   100  
   101  // SetProjectName sets the project name
   102  func (i *Input) SetProjectName(n string) {
   103  	if i.ProjectName == "" {
   104  		i.ProjectName = n
   105  	}
   106  }
   107  
   108  // File is a scaffoldable file
   109  type File interface {
   110  	// GetInput returns the Input for creating a scaffold file
   111  	GetInput() (Input, error)
   112  }
   113  
   114  // Validate validates input
   115  type Validate interface {
   116  	// Validate returns nil if the inputs' validation logic approves of
   117  	// field values, the template, etc.
   118  	Validate() error
   119  }
   120  
   121  // Config configures the execution scaffold templates
   122  type Config struct {
   123  	// Repo is the go project package
   124  	Repo string
   125  
   126  	// AbsProjectPath is the absolute path to the project root, including the project directory.
   127  	AbsProjectPath string
   128  
   129  	// ProjectName is the operator's name, ex. app-operator
   130  	ProjectName string
   131  }