-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudo.go
55 lines (44 loc) · 1.22 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
49
50
51
52
53
54
55
package clicommon
import (
"errors"
"fmt"
"os"
)
const sudoArg = "__sudo"
type SudoAction interface {
Name() string
Params() []string
Handle(params []string) error
}
var registeredActions = map[string]SudoAction{}
func RegisterAction(action SudoAction) {
registeredActions[action.Name()] = action
}
// CallSudo asks the user for superuser permissions, and then executes the
// currently-running program with those permissions for a particular action.
// TryHandleSudo should be called at the beginning of the program's main()
// function to catch these sudo calls.
func CallSudo(action SudoAction) error {
return callSudo(action.Name(), action.Params())
}
// TryHandleSudo catches superuser self-executions to do certain actions that
// require superuser permissions
func TryHandleSudo() {
if len(os.Args) >= 3 && os.Args[1] == sudoArg {
action := os.Args[2]
params := os.Args[3:]
err := handleSudo(action, params)
if err != nil {
fmt.Println("Error handling sudo action")
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
}
func handleSudo(action string, params []string) error {
if handler, ok := registeredActions[action]; ok {
return handler.Handle(params)
}
return errors.New("unknown sudo action")
}