-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (49 loc) · 1.08 KB
/
main.go
File metadata and controls
68 lines (49 loc) · 1.08 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
package main
import (
"os"
"strings"
"github.com/coalaura/logger"
)
var log = logger.New().WithOptions(logger.Options{
NoTime: true,
})
func main() {
isAdmin, err := isElevated()
if err != nil {
log.Fatalf("Unable to resolve elevation status: %v\n", err)
os.Exit(1)
}
args := os.Args[1:]
if len(args) == 0 {
log.Fatal("Usage: sudo <command>")
os.Exit(1)
}
if isAdmin {
log.Note("Already elevated.")
}
terminal, shell, err := GetCurrentTerminalAndShell()
if err != nil {
log.Fatalf("Unable to resolve terminal/shell: %v\n", err)
os.Exit(1)
}
command := args[0]
args = args[1:]
isSu := command == "su"
isWho := command == "who"
// special "sudo su" handling
if isSu || isWho {
command, args = terminal.Build(shell, "", nil)
if len(command) == 0 {
log.Fatalf("Unable to resolve terminal/shell: %v\n", err)
os.Exit(1)
}
if isWho {
log.Infof("%s %s\n", command, strings.Join(args, " "))
return
}
} else {
command, args = terminal.Build(shell, command, args)
}
cwd, _ := os.Getwd()
elevate(command, strings.Join(args, " "), cwd)
}