github.com/jmrodri/operator-sdk@v0.5.0/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  
    65  // Repo allows a repo to be set on an object
    66  type Repo interface {
    67  	// SetRepo sets the repo
    68  	SetRepo(string)
    69  }
    70  
    71  // SetRepo sets the repo
    72  func (i *Input) SetRepo(r string) {
    73  	if i.Repo == "" {
    74  		i.Repo = r
    75  	}
    76  }
    77  
    78  // AbsProjectPath allows the absolute project path to be set on an object
    79  type AbsProjectPath interface {
    80  	// SetAbsProjectPath sets the project file location
    81  	SetAbsProjectPath(string)
    82  }
    83  
    84  // SetAbsProjectPath sets the absolute project path
    85  func (i *Input) SetAbsProjectPath(p string) {
    86  	if i.AbsProjectPath == "" {
    87  		i.AbsProjectPath = p
    88  	}
    89  }
    90  
    91  // ProjectName allows the project name to be set on an object
    92  type ProjectName interface {
    93  	// SetProjectName sets the project name
    94  	SetProjectName(string)
    95  }
    96  
    97  // SetProjectName sets the project name
    98  func (i *Input) SetProjectName(n string) {
    99  	if i.ProjectName == "" {
   100  		i.ProjectName = n
   101  	}
   102  }
   103  
   104  // File is a scaffoldable file
   105  type File interface {
   106  	// GetInput returns the Input for creating a scaffold file
   107  	GetInput() (Input, error)
   108  }
   109  
   110  // Validate validates input
   111  type Validate interface {
   112  	// Validate returns nil if the inputs' validation logic approves of
   113  	// field values, the template, etc.
   114  	Validate() error
   115  }
   116  
   117  // Config configures the execution scaffold templates
   118  type Config struct {
   119  	// Repo is the go project package
   120  	Repo string
   121  
   122  	// AbsProjectPath is the absolute path to the project root, including the project directory.
   123  	AbsProjectPath string
   124  
   125  	// ProjectName is the operator's name, ex. app-operator
   126  	ProjectName string
   127  }