feat: get module name from path
parent
2fe4e8ec27
commit
7f351b49f6
2
go.mod
2
go.mod
|
@ -5,6 +5,7 @@ go 1.19
|
||||||
require (
|
require (
|
||||||
github.com/make-go-great/color-go v0.4.1
|
github.com/make-go-great/color-go v0.4.1
|
||||||
github.com/urfave/cli/v2 v2.23.5
|
github.com/urfave/cli/v2 v2.23.5
|
||||||
|
golang.org/x/mod v0.7.0
|
||||||
golang.org/x/tools v0.3.0
|
golang.org/x/tools v0.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,6 +16,5 @@ require (
|
||||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||||
golang.org/x/mod v0.7.0 // indirect
|
|
||||||
golang.org/x/sys v0.2.0 // indirect
|
golang.org/x/sys v0.2.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/mod/modfile"
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,21 +26,22 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrEmptyPaths = errors.New("empty paths")
|
ErrEmptyPaths = errors.New("empty paths")
|
||||||
ErrNotGoFile = errors.New("not go file")
|
ErrNotGoFile = errors.New("not go file")
|
||||||
ErrGoGeneratedFile = errors.New("go generated file")
|
ErrGoGeneratedFile = errors.New("go generated file")
|
||||||
ErrGoModNotExist = errors.New("go mod not exist")
|
ErrGoModNotExist = errors.New("go mod not exist")
|
||||||
|
ErrGoModEmptyModule = errors.New("go mod empty module")
|
||||||
)
|
)
|
||||||
|
|
||||||
// stdPackages -> save std packages for later search
|
// stdPackages -> save std packages for later search
|
||||||
// packageNames -> map path to its go.mod package name
|
// moduleNames -> map path to its go.mod module name
|
||||||
// formattedPaths -> make sure we not format path more than 1 time
|
// formattedPaths -> make sure we not format path more than 1 time
|
||||||
type Formatter struct {
|
type Formatter struct {
|
||||||
stdPackages map[string]struct{}
|
stdPackages map[string]struct{}
|
||||||
packageNames map[string]string
|
moduleNames map[string]string
|
||||||
formattedPaths map[string]struct{}
|
formattedPaths map[string]struct{}
|
||||||
companyPrefix string
|
companyPrefix string
|
||||||
muPackageNames sync.RWMutex
|
muModuleNames sync.RWMutex
|
||||||
muFormattedPaths sync.RWMutex
|
muFormattedPaths sync.RWMutex
|
||||||
isList bool
|
isList bool
|
||||||
isWrite bool
|
isWrite bool
|
||||||
|
@ -65,7 +67,7 @@ func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) {
|
||||||
}
|
}
|
||||||
ft.log("stdPackages: %+v\n", ft.stdPackages)
|
ft.log("stdPackages: %+v\n", ft.stdPackages)
|
||||||
|
|
||||||
ft.packageNames = make(map[string]string)
|
ft.moduleNames = make(map[string]string)
|
||||||
ft.formattedPaths = make(map[string]struct{})
|
ft.formattedPaths = make(map[string]struct{})
|
||||||
|
|
||||||
return ft, nil
|
return ft, nil
|
||||||
|
@ -143,7 +145,7 @@ func (ft *Formatter) formatFile(path string) error {
|
||||||
ft.log("importsAST: %+v\n", importsAST)
|
ft.log("importsAST: %+v\n", importsAST)
|
||||||
|
|
||||||
// TODO: Find dir go.mod package name
|
// TODO: Find dir go.mod package name
|
||||||
pkgName, err := ft.packageName(path)
|
pkgName, err := ft.moduleName(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -251,13 +253,13 @@ func (ft *Formatter) groupImports(importsAST map[string]*ast.ImportSpec) (map[st
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ft *Formatter) packageName(path string) (string, error) {
|
func (ft *Formatter) moduleName(path string) (string, error) {
|
||||||
ft.muPackageNames.RLock()
|
ft.muModuleNames.RLock()
|
||||||
if pkgName, ok := ft.packageNames[path]; ok {
|
if pkgName, ok := ft.moduleNames[path]; ok {
|
||||||
ft.muPackageNames.RUnlock()
|
ft.muModuleNames.RUnlock()
|
||||||
return pkgName, nil
|
return pkgName, nil
|
||||||
}
|
}
|
||||||
ft.muPackageNames.RUnlock()
|
ft.muModuleNames.RUnlock()
|
||||||
|
|
||||||
// Copy from goimports-reviser
|
// Copy from goimports-reviser
|
||||||
// Check path/go.mod first
|
// Check path/go.mod first
|
||||||
|
@ -286,7 +288,22 @@ func (ft *Formatter) packageName(path string) (string, error) {
|
||||||
}
|
}
|
||||||
ft.log("goModPath: %+v\n", goModPath)
|
ft.log("goModPath: %+v\n", goModPath)
|
||||||
|
|
||||||
return "", nil
|
goModPathBytes, err := os.ReadFile(goModPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("os: failed to read file: [%s] %w", goModPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
goModFile, err := modfile.Parse(goModPath, goModPathBytes, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("modfile: failed to parse: [%s] %w", goModPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := goModFile.Module.Mod.Path
|
||||||
|
if result == "" {
|
||||||
|
return "", ErrGoModEmptyModule
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap log.Printf with verbose flag
|
// Wrap log.Printf with verbose flag
|
||||||
|
|
Loading…
Reference in New Issue