-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
559 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
staging/src/k8s.io/code-generator/internal/codegen/command.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
77 changes: 77 additions & 0 deletions
77
staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
82 changes: 82 additions & 0 deletions
82
staging/src/k8s.io/code-generator/internal/codegen/command/client/flags.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
staging/src/k8s.io/code-generator/internal/codegen/command/client/help.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.