Skip to content

Commit

Permalink
Better handling of generated package name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Jul 10, 2018
1 parent 5d3c8ed commit 936bc76
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ func (c *PackageConfig) normalize() error {
if c.Filename == "" {
return errors.New("Filename is required")
}
c.Filename = abs(c.Filename)
// If Package is not set, first attempt to load the package at the output dir. If that fails
// fallback to just the base dir name of the output filename.
if c.Package == "" {
cwd, _ := os.Getwd()
pkg, err := build.Default.Import(c.Dir(), cwd, 0)
if err != nil {
c.Package = filepath.Base(c.Dir())
} else {
pkg, _ := build.Default.Import(c.ImportPath(), cwd, 0)
if pkg.Name != "" {
c.Package = pkg.Name
} else {
c.Package = filepath.Base(c.Dir())
}
}
c.Package = sanitizePackageName(c.Package)
c.Filename = abs(c.Filename)
return nil
}

Expand Down

0 comments on commit 936bc76

Please sign in to comment.