mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
36 lines
587 B
Go
36 lines
587 B
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func GetFullPackage(pkg string) (string, error) {
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
pkgPath := path.Join(dir, pkg)
|
|
info, err := os.Stat(pkgPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
return "", fmt.Errorf("%s is not a directory", pkg)
|
|
}
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
parent := path.Join(gopath, "src")
|
|
pos := strings.Index(pkgPath, parent)
|
|
if pos < 0 {
|
|
return "", fmt.Errorf("%s is not a correct package", pkg)
|
|
}
|
|
|
|
// skip slash
|
|
return pkgPath[len(parent)+1:], nil
|
|
}
|