Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Fix #963: symlinks problems when creating a new app #1010

Merged
merged 2 commits into from Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion generators/newapp/generator.go
Expand Up @@ -93,7 +93,8 @@ func (g Generator) Validate() error {
func (g Generator) validateInGoPath() error {
gpMultiple := envy.GoPaths()

larp := strings.ToLower(g.Root)
larp := strings.ToLower(meta.ResolveSymlinks(filepath.Dir(g.Root)))

for i := 0; i < len(gpMultiple); i++ {
lgpm := strings.ToLower(filepath.Join(gpMultiple[i], "src"))
if strings.HasPrefix(larp, lgpm) {
Expand Down
37 changes: 37 additions & 0 deletions meta/app.go
Expand Up @@ -37,6 +37,25 @@ func New(root string) App {
if root == "." {
root = pwd
}

// Handle symlinks
var oldPwd = pwd
pwd = ResolveSymlinks(pwd)
os.Chdir(pwd)
if runtime.GOOS != "windows" {
// On Non-Windows OS, os.Getwd() uses PWD env var as a prefered
// way to get the working dir.
os.Setenv("PWD", pwd)
}
defer func() {
// Restore PWD
os.Chdir(oldPwd)
if runtime.GOOS != "windows" {
os.Setenv("PWD", oldPwd)
}
}()

// Gather meta data
name := Name(filepath.Base(root))
pp := envy.CurrentPackage()
if filepath.Base(pp) != string(name) {
Expand Down Expand Up @@ -87,6 +106,24 @@ func New(root string) App {
return app
}

// ResolveSymlinks takes a path and gets the pointed path
// if the original one is a symlink.
func ResolveSymlinks(p string) string {
cd, err := os.Lstat(p)
if err != nil {
return p
}
if cd.Mode()&os.ModeSymlink != 0 {
// This is a symlink
r, err := filepath.EvalSymlinks(p)
if err != nil {
return p
}
return r
}
return p
}

func (a App) String() string {
b, _ := json.Marshal(a)
return string(b)
Expand Down