-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix symlink issue on windows for goctl (#1034)
* fix symlink issue on windows for goctl * move readlink into separate file
- Loading branch information
Showing
5 changed files
with
66 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package proc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package proc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package util | ||
|
||
func ReadLink(name string) (string, error) { | ||
return name, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package util | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// ReadLink returns the destination of the named symbolic link recursively. | ||
func ReadLink(name string) (string, error) { | ||
name, err := filepath.Abs(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if _, err := os.Lstat(name); err != nil { | ||
return name, nil | ||
} | ||
|
||
// uncheck condition: ignore file path /var, maybe be temporary file path | ||
if name == "/" || name == "/var" { | ||
return name, nil | ||
} | ||
|
||
isLink, err := isLink(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !isLink { | ||
dir, base := filepath.Split(name) | ||
dir = filepath.Clean(dir) | ||
dir, err := ReadLink(dir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(dir, base), nil | ||
} | ||
|
||
link, err := os.Readlink(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dir, base := filepath.Split(link) | ||
dir = filepath.Dir(dir) | ||
dir, err = ReadLink(dir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(dir, base), nil | ||
} |