sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/internal/templates/hack/boilerplate.go (about) 1 /* 2 Copyright 2022 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 hack 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "time" 23 24 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 25 ) 26 27 // DefaultBoilerplatePath is the default path to the boilerplate file 28 var DefaultBoilerplatePath = filepath.Join("hack", "boilerplate.go.txt") 29 30 var _ machinery.Template = &Boilerplate{} 31 32 // Boilerplate scaffolds a file that defines the common header for the rest of the files 33 type Boilerplate struct { 34 machinery.TemplateMixin 35 machinery.BoilerplateMixin 36 37 // License is the License type to write 38 License string 39 40 // Licenses maps License types to their actual string 41 Licenses map[string]string 42 43 // Owner is the copyright owner - e.g. "The Kubernetes Authors" 44 Owner string 45 46 // Year is the copyright year 47 Year string 48 } 49 50 // Validate implements file.RequiresValidation 51 func (f Boilerplate) Validate() error { 52 if f.License == "" { 53 // A default license will be set later 54 } else if _, found := knownLicenses[f.License]; found { 55 // One of the know licenses 56 } else if _, found := f.Licenses[f.License]; found { 57 // A map containing the requested license was also provided 58 } else { 59 return fmt.Errorf("unknown specified license %s", f.License) 60 } 61 62 return nil 63 } 64 65 // SetTemplateDefaults implements file.Template 66 func (f *Boilerplate) SetTemplateDefaults() error { 67 if f.Path == "" { 68 f.Path = DefaultBoilerplatePath 69 } 70 71 if f.License == "" { 72 f.License = "apache2" 73 } 74 75 if f.Licenses == nil { 76 f.Licenses = make(map[string]string, len(knownLicenses)) 77 } 78 79 for key, value := range knownLicenses { 80 if _, hasLicense := f.Licenses[key]; !hasLicense { 81 f.Licenses[key] = value 82 } 83 } 84 85 if f.Year == "" { 86 f.Year = fmt.Sprintf("%v", time.Now().Year()) 87 } 88 89 // Boilerplate given 90 if len(f.Boilerplate) > 0 { 91 f.TemplateBody = f.Boilerplate 92 return nil 93 } 94 95 f.TemplateBody = boilerplateTemplate 96 97 return nil 98 } 99 100 const boilerplateTemplate = `/* 101 {{ if .Owner -}} 102 Copyright {{ .Year }} {{ .Owner }}. 103 {{- else -}} 104 Copyright {{ .Year }}. 105 {{- end }} 106 {{ index .Licenses .License }}*/` 107 108 var knownLicenses = map[string]string{ 109 "apache2": apache2, 110 "copyright": "", 111 } 112 113 const apache2 = ` 114 Licensed under the Apache License, Version 2.0 (the "License"); 115 you may not use this file except in compliance with the License. 116 You may obtain a copy of the License at 117 118 http://www.apache.org/licenses/LICENSE-2.0 119 120 Unless required by applicable law or agreed to in writing, software 121 distributed under the License is distributed on an "AS IS" BASIS, 122 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123 See the License for the specific language governing permissions and 124 limitations under the License. 125 `