-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
177 lines (150 loc) · 3.72 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package pathutil
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)
// New construct Path
//
// for example
//
// path := New("/home/test", ".vimrc")
//
// input can be `string` or type implements `fmt.Stringer` interface
func New(path ...interface{}) (Path, error) {
newPath := PathImpl{}
paths := make([]string, len(path))
for index, chunk := range path {
var pathChunk string
switch t := chunk.(type) {
case string:
pathChunk = chunk.(string)
case fmt.Stringer:
pathChunk = chunk.(fmt.Stringer).String()
default:
return nil, errors.Errorf("Chunk %d is type %t (allowed are string or fmt.Stringer)", index, t)
}
if len(pathChunk) == 0 {
return nil, errors.Errorf("Paths requires defined, positive-lengths parts (part %d is empty", index)
}
paths[index] = pathChunk
}
joinPath := filepath.Join(paths...)
newPath.path = filepath.Clean(joinPath)
if runtime.GOOS == "windows" {
newPath.path = strings.Replace(newPath.path, "\\", "/", -1)
}
return newPath, nil
}
type tempOpt struct {
dir string
prefix string
}
// TempOpt is func for configure tempdir or tempfile
type TempOpt func(*tempOpt)
// TempDir set directory where is temp file/dir create, empty string `""` (default) means TEMPDIR (`os.TempDir`)
func TempDir(dir string) TempOpt {
return func(o *tempOpt) {
o.dir = dir
}
}
// TempPrefix set name beginning with prefix
func TempPrefix(prefix string) TempOpt {
return func(o *tempOpt) {
o.prefix = prefix
}
}
// NewTempFile create temp file
//
// for cleanup use defer
//
// temp, err := NewTempFile(TempOpt{})
// defer temp.Remove()
//
// if you need only temp file name, you must delete file
//
// temp, err := NewTempFile()
// temp.Remove()
//
// if you need set directory or prefix, then use `TempDir` and/or `TempPrefix`
//
// temp, err := NewTempFile(TempDir("/home/my/test"), TempPrefix("myfile"))
// ...
func NewTempFile(options ...TempOpt) (p Path, err error) {
opt := tempOpt{}
for _, o := range options {
o(&opt)
}
file, err := os.CreateTemp(opt.dir, opt.prefix)
if err != nil {
return nil, errors.Wrapf(err, "NewTempFile(%+v) fail", opt)
}
defer func() {
if errClose := file.Close(); errClose != nil {
err = errClose
}
}()
return New(file.Name())
}
// NewTempDir create temp directory
//
// for cleanup use `defer`
//
// tempdir, err := pathutil.NewTempDir()
// defer tempdir.RemoveTree()
//
// if you need set directory or prefix, then use `TempDir` and/or `TempPrefix`
//
// temp, err := NewTempFile(TempDir("/home/my/test"), TempPrefix("myfile"))
// ...
func NewTempDir(options ...TempOpt) (Path, error) {
opt := tempOpt{}
for _, o := range options {
o(&opt)
}
dir, err := os.MkdirTemp(opt.dir, opt.prefix)
if err != nil {
return nil, errors.Wrapf(err, "NewTempDir(%+v) fail", opt)
}
return New(dir)
}
// Cwd create new Path from current working directory
// optional is possible to set subpath
//
// for example
//
// gitConfigPath, err := pathutil.Cwd('.git/config')
func Cwd(subpath ...string) (Path, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "Getwd fail")
}
return New(join(cwd, subpath)...)
}
// Home create new Path from home directory
// optional is possible to set subpath
//
// for example
//
// initPath, err := pathutil.Home('.config/nvim/init.vim')
//
// (internally use https://github.com/mitchellh/go-homedir library)
func Home(subpath ...string) (Path, error) {
home, err := homedir.Dir()
if err != nil {
return nil, errors.Wrap(err, "homedir.Dir fail")
}
return New(join(home, subpath)...)
}
func join(a string, b []string) []interface{} {
p := make([]interface{}, len(b)+1)
p[0] = a
for i, c := range b {
p[i+1] = c
}
return p
}