File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ package goutils
22
33import (
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 {
2628func 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments