-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd.go
22 lines (20 loc) · 866 Bytes
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package platform
import "github.com/ARM-software/golang-utils/utils/subprocess/command"
// WithPrivileges redefines a command so that it is run with elevated privileges.
// For instance, on Linux, if the current user has enough privileges, the command will be run as is.
// Otherwise, `sudo` will be used if defined as the sudo (See `DefineSudoCommand`).
// Similar scenario will happen on Windows, although the elevated command is defined using `DefineSudoCommand`.
func WithPrivileges(cmd *command.CommandAsDifferentUser) (cmdWithPrivileges *command.CommandAsDifferentUser) {
cmdWithPrivileges = cmd
if cmdWithPrivileges == nil {
cmdWithPrivileges = command.NewCommandAsDifferentUser()
}
hasPrivileges, err := IsCurrentUserAnAdmin()
if err != nil {
return
}
if !hasPrivileges {
cmdWithPrivileges.Prepend(getRunCommandWithPrivileges())
}
return
}