Skip to content

Commit 6e7140e

Browse files
committed
🍏 fixbug::Specific path conversion
1 parent ab1928c commit 6e7140e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

file.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package goutils
22

33
import (
44
"io/ioutil"
5+
"log"
56
"os"
67
"path"
8+
"strings"
79
)
810

911
// IsFile returns true if given path is a file,
@@ -26,4 +28,21 @@ func IsExist(path string) bool {
2628
func WriteFile(filename string, data []byte) error {
2729
os.MkdirAll(path.Dir(filename), os.ModePerm)
2830
return ioutil.WriteFile(filename, data, 0644)
29-
}
31+
}
32+
33+
func UserHomeDir() string {
34+
return os.Getenv("HOME")
35+
}
36+
37+
func ConversionInPath(inPath string) string {
38+
log.Println("Func::ConversionInpath:inpath to:: ", inPath)
39+
if inPath == "~" || strings.HasPrefix(inPath, "~"+string(os.PathSeparator)) {
40+
inPath = UserHomeDir() + inPath[1:]
41+
}
42+
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
43+
inPath = UserHomeDir() + inPath[5:]
44+
}
45+
46+
inPath = os.ExpandEnv(inPath)
47+
return inPath
48+
}

file_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package goutils
2+
3+
import "testing"
4+
5+
//功能测试
6+
func Test_ConversionInPath_1(t *testing.T) {
7+
inPath := "~/test.log"
8+
rs_inPath := ConversionInPath(inPath)
9+
t.Log("New function 测试:", rs_inPath)
10+
}
11+
12+
func Test_ConversionInPath_2(t *testing.T) {
13+
inPath := "$HOME/test/test.log"
14+
rs_inPath := ConversionInPath(inPath)
15+
t.Log("New function 测试:", rs_inPath)
16+
}

0 commit comments

Comments
 (0)