Skip to content

Commit 16bafbc

Browse files
authored
Merge pull request #48 from zhiyuone/doc
apiserver-runtime-gen easier to use
2 parents 87802d7 + 2df631f commit 16bafbc

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/spf13/cobra v1.1.3
1313
github.com/spf13/pflag v1.0.5
1414
github.com/stretchr/testify v1.7.0
15+
golang.org/x/mod v0.4.2
1516
k8s.io/api v0.21.2
1617
k8s.io/apimachinery v0.21.2
1718
k8s.io/apiserver v0.21.2

pkg/builder/doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ limitations under the License.
4343
// Install the code generators (from your module):
4444
//
4545
// $ go get sigs.k8s.io/apiserver-runtime/tools/apiserver-runtime-gen
46-
// $ apiserver-runtime-gen --install
46+
// $ apiserver-runtime-gen --install-generators
4747
//
4848
// Add the code generation tag to you main package:
4949
//

tools/apiserver-runtime-gen/main.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/spf13/cobra"
15+
"golang.org/x/mod/modfile"
1516
)
1617

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

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

@@ -226,3 +223,18 @@ func getCmd(cmd string, args ...string) *exec.Cmd {
226223
e.Args = append(e.Args, args...)
227224
return e
228225
}
226+
227+
func findModuleRoot(dir string) string {
228+
for {
229+
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
230+
return dir
231+
}
232+
233+
parentDIR := path.Dir(dir)
234+
if parentDIR == dir {
235+
break
236+
}
237+
dir = parentDIR
238+
}
239+
return ""
240+
}

0 commit comments

Comments
 (0)