forked from rwxrob/bonzai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
332 lines (294 loc) · 8.07 KB
/
run.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package run
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/rwxrob/bonzai/futil"
)
// Executable returns the absolute path of the executable, resolving
// any symbolic links. It first retrieves the executable path using
// [os.Executable] and, if successful, evaluates any symbolic links
// in the path using [filepath.EvalSymlinks].
func Executable() (string, error) {
path, err := os.Executable()
if err != nil {
return path, err
}
return filepath.EvalSymlinks(path)
}
// ExeName returns just the base name of the executable by parsing
// it from [os.Args] at index 0 (same as $0 in shell). No attempt to
// resolve any symbolic links or remove any suffix is made.
func ExeName() string { return filepath.Base(os.Args[0]) }
// RealExeName retrieves the name of the current executable, resolving any
// symbolic links to return the base name. It returns an error if unable
// to retrieve or evaluate the executable path.
func RealExeName() (string, error) {
path, err := os.Executable()
if err != nil {
return path, err
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
return path, err
}
return filepath.Base(path), nil
}
func addExeName(base string) string {
name := ExeName()
return filepath.Join(base, name)
}
func addRealExeName(base string) (string, error) {
name, err := RealExeName()
if err != nil {
return "", err
}
return filepath.Join(base, name), nil
}
func ExeCacheDir() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return addExeName(dir), nil
}
func RealExeCacheDir() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return addRealExeName(dir)
}
func ExeConfigDir() (string, error) {
dir, err := futil.UserConfigDir()
if err != nil {
return "", err
}
return addExeName(dir), nil
}
func RealExeStateDir() (string, error) {
dir, err := futil.UserStateDir()
if err != nil {
return "", err
}
return addExeName(dir), nil
}
func ExeStateDir() (string, error) {
dir, err := futil.UserStateDir()
if err != nil {
return "", err
}
return addExeName(dir), nil
}
func ExeIsSymLink() (bool, error) {
path, err := os.Executable()
if err != nil {
return false, err
}
return futil.IsSymLink(path)
}
func ExeIsHardLink() (bool, error) {
path, err := os.Executable()
if err != nil {
return false, err
}
return futil.IsHardLink(path)
}
// SysExec will check for the existence of the first argument as an
// executable on the system and then execute it using syscall.Exec(),
// which replaces the currently running program with the new one in all
// respects (stdin, stdout, stderr, process ID, signal handling, etc).
// Generally speaking, this is only available on UNIX variations. This
// is exceptionally faster and cleaner than calling any of the os/exec
// variations, but it can make your code far be less compatible
// with different operating systems.
func SysExec(args ...string) error {
if len(args) == 0 {
return fmt.Errorf("missing name of executable")
}
path, err := exec.LookPath(args[0])
if err != nil {
return err
}
// exits the program unless there is an error
return syscall.Exec(path, args, os.Environ())
}
// Exec checks for existence of first argument as an executable on the
// system and then runs it with [exec.Command.Run] exiting in a way that
// is supported across all architectures that Go supports. The [os.Stdin],
// [os.Stdout], and [os.Stderr] are connected directly to that of the calling
// program. Sometimes this is insufficient and the UNIX-specific SysExec
// is preferred.
func Exec(args ...string) error {
if len(args) == 0 {
return fmt.Errorf("missing name of executable")
}
path, err := exec.LookPath(args[0])
if err != nil {
return err
}
cmd := exec.Command(path, args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Out returns the standard output of the executed command as
// a string. Errors are logged but not returned.
func Out(args ...string) string {
if len(args) == 0 {
log.Println("missing name of executable")
return ""
}
path, err := exec.LookPath(args[0])
if err != nil {
log.Println(err)
return ""
}
out, err := exec.Command(path, args[1:]...).Output()
if err != nil {
log.Println(err)
}
return string(out)
}
// IsAdmin checks whether this program is run as a privileged user.
// In windows this will always return false.
func IsAdmin() bool {
currentUser, err := user.Current()
if err != nil {
return false
}
switch runtime.GOOS {
case "windows":
return currentUser.Username == "SYSTEM"
default:
return os.Geteuid() == 0 || currentUser.Username == "root"
}
}
func ShellIsBash() bool {
return strings.Contains(os.Getenv("SHELL"), `bash`)
}
func ShellIsFish() bool {
return len(os.Getenv("FISH_VERSION")) > 0
}
func ShellIsZsh() bool {
return strings.Contains(os.Getenv("SHELL"), `zsh`)
}
func ShellIsPowerShell() bool {
return len(os.Getenv(`PSModulePath`)) > 0
}
// ArgsFrom returns a list of field strings split on space (using
// [strings.Fields]) with an extra trailing special space item appended
// if the line has any trailing spaces at all signifying a definite word
// boundary and not a potential prefix. This is critical for resolving
// completion with [bonzai.Completer].
func ArgsFrom(line string) []string {
args := []string{}
if line == "" {
return args
}
args = strings.Fields(line)
if line[len(line)-1] == ' ' {
args = append(args, "")
}
return args
}
// ArgsOrIn takes a slice or nil as argument and if the slice has any
// length greater than 0 returns all the argument joined together with
// a single space between them. Otherwise, it reads standard input
// until end of file reached (Cntl-D).
func ArgsOrIn(args []string) (string, error) {
if len(args) == 0 {
buf, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err
}
return string(buf), nil
}
return strings.Join(args, " "), nil
}
// FileOrIn takes a string containing a path to a file. If the file does
// not exist (or file is empty string) then read from [os.Stdin].
func FileOrIn(file string) (string, error) {
in := os.Stdin
var err error
if len(file) > 0 {
in, err = os.Open(file)
if err != nil {
return "", err
}
}
buf, err := io.ReadAll(in)
return string(buf), err
}
// AllowPanic disables TrapPanic stopping it from cleaning panic errors.
var AllowPanic = false
// TrapPanic recovers from any panic and more gracefully displays the
// panic by logging it before exiting with a return value of 1.
var TrapPanic = func() {
if !AllowPanic {
if r := recover(); r != nil {
log.Println(r)
os.Exit(1)
}
}
}
// ExitError prints err and exits with 1 return value unless DoNotExit
// has been set to true. Commands should usually never call ExitError
// themselves returning an error from their Method instead.
func ExitError(err ...any) {
switch e := err[0].(type) {
case string:
if len(e) > 1 {
fmt.Fprintf(os.Stderr, e+"\n", err[1:]...)
} else {
fmt.Fprint(os.Stderr, e)
}
case error:
out := fmt.Sprintf("%v", e)
if len(out) > 0 {
fmt.Println(strings.TrimSpace(fmt.Sprint(out)))
}
}
if !DoNotExit {
os.Exit(1)
}
}
// Exit calls os.Exit(0) unless DoNotExit has been set to true. Cmds
// should never call Exit themselves returning a nil error from their
// Methods instead.
func Exit() {
if !DoNotExit {
os.Exit(0)
}
}
// DoNotExit effectively disables Exit and ExitError allowing the
// program to continue running, usually for test evaluation.
var DoNotExit bool
// ExitOff sets DoNotExit to false.
func ExitOff() { DoNotExit = true }
// ExitOn sets DoNotExit to true.
func ExitOn() { DoNotExit = false }
var DefaultInterruptHandler = func() { fmt.Print("\b\b"); Exit() }
// Trap sets up a signal handler for signals that calls the handler.
func Trap(handler func(), signals ...os.Signal) {
if handler == nil {
handler = DefaultInterruptHandler
}
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, signals...)
go func() {
<-signalChannel
handler()
}()
}