goctl added
This commit is contained in:
35
tools/goctl/util/env.go
Normal file
35
tools/goctl/util/env.go
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user