-
Notifications
You must be signed in to change notification settings - Fork 32
/
os.go
107 lines (94 loc) · 2.42 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
package core
import (
"fmt"
"github.com/mitchellh/go-homedir"
"io"
"os"
"strconv"
"strings"
)
// ExpandOrReturnPath expands a homedir path, if it can't be expanded, the original dir is returned
// since on these systems homedir cannot be used anyway.
func ExpandOrReturnPath(path string) string {
expanded, err := homedir.Expand(path)
if err != nil {
return path
}
return expanded
}
// GetEnv gets an environment variable. If it is not present, the default variable is used.
func GetEnv(name, defaultVal string) string {
val := os.Getenv(name)
if val == "" {
return defaultVal
}
return val
}
// GetEnvBool gets an environment variable as a bool. If not found the default value is used.
func GetEnvBool(name string, defaultVal bool) bool {
val := os.Getenv(name)
if val == name {
return defaultVal
}
res, err := strconv.ParseBool(val)
if err != nil {
return defaultVal
}
return res
}
// HasEnv checks if an environment variable is set.
func HasEnv(name string) bool {
val := os.Getenv(name)
return val != ""
}
// GetEnvInt gets an environment variable as an int. if not found or cast the
// defaultVal is returned.
func GetEnvInt(name string, defaultVal int) int {
val := os.Getenv(name)
if val == "" {
return defaultVal
}
res, err := strconv.Atoi(val)
if err != nil {
return defaultVal
}
return res
}
// IsTest returns true if the current process is a test.
func IsTest() bool {
if len(os.Args) == 0 {
return false
}
return strings.HasSuffix(os.Args[0], ".test")
}
// CopyFile copies a file from src to dest and preserves its permissions.
func CopyFile(src, dest string) error {
// Open source file for reading
//nolint: gosec
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer func() {
_ = srcFile.Close()
}()
// Obtain stat information from source
srcInfo, err := srcFile.Stat()
if err != nil {
return fmt.Errorf("failed to stat source file: %w", err)
}
// Create destination file with the source file permissions
//nolint: gosec
destFile, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return fmt.Errorf("failed to open destination file: %w", err)
}
defer func() {
_ = destFile.Close()
}()
// Copy data from source to destination
if _, err := io.Copy(destFile, srcFile); err != nil {
return fmt.Errorf("failed to copy data from source to destination: %w", err)
}
return nil
}