You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/util/env.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
}