Skip to content

Commit

Permalink
Go-native code-gen entrypoint structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent c6887b1 commit bcedd4a
Show file tree
Hide file tree
Showing 20 changed files with 1,001 additions and 2 deletions.
44 changes: 44 additions & 0 deletions staging/src/k8s.io/code-generator/build/kn-event-e2e.junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="6" failures="0" errors="0" time="1.157295">
<testsuite tests="2" failures="0" time="1.008000" name="k8s.io/code-generator/internal/codegen" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
<testcase classname="k8s.io/code-generator/internal/codegen" name="TestRun" time="0.000000"></testcase>
<testcase classname="k8s.io/code-generator/internal/codegen" name="TestInvalid" time="0.000000"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0.000000" name="k8s.io/code-generator/internal/codegen/command" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0.000000" name="k8s.io/code-generator/internal/codegen/command/genclient" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0.000000" name="k8s.io/code-generator/internal/codegen/command/genhelpers" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0.000000" name="k8s.io/code-generator/internal/codegen/command/genopenapi" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
</testsuite>
<testsuite tests="2" failures="0" time="1.008000" name="k8s.io/code-generator/internal/codegen/command/help" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
<testcase classname="k8s.io/code-generator/internal/codegen/command/help" name="TestHelpCommand" time="0.000000"></testcase>
<testcase classname="k8s.io/code-generator/internal/codegen/command/help" name="TestInvalidCommand" time="0.000000"></testcase>
</testsuite>
<testsuite tests="2" failures="0" time="1.008000" name="k8s.io/code-generator/internal/codegen/execution" timestamp="2023-12-01T16:55:30+01:00">
<properties>
<property name="go.version" value="go1.21.4 linux/amd64"></property>
</properties>
<testcase classname="k8s.io/code-generator/internal/codegen/execution" name="TestPrint" time="0.000000"></testcase>
<testcase classname="k8s.io/code-generator/internal/codegen/execution" name="TestPrintErr" time="0.000000"></testcase>
</testsuite>
</testsuites>
36 changes: 36 additions & 0 deletions staging/src/k8s.io/code-generator/codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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

import (
"k8s.io/code-generator/internal/codegen"
"k8s.io/code-generator/internal/codegen/execution"
)

var options []execution.Option

func main() {
codegen.Run(options...)
}

// RunMain is a wrapper for main() to allow for testing.
func RunMain(opts ...execution.Option) {
old := options
defer func() { options = old }()
options = append(make([]execution.Option, 0, len(opts)), opts...)
main()
}
31 changes: 31 additions & 0 deletions staging/src/k8s.io/code-generator/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main_test

import (
"bytes"
"k8s.io/code-generator"
"k8s.io/code-generator/internal/codegen/execution"
"testing"
)

func TestMainFn(t *testing.T) {
var out, err bytes.Buffer
var retcode *int
main.RunMain(func(ex *execution.Vars) {
ex.Args = []string{"--help"}
ex.Out = &out
ex.Err = &err
ex.Exit = func(code int) {
retcode = &code
}
})

if retcode != nil {
t.Errorf("expected exit func will not be called, but was with %d", *retcode)
}
if out.Len() != 0 {
t.Errorf("expected no output, got %#v", out.String())
}
if !bytes.Contains(err.Bytes(), []byte("Command:")) {
t.Errorf("expected usage output, got %#v", err.String())
}
}
2 changes: 1 addition & 1 deletion staging/src/k8s.io/code-generator/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package codegenerator // import "k8s.io/code-generator"
package main // import "k8s.io/code-generator"
31 changes: 31 additions & 0 deletions staging/src/k8s.io/code-generator/internal/codegen/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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 codegen

import (
"k8s.io/code-generator/internal/codegen/command"
"k8s.io/code-generator/internal/codegen/execution"
)

// Command is a command that can be run by code-gen.
type Command interface {
// Matches returns true if the command matches the execution context.
Matches(ex *execution.Vars) bool
// Run runs the command.
Run(ex *execution.Vars)
command.Usage
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 genclient

import (
"k8s.io/code-generator/internal/codegen/execution"
)

type Command struct{}

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")
}

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

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

func (c Command) Help() string {
return `Usage: code-generator gen-client [options]
Generate client code
Options:
--input-pkg-root <string>
The root package under which to search for types.go files which request
clients to be generated. This must be Go package syntax, e.g.
"k8s.io/foo/bar".
--output-pkg-root <string>
The root package into which generated directories and files will be
placed. 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> + <output-pkg-root> must be valid.
--boilerplate <string = path_to_kube_codegen_boilerplate>
An optional override for the header file to insert into generated files.
--clientset-name <string = "clientset">
An optional override for the leaf name of the generated "clientset" directory.
--versioned-name <string = "versioned">
An optional override for the leaf name of the generated
"<clientset>/versioned" directory.
--with-applyconfig
Enables generation of applyconfiguration files.
--applyconfig-name <string = "applyconfiguration">
An optional override for the leaf name of the generated "applyconfiguration" directory.
--with-watch
Enables generation of listers and informers for APIs which support WATCH.
--listers-name <string = "listers">
An optional override for the leaf name of the generated "listers" directory.
--informers-name <string = "informers">
An optional override for the leaf name of the generated "informers" directory.
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 genhelpers

import (
"k8s.io/code-generator/internal/codegen/execution"
)

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"
}

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.
`
}

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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 genopenapi

import (
"k8s.io/code-generator/internal/codegen/execution"
)

type Command struct{}

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")
}

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

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

func (c Command) Help() string {
return `Usage: code-generator gen-openapi [options]
Generate openapi code
Options:
--input-pkg-root <string>
The root package under which to search for files which request openapi to
be generated. This must be Go package syntax, e.g. "k8s.io/foo/bar".
--output-pkg-root <string>
The root package under which generated directories and files
will be placed. 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.
--openapi-name <string = "openapi">
An optional override for the leaf name of the generated directory.
--extra-pkgs <string>
An optional list of additional packages to be imported during openapi
generation. The argument must be Go package syntax, e.g.
"k8s.io/foo/bar". It may be a single value or a comma-delimited list.
This flag may be repeated.
--report-filename <string = "/dev/null">
An optional path at which to write an API violations report. "-" means
stdout.
--update-report
If specified, update the report file in place, rather than diffing it.
--boilerplate <string = path_to_kube_codegen_boilerplate>
An optional override for the header file to insert into generated files.
`
}

0 comments on commit bcedd4a

Please sign in to comment.