Skip to content

Commit

Permalink
Structure for all commands of the K8s code-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent 1160fc7 commit db6a5d7
Show file tree
Hide file tree
Showing 17 changed files with 547 additions and 147 deletions.
44 changes: 0 additions & 44 deletions staging/src/k8s.io/code-generator/build/kn-event-e2e.junit.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ import (
"k8s.io/code-generator/pkg/printer"
)

// Generator is the interface for generating client code.
type Generator interface {
Generate(args *client.Args) error
}

// Command is the command for generating client code.
type Command struct {
Gen client.Generator
Gen Generator
}

func (c Command) Matches(ex *execution.Vars) bool {
Expand All @@ -43,17 +48,17 @@ func (c Command) Run(ex *execution.Vars) {
ex.Exit(8)
return
}
gen := c.getOrCreateGen(ex)
gen := c.createOrGetGenerator(ex)
if err := gen.Generate(args); err != nil {
ex.PrintErrln("Error generating clients:", err)
ex.Exit(9)
return
}
}

func (c Command) getOrCreateGen(pr printer.Printer) client.Generator {
func (c Command) createOrGetGenerator(pr printer.Printer) Generator {
if c.Gen == nil {
return client.NewGenerator(pr)
return &client.Generator{Printer: pr}
}
return c.Gen
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCommandMatches(t *testing.T) {

cmd := client.Command{Gen: &testGen{}}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{"gen-client"}
v.Args = []string{cmd.Name()}
})
if !cmd.Matches(ex) {
t.Errorf("expected command to match")
Expand All @@ -42,7 +42,7 @@ func TestCommandRun(t *testing.T) {
cmd := client.Command{Gen: gen}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{
"gen-client",
cmd.Name(),
"--input-pkg-root", "foo",
"--output-pkg-root", "bar",
"--boilerplate", "baz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (c Command) Help() string {
args := &client.Args{}
fs := pflag.NewFlagSet("help", pflag.ContinueOnError)
defineFlags(fs, args)
return "Usage: code-generator gen-client [options]\n" +
return "Usage: code-generator " + c.Name() + " [options]\n" +
"\n" +
"Generate client code\n" +
c.OneLine() + "\n" +
"\n" +
"Options:\n" + fs.FlagUsagesWrapped(100)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@
package helpers

import (
"github.com/spf13/pflag"
"k8s.io/code-generator/internal/codegen/execution"
"k8s.io/code-generator/pkg/codegen/helpers"
"k8s.io/code-generator/pkg/printer"
)

type Command struct{}

func (c Command) Name() string {
return "gen-helpers"
}

func (c Command) OneLine() string {
return "Generate tagged helper code: conversions, deepcopy, and defaults"
// Generator is the interface for generating helper code.
type Generator interface {
Generate(args *helpers.Args) error
}

func (c Command) Help() string {
return `Usage: code-generator gen-helpers [options]
Generate tagged helper code: conversions, deepcopy, and defaults
Options:
--input-pkg-root <string>
The root package under which to search for files which request code to be
generated. This must be Go package syntax, e.g. "k8s.io/foo/bar".
--output-base <string>
The root directory under which to emit code. The concatenation of
<output-base> + <input-pkg-root> must be valid.
--boilerplate <string = path_to_kube_codegen_boilerplate>
An optional override for the header file to insert into generated files.
--extra-peer-dir <string>
An optional list (this flag may be specified multiple times) of "extra"
directories to consider during conversion generation.
`
type Command struct {
Gen Generator
}

func (c Command) Matches(ex *execution.Vars) bool {
return len(ex.Args) >= 1 && ex.Args[0] == c.Name()
}

func (c Command) Run(ex *execution.Vars) {
//TODO implement me
panic("implement me")
args := &helpers.Args{}
fs := pflag.NewFlagSet(ex.Args[0], pflag.ExitOnError)
defineFlags(fs, args)
if err := fs.Parse(ex.Args[1:]); err != nil {
ex.PrintErrln("Error parsing arguments:", err)
ex.PrintErrln()
ex.PrintErrln(c.Help())
ex.Exit(10)
return
}
gen := c.createOrGetGenerator(ex)
if err := gen.Generate(args); err != nil {
ex.PrintErrln("Error generating helpers:", err)
ex.Exit(11)
return
}
}

func (c Command) createOrGetGenerator(pr printer.Printer) Generator {
if c.Gen == nil {
return &helpers.Generator{Printer: pr}
}
return c.Gen
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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 helpers_test

import (
"k8s.io/code-generator/internal/codegen/command/helpers"
"k8s.io/code-generator/internal/codegen/execution"
pkghelpers "k8s.io/code-generator/pkg/codegen/helpers"
"testing"
)

func TestCommandMatches(t *testing.T) {
t.Parallel()

cmd := helpers.Command{Gen: &testGen{}}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{cmd.Name()}
})
if !cmd.Matches(ex) {
t.Errorf("expected command to match")
}
}

func TestCommandRun(t *testing.T) {
t.Parallel()

gen := &testGen{}
cmd := helpers.Command{Gen: gen}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{
cmd.Name(),
"--input-pkg-root", "foo",
"--output-base", "bar",
"--boilerplate", "baz",
}
})
cmd.Run(ex)
if len(gen.calls) != 1 {
t.Errorf("expected gen to be called once, got %d", len(gen.calls))
}
call := gen.calls[0]
if call.InputPkgRoot != "foo" {
t.Errorf("expected input package to be foo, got %s", call.InputPkgRoot)
}
if call.OutputBase != "bar" {
t.Errorf("expected output base to be bar, got %s", call.OutputBase)
}
if call.Boilerplate != "baz" {
t.Errorf("expected boilerplate to be baz, got %s", call.Boilerplate)
}
}

type testGen struct {
calls []pkghelpers.Args
}

func (t *testGen) Generate(args *pkghelpers.Args) error {
if t.calls == nil {
t.calls = make([]pkghelpers.Args, 0, 1)
}
t.calls = append(t.calls, *args)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 helpers

import (
"github.com/spf13/pflag"
"k8s.io/code-generator/pkg/codegen/helpers"
"reflect"
)

func defineFlags(fs *pflag.FlagSet, args *helpers.Args) {
ty := reflect.TypeOf(*args)
if f, ok := ty.FieldByName("InputPkgRoot"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.InputPkgRoot, "input-pkg-root", "", usage)
}
}
if f, ok := ty.FieldByName("OutputBase"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.OutputBase, "output-base", "", usage)
}
}
if f, ok := ty.FieldByName("Boilerplate"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.Boilerplate, "boilerplate", "", usage)
}
}
if f, ok := ty.FieldByName("ExtraPeerDirs"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringSliceVar(&args.ExtraPeerDirs, "extra-peer-dir", nil, usage)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 helpers

import (
"github.com/spf13/pflag"
"k8s.io/code-generator/pkg/codegen/helpers"
)

func (c Command) Name() string {
return "gen-helpers"
}

func (c Command) OneLine() string {
return "Generate tagged helper code: conversions, deepcopy, and defaults"
}

func (c Command) Help() string {
args := &helpers.Args{}
fs := pflag.NewFlagSet("help", pflag.ContinueOnError)
defineFlags(fs, args)
return "Usage: code-generator " + c.Name() + " [options]\n" +
"\n" +
c.OneLine() + "\n" +
"\n" +
"Options:\n" + fs.FlagUsagesWrapped(100)
}

0 comments on commit db6a5d7

Please sign in to comment.