@@ -12,6 +12,7 @@ import (
12
12
"strings"
13
13
14
14
"github.com/spf13/cobra"
15
+ "golang.org/x/mod/modfile"
15
16
)
16
17
17
18
var bin , output string
@@ -166,15 +167,11 @@ func main() {
166
167
cmd .Flags ().BoolVar (& install , "install-generators" , true , "Go get the generators" )
167
168
168
169
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 )
175
174
}
176
- } else {
177
- fmt .Fprintf (os .Stderr , "cannot parse go module: %v\n " , err )
178
175
}
179
176
cmd .Flags ().StringVar (& module , "module" , defaultModule , "Go module of the apiserver." )
180
177
@@ -226,3 +223,18 @@ func getCmd(cmd string, args ...string) *exec.Cmd {
226
223
e .Args = append (e .Args , args ... )
227
224
return e
228
225
}
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