This repository was archived by the owner on Aug 3, 2022. It is now read-only.
File tree 2 files changed +65
-0
lines changed
2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,44 @@ func ExpandHomeFolder(path string) (string, error) {
67
67
return buffer .String (), nil
68
68
}
69
69
70
+ // PrependHomeFolder ...
71
+ func PrependHomeFolder (path string ) (string , error ) {
72
+ if strings .HasPrefix (path , string (os .PathSeparator )) {
73
+ return path , nil
74
+ }
75
+
76
+ var buffer bytes.Buffer
77
+ usr , err := user .Current ()
78
+ if err != nil {
79
+ return "" , err
80
+ }
81
+
82
+ _ , err = buffer .WriteString (filepath .Join (usr .HomeDir , filepath .Clean (path )))
83
+ if err != nil {
84
+ return "" , err
85
+ }
86
+
87
+ return buffer .String (), nil
88
+ }
89
+
90
+ // Transformer ...
91
+ type Transformer func (path string ) (string , error )
92
+
93
+ // PathTransform ...
94
+ func PathTransform (path string , funcs ... Transformer ) (string , error ) {
95
+ p := path
96
+
97
+ var err error
98
+ for _ , fn := range funcs {
99
+ p , err = fn (path )
100
+ if err != nil {
101
+ return "" , err
102
+ }
103
+ }
104
+
105
+ return p , nil
106
+ }
107
+
70
108
func copy (src , dst string ) (int64 , error ) {
71
109
sfi , err := os .Stat (src )
72
110
if err != nil {
Original file line number Diff line number Diff line change 4
4
"io/ioutil"
5
5
"os"
6
6
"os/user"
7
+ "path/filepath"
7
8
"strings"
8
9
"testing"
9
10
@@ -99,3 +100,29 @@ func TestCopyFileMkdir(t *testing.T) {
99
100
100
101
assert .Equal (t , "Hello World" , string (b ))
101
102
}
103
+
104
+ func TestPrependHomeFolder (t * testing.T ) {
105
+ sr , err := user .Current ()
106
+ assert .NoError (t , err )
107
+
108
+ var tests = []struct {
109
+ desc string
110
+ path string
111
+ expected string
112
+ expectedErr error
113
+ }{
114
+ {
115
+ path : "csync/.nanorc" ,
116
+ expected : filepath .Join (sr .HomeDir , "csync/.nanorc" ),
117
+ expectedErr : nil ,
118
+ },
119
+ }
120
+
121
+ for _ , tc := range tests {
122
+ t .Run (tc .desc , func (t * testing.T ) {
123
+ p , err := files .PrependHomeFolder (tc .path )
124
+ assert .NoError (t , err )
125
+ assert .Equal (t , tc .expected , p )
126
+ })
127
+ }
128
+ }
You can’t perform that action at this time.
0 commit comments