Skip to content

Commit

Permalink
Parsing of gen-client flags
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent bcedd4a commit 1160fc7
Show file tree
Hide file tree
Showing 24 changed files with 559 additions and 260 deletions.
20 changes: 10 additions & 10 deletions staging/src/k8s.io/code-generator/codegen.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2023 The Kubernetes Authors.
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
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
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.
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 main
Expand Down
16 changes: 16 additions & 0 deletions staging/src/k8s.io/code-generator/codegen_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 main_test

import (
Expand Down
20 changes: 10 additions & 10 deletions staging/src/k8s.io/code-generator/internal/codegen/command.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2023 The Kubernetes Authors.
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
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
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.
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 codegen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 client

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

// Command is the command for generating client code.
type Command struct {
Gen client.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) {
args := &client.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(8)
return
}
gen := c.getOrCreateGen(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 {
if c.Gen == nil {
return client.NewGenerator(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 client_test

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

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

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

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

gen := &testGen{}
cmd := client.Command{Gen: gen}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{
"gen-client",
"--input-pkg-root", "foo",
"--output-pkg-root", "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.OutputPkgRoot != "bar" {
t.Errorf("expected output package to be bar, got %s", call.OutputPkgRoot)
}
if call.Boilerplate != "baz" {
t.Errorf("expected boilerplate to be baz, got %s", call.Boilerplate)
}
}

type testGen struct {
calls []pkgclient.Args
}

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

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

func defineFlags(fs *pflag.FlagSet, args *client.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("OutputPkgRoot"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.OutputPkgRoot, "output-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("ClientsetName"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.ClientsetName, "clientset-name", "", usage)
}
}
if f, ok := ty.FieldByName("VersionedName"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.VersionedName, "versioned-name", "", usage)
}
}
if f, ok := ty.FieldByName("WithApplyConfig"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.BoolVar(&args.WithApplyConfig, "with-applyconfig", false, usage)
}
}
if f, ok := ty.FieldByName("ApplyConfigName"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.ApplyConfigName, "applyconfig-name", "", usage)
}
}
if f, ok := ty.FieldByName("WithWatch"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.BoolVar(&args.WithWatch, "with-watch", false, usage)
}
}
if f, ok := ty.FieldByName("ListersName"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.ListersName, "listers-name", "", usage)
}
}
if f, ok := ty.FieldByName("InformersName"); ok {
if usage, ook := f.Tag.Lookup("doc"); ook {
fs.StringVar(&args.InformersName, "informers-name", "", 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 client

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

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

func (c Command) OneLine() string {
return "Generate client code"
}

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" +
"\n" +
"Generate client code\n" +
"\n" +
"Options:\n" + fs.FlagUsagesWrapped(100)
}

0 comments on commit 1160fc7

Please sign in to comment.