-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos.go
More file actions
75 lines (58 loc) · 1.53 KB
/
Copy pathos.go
File metadata and controls
75 lines (58 loc) · 1.53 KB
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
package cli
import (
"fmt"
"io"
"os"
"path/filepath"
)
func CopyFile(inPath, outPath string) {
inFile, err := os.Open(inPath)
NoError(err, "Unable to open actual file %q", inPath)
defer inFile.Close()
outFile, err := os.Create(outPath)
NoError(err, "Unable to open expected file %q", outPath)
defer outFile.Close()
_, err = io.Copy(outFile, inFile)
NoError(err, "Unable to copy file %q to %q", inPath, outPath)
}
func FileExists(path string) bool {
stat, err := os.Stat(path)
if err != nil {
// For this script, we don't care
return false
}
return !stat.IsDir()
}
func DirectoryExists(path string) bool {
stat, err := os.Stat(path)
if err != nil {
// For this script, we don't care
return false
}
return stat.IsDir()
}
// WriteFile is a quick version `os.WriteFile` where [NoError] is used to
// ensure no error occur.
func WriteFile(name string, content string, args ...any) {
NoError(os.WriteFile(name, []byte(fmt.Sprintf(content, args...)), os.ModePerm), "Unable to write file")
}
func ReadFile(name string) string {
content, err := os.ReadFile(name)
NoError(err, "Unable to read file %q", name)
return string(content)
}
func WorkingDirectory() string {
directory, err := os.Getwd()
NoError(err, "Unable to get working directory")
return directory
}
func UserHomeDirectory() string {
home, err := os.UserHomeDir()
NoError(err, "Unable to get user home directory")
return home
}
func AbsolutePath(in string) string {
out, err := filepath.Abs(in)
NoError(err, "Unable to make path %q absolute", in)
return out
}