generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos.go
127 lines (113 loc) · 2.92 KB
/
os.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
package u
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
)
// TempfileWithContent creates a tempfile with specified content written in it, it also seeks the file pointer so you can read it directly.
// The second returned parameter is a cleanup function that closes and removes the temp file.
func TempfileWithContent(content []byte) (*os.File, func(), error) {
// create temp file
tmpfile, err := ioutil.TempFile("", "u")
if err != nil {
return nil, nil, err
}
// write content
_, err = tmpfile.Write(content)
if err != nil {
return nil, nil, err
}
// seek at the beginning of file
_, err = tmpfile.Seek(0, io.SeekStart)
if err != nil {
return nil, nil, err
}
cleanup := func() {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
}
return tmpfile, cleanup, nil
}
// MustTempfileWithContent wraps TempfileWithContent and panics if initialization fails.
func MustTempfileWithContent(content []byte) (*os.File, func()) {
f, cleanup, err := TempfileWithContent(content)
if err != nil {
panic(err)
}
return f, cleanup
}
// PathExists checks whether a path exists or not.
func PathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// DirExists checks whether a path exists and is a directory.
func DirExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.IsDir()
}
// FileExists checks whether a path exists and is a regular file.
func FileExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.Mode().IsRegular()
}
// TempFileName returns a valid temporary file name (the file is not created).
func TempFileName(dir, pattern string) (string, error) {
tmpfile, err := ioutil.TempFile(dir, pattern)
if err != nil {
return "", err
}
tmpfile.Close()
os.Remove(tmpfile.Name())
return tmpfile.Name(), nil
}
// MustTempFileName wraps TempFileName and panics if initialization fails.
func MustTempFileName(dir, pattern string) string {
ret, err := TempFileName(dir, pattern)
if err != nil {
panic(err)
}
return ret
}
// CreateEmptyFileWithSize creates a new file of the desired size, filled with zeros.
func CreateEmptyFileWithSize(path string, size uint) error {
fd, err := os.Create(path)
if err != nil {
return fmt.Errorf("create failed: %v", err)
}
if size > 0 {
_, err = fd.Seek(int64(size)-1, 0)
if err != nil {
return fmt.Errorf("seek failed: %v", err)
}
_, err = fd.Write([]byte{0})
if err != nil {
return fmt.Errorf("write failed: %v", err)
}
}
err = fd.Close()
if err != nil {
return fmt.Errorf("close failed: %v", err)
}
return nil
}
// CurrentUsename returns the current user's username.
// If username cannot be retrieved, it returns the passed fallback.
func CurrentUsername(fallback string) string {
current, err := user.Current()
if err == nil && current.Username != "" {
return current.Username
}
if name := os.Getenv("USER"); name != "" {
return name
}
return fallback
}