MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / detectPackageName

Function detectPackageName

cmd/oapi-codegen/oapi-codegen.go:384–417  ·  view source on GitHub ↗

detectPackageName detects and sets PackageName if not already set.

(cfg *configuration)

Source from the content-addressed store, hash-verified

382
383// detectPackageName detects and sets PackageName if not already set.
384func detectPackageName(cfg *configuration) error {
385 if cfg.PackageName != "" {
386 return nil
387 }
388
389 if cfg.OutputFile != "" {
390 // Determine from the package name of the output file.
391 dir := filepath.Dir(cfg.PackageName)
392 cmd := exec.Command("go", "list", "-f", "{{.Name}}", dir)
393 out, err := cmd.CombinedOutput()
394 if err != nil {
395 outStr := string(out)
396 switch {
397 case strings.Contains(outStr, "expected 'package', found 'EOF'"):
398 // Redirecting the output to current directory which hasn't
399 // written anything yet, ignore.
400 case strings.HasPrefix(outStr, "no Go files in"):
401 // No go files yet, ignore.
402 default:
403 // Unexpected failure report.
404 return fmt.Errorf("detect package name for %q output: %q: %w", dir, string(out), err)
405 }
406 } else {
407 cfg.PackageName = string(out)
408 return nil
409 }
410 }
411
412 // Fallback to determining from the spec file name.
413 parts := strings.Split(filepath.Base(flag.Arg(0)), ".")
414 cfg.PackageName = codegen.LowercaseFirstCharacter(codegen.ToCamelCase(parts[0]))
415
416 return nil
417}
418
419// updateConfigFromFlags updates a loaded configuration from flags. Flags
420// override anything in the file. We generate errors for any unsupported

Callers 1

mainFunction · 0.85

Calls 2

LowercaseFirstCharacterFunction · 0.92
ToCamelCaseFunction · 0.92

Tested by

no test coverage detected