-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into interrupt
- Loading branch information
Showing
18 changed files
with
467 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Version=1.48.0 | ||
Version=1.49.0 | ||
MajorVersion=1 | ||
MinorVersion=48 | ||
MinorVersion=49 | ||
PatchVersion=0 | ||
CommitHash=3aa6349c524af72e3eb4eb96321c162f071e69e5 | ||
CommitHash=252a7ebceb348952f19c508ee71a4cb586e8edeb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package platform | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bxcodec/faker/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ARM-software/golang-utils/utils/subprocess/command" | ||
) | ||
|
||
func TestWithPrivileges(t *testing.T) { | ||
admin, err := IsCurrentUserAnAdmin() | ||
require.NoError(t, err) | ||
name := faker.Username() | ||
cmd := WithPrivileges(command.Gosu(name)).RedefineInShellForm("test", "1", "2", "3") | ||
if admin { | ||
assert.Equal(t, fmt.Sprintf("gosu %v test 1 2 3", name), cmd) | ||
} else { | ||
assert.True(t, strings.Contains(cmd, fmt.Sprintf("gosu %v test 1 2 3", name))) | ||
assert.False(t, strings.HasPrefix(cmd, fmt.Sprintf("gosu %v test 1 2 3", name))) | ||
} | ||
cmd = WithPrivileges(nil).RedefineInShellForm("test", "1", "2", "3") | ||
if admin { | ||
assert.Equal(t, "test 1 2 3", cmd) | ||
} else { | ||
assert.True(t, strings.Contains(cmd, "test 1 2 3")) | ||
assert.False(t, strings.HasPrefix(cmd, "test 1 2 3")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package platform | ||
|
||
import "github.com/ARM-software/golang-utils/utils/subprocess/command" | ||
|
||
var ( | ||
// runAsAdmin describes the command to use to run command as Administrator | ||
// See https://ss64.com/nt/syntax-elevate.html | ||
// see https://lazyadmin.nl/it/runas-command/ | ||
// see https://www.tenforums.com/general-support/111929-how-use-runas-without-password-prompt.html | ||
runAsAdmin = command.RunAs("Administrator") | ||
) | ||
|
||
// DefineRunAsAdmin defines the command to run as Administrator. | ||
// e.g. | ||
// - args="runas", "/user:adrien" to run commands as `adrien` | ||
func DefineRunAsAdmin(args ...string) { | ||
runAsAdmin = command.NewCommandAsDifferentUser(args...) | ||
} | ||
|
||
func getRunCommandWithPrivileges() *command.CommandAsDifferentUser { | ||
return runAsAdmin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.