-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudo.go
48 lines (40 loc) · 1.01 KB
/
sudo.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
package main
import (
"fmt"
"os"
"os/exec"
)
// Checks if the user has passwordless sudo access
func hasPasswordlessSudo() bool {
cmd := exec.Command("sudo", "-n", "true")
err := cmd.Run()
return err == nil
}
// Re-run the current executable with sudo
func reRunElevated() error {
if os.Geteuid() == 0 {
return nil
}
if !hasPasswordlessSudo() {
fmt.Println("This operation requires elevated privileges. Requesting sudo...")
}
// Get the path to the current executable
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
// Prepare sudo command with all original arguments
cmd := exec.Command("sudo", append([]string{exe}, os.Args[1:]...)...) //nolint:gosec
// Connect standard IO
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run and wait for completion
err = cmd.Run()
if err != nil {
return fmt.Errorf("sudo execution failed: %w", err)
}
// Exit the original process after sudo execution
os.Exit(0)
return nil
}