Skip to content

apiserver-runtime-gen easier to use #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/mod v0.4.2
k8s.io/api v0.21.2
k8s.io/apimachinery v0.21.2
k8s.io/apiserver v0.21.2
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ limitations under the License.
// Install the code generators (from your module):
//
// $ go get sigs.k8s.io/apiserver-runtime/tools/apiserver-runtime-gen
// $ apiserver-runtime-gen --install
// $ apiserver-runtime-gen --install-generators
//
// Add the code generation tag to you main package:
//
Expand Down
28 changes: 20 additions & 8 deletions tools/apiserver-runtime-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
)

var bin, output string
Expand Down Expand Up @@ -166,15 +167,11 @@ func main() {
cmd.Flags().BoolVar(&install, "install-generators", true, "Go get the generators")

var defaultModule string
why := exec.Command("go", "mod", "why")
why.Stderr = os.Stderr
if m, err := why.Output(); err == nil {
parts := strings.Split(string(m), "\n")
if len(parts) > 1 {
defaultModule = parts[1]
cwd, _ := os.Getwd()
if modRoot := findModuleRoot(cwd); modRoot != "" {
if b, err := ioutil.ReadFile(path.Join(modRoot, "go.mod")); err == nil {
defaultModule = modfile.ModulePath(b)
}
} else {
fmt.Fprintf(os.Stderr, "cannot parse go module: %v\n", err)
}
cmd.Flags().StringVar(&module, "module", defaultModule, "Go module of the apiserver.")

Expand Down Expand Up @@ -226,3 +223,18 @@ func getCmd(cmd string, args ...string) *exec.Cmd {
e.Args = append(e.Args, args...)
return e
}

func findModuleRoot(dir string) string {
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}

parentDIR := path.Dir(dir)
if parentDIR == dir {
break
}
dir = parentDIR
}
return ""
}