Skip to content

Commit 7479e06

Browse files
authored
Merge pull request #77 from yue9944882/feat/support-single-group-pkg
Support single api group project
2 parents 965f497 + 0ebcafb commit 7479e06

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

tools/apiserver-runtime-gen/main.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,17 @@ func doGen() error {
116116
}
117117

118118
if gen["client-gen"] {
119+
inputBase := ""
120+
versionsInputs := inputs
121+
// e.g. base = "example.io/foo/api", strippedVersions = "v1,v1beta1"
122+
// e.g. base = "example.io/foo/pkg/apis", strippedVersions = "test/v1,test/v1beta1"
123+
if base, strippedVersions, ok := findInputBase(module, versions); ok {
124+
inputBase = base
125+
versionsInputs = strings.Join(strippedVersions, ",")
126+
}
119127
err := run(getCmd("client-gen",
120-
"--clientset-name", "versioned", "--input-base", "",
121-
"--input", inputs, "--output-package", path.Join(module, "pkg/generated/clientset")))
128+
"--clientset-name", "versioned", "--input-base", inputBase,
129+
"--input", versionsInputs, "--output-package", path.Join(module, "pkg/generated/clientset")))
122130
if err != nil {
123131
return err
124132
}
@@ -238,3 +246,32 @@ func findModuleRoot(dir string) string {
238246
}
239247
return ""
240248
}
249+
250+
func findInputBase(module string, versions []string) (string, []string, bool) {
251+
if allHasPrefix(filepath.Join(module, "api"), versions) {
252+
base := filepath.Join(module, "api")
253+
return base, allTrimPrefix(base+"/", versions), true
254+
}
255+
if allHasPrefix(filepath.Join(module, "pkg", "apis"), versions) {
256+
base := filepath.Join(module, "pkg", "apis")
257+
return base, allTrimPrefix(base+"/", versions), true
258+
}
259+
return "", nil, false
260+
}
261+
262+
func allHasPrefix(prefix string, paths []string) bool {
263+
for _, p := range paths {
264+
if !strings.HasPrefix(p, prefix) {
265+
return false
266+
}
267+
}
268+
return true
269+
}
270+
271+
func allTrimPrefix(prefix string, versions []string) []string {
272+
vs := make([]string, 0)
273+
for _, v := range versions {
274+
vs = append(vs, strings.TrimPrefix(v, prefix))
275+
}
276+
return vs
277+
}

0 commit comments

Comments
 (0)