Skip to content

Commit

Permalink
Setup a default boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent eb6748c commit 67bcbb0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
17 changes: 17 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/codegen/helpers/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package helpers

import (
"errors"
"k8s.io/code-generator/pkg/current"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -62,6 +63,13 @@ func (a *Args) Validate() error {

}
a.root = root
if len(a.Boilerplate) == 0 {
if b, err := defaultBoilerplate(); err != nil {
return err
} else {
a.Boilerplate = b
}
}
if len(a.Boilerplate) > 0 && !isReadable(a.Boilerplate) {
return ErrBoilerplateIsntReadable
}
Expand All @@ -73,6 +81,15 @@ func (a *Args) Validate() error {
return nil
}

func defaultBoilerplate() (string, error) {
dir, err := current.Dir()
if err != nil {
return "", err
}
codegenRoot := path.Dir(path.Dir(dir))
return path.Join(codegenRoot, "examples", "hack", "boilerplate.go.txt"), nil
}

func isReadable(path string) bool {
_, err := os.Stat(path)
return err == nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ func generateDeepCopyHelpers(args *Args, pkgs []string) error {
dcargs := []string{
"-O", "zz_generated.deepcopy",
"--output-base", args.OutputBase,
}
if args.Boilerplate != "" {
dcargs = append(dcargs, "--go-header-file", args.Boilerplate)
"--go-header-file", args.Boilerplate,
}
for _, pkg := range pkgs {
dcargs = append(dcargs, "--input-dirs", pkg)
Expand Down
28 changes: 28 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/current/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2023 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 current

import "path/filepath"

// Dir is the __dirname equivalent.
func Dir() (string, error) {
filename, err := File()
if err != nil {
return "", err
}
return filepath.Dir(filename), nil
}
33 changes: 33 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/current/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2023 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 current

import (
"errors"
"runtime"
)

var ErrCantGetCurrentFilename = errors.New("unable to get the current filename")

// File is the __filename equivalent
func File() (string, error) {
_, filename, _, ok := runtime.Caller(1)
if !ok {
return "", ErrCantGetCurrentFilename
}
return filename, nil
}

0 comments on commit 67bcbb0

Please sign in to comment.