-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
210 lines (172 loc) · 4.58 KB
/
helpers.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package viaduct
import (
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
// ExpandPath ensures that "~" are expanded.
func ExpandPath(path string) string {
if strings.HasPrefix(path, "~") {
if p, err := filepath.Abs(strings.Replace(path, "~", Attribute.User.HomeDir, 1)); err == nil {
path = p
} else {
log.Fatal(err)
}
}
return path
}
// ExpandPathRoot is like ExpandPath, but ignores the user attribute
func ExpandPathRoot(path string) string {
p, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
return p
}
// PrependSudo takes a slice of args and simply prepends sudo to the front.
func PrependSudo(args []string) []string {
return append([]string{"sudo"}, args...)
}
// RunCommand is essentially a wrapper around exec.Command. Generally the
// Execute resource should be used, but sometimes it can be useful to run
// things directly.
func RunCommand(command ...string) error {
cmd := exec.Command("bash", "-c", strings.Join(command, " "))
cmd.Stderr = os.Stderr
return cmd.Run()
}
// SudoCommand is the same as RunCommand but runs with sudo.
func SudoCommand(command ...string) error {
return RunCommand(PrependSudo(command)...)
}
// IsUbuntu returns true if the distribution is Ubuntu
func IsUbuntu() bool {
// Linux Mint uses IDLike since it's an Ubuntu fork
return strings.Contains(Attribute.Platform.ID, "ubuntu") || strings.Contains(Attribute.Platform.IDLike, "ubuntu")
}
// FileExists returns true if the file exists
func FileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
// FileContents simply returns the file contents as a string
func FileContents(path string) string {
c, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
return string(c)
}
// LinkExists returns true if the symlink exists
func LinkExists(path string) bool {
if _, err := os.Lstat(path); err == nil {
return true
}
return false
}
// IsDirectory is an alias for DirExists
func IsDirectory(path string) bool {
return DirExists(path)
}
// DirExists returns true if the file exists, and is a directory
func DirExists(path string) bool {
if info, err := os.Stat(path); err == nil {
if info.IsDir() {
return true
}
}
return false
}
// ListFiles returns all files within a path
func ListFiles(path string) (files []string, err error) {
err = filepath.Walk(path, func(p string, f os.FileInfo, err error) error {
if err != nil {
return err
}
files = append(files, p)
return nil
})
return files, err
}
// MatchChmod returns true if the permissions of the path match
func MatchChmod(path string, perms fs.FileMode) bool {
if info, err := os.Stat(path); err == nil {
if info.Mode() == perms {
return true
}
} else {
log.Fatal(err)
}
return false
}
// MatchChown returns true if the path is owned by the specified user and group
func MatchChown(path string, user, group int) bool {
if info, err := os.Stat(path); err == nil {
stat := info.Sys().(*syscall.Stat_t)
uid := stat.Uid
gid := stat.Gid
if user == int(uid) && group == int(gid) {
return true
}
} else {
log.Fatal(err)
}
return false
}
// IsRoot returns true if the user is root
func IsRoot() bool {
return Attribute.runuser.Username == "root"
}
// TmpFile returns the path for a Viaduct temporary file
func TmpFile(path string) string {
return filepath.Join(Attribute.TmpDir, path)
}
// FileSize returns the file size in bytes
func FileSize(path string) int64 {
f, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
return f.Size()
}
// CommandTrue is similar to the "Unless" parameter found in some resources, but instead
// can be used freeform within configuration. If it exits cleanly, then it
// returns true.
func CommandTrue(command string) bool {
c := strings.Split(command, " ")
// nolint:gosec
cmd := exec.Command("bash", "-c", strings.Join(c, " "))
if Cli.Quiet {
cmd.Stderr = os.Stderr
} else if !Cli.Silent {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
if err := cmd.Run(); err == nil {
return true
} else {
return false
}
}
// CommandFalse is the same as True, but returns the opposite.
func CommandFalse(command string) bool {
return !CommandTrue(command)
}
// CommandOutput will run a command and provide the output. Can be useful in if
// statements for checking arbitary values. Will not error, but will return an
// empty string.
func CommandOutput(command string) string {
c := strings.Split(command, " ")
cmd := exec.Command("bash", "-c", strings.Join(c, " "))
if out, err := cmd.Output(); err == nil {
return strings.TrimSpace(string(out))
} else {
return ""
}
}