diff --git a/pkg/download/abs_test.go b/pkg/download/abs_test.go index 9167b235..56c55e8c 100644 --- a/pkg/download/abs_test.go +++ b/pkg/download/abs_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "os/exec" "path/filepath" "testing" @@ -199,10 +200,19 @@ func Test_getPackageRepoURL(t *testing.T) { func TestABSPKGBUILDRepo(t *testing.T) { t.Parallel() cmdRunner := &testRunner{} + want := "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch -b packages/linux https://github.com/archlinux/svntogit-packages.git linux" + if os.Getuid() == 0 { + ld := "systemd-run" + if path, _ := exec.LookPath(ld); path != "" { + ld = path + } + want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch -b packages/linux https://github.com/archlinux/svntogit-packages.git linux", ld) + } + cmdBuilder := &testGitBuilder{ index: 0, test: t, - want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch -b packages/linux https://github.com/archlinux/svntogit-packages.git linux", + want: want, parentBuilder: &exe.CmdBuilder{ Runner: cmdRunner, GitBin: "/usr/local/bin/git", @@ -224,11 +234,20 @@ func TestABSPKGBUILDRepoExistsPerms(t *testing.T) { os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777) + want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --ff-only", dir) + if os.Getuid() == 0 { + ld := "systemd-run" + if path, _ := exec.LookPath(ld); path != "" { + ld = path + } + want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C %s/linux pull --ff-only", ld, dir) + } + cmdRunner := &testRunner{} cmdBuilder := &testGitBuilder{ index: 0, test: t, - want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --ff-only", dir), + want: want, parentBuilder: &exe.CmdBuilder{ Runner: cmdRunner, GitBin: "/usr/local/bin/git", diff --git a/pkg/download/aur_test.go b/pkg/download/aur_test.go index ce7beadb..b0041ec2 100644 --- a/pkg/download/aur_test.go +++ b/pkg/download/aur_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "os/exec" "path/filepath" "testing" @@ -77,11 +78,20 @@ func TestGetAURPkgbuild(t *testing.T) { // THEN a clone command should be formed func TestAURPKGBUILDRepo(t *testing.T) { t.Parallel() + want := "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin" + if os.Getuid() == 0 { + ld := "systemd-run" + if path, _ := exec.LookPath(ld); path != "" { + ld = path + } + want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin", ld) + } + cmdRunner := &testRunner{} cmdBuilder := &testGitBuilder{ index: 0, test: t, - want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin", + want: want, parentBuilder: &exe.CmdBuilder{ Runner: cmdRunner, GitBin: "/usr/local/bin/git", @@ -103,11 +113,20 @@ func TestAURPKGBUILDRepoExistsPerms(t *testing.T) { os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777) + want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --ff-only", dir) + if os.Getuid() == 0 { + ld := "systemd-run" + if path, _ := exec.LookPath(ld); path != "" { + ld = path + } + want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C %s/yay-bin pull --ff-only", ld, dir) + } + cmdRunner := &testRunner{} cmdBuilder := &testGitBuilder{ index: 0, test: t, - want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --ff-only", dir), + want: want, parentBuilder: &exe.CmdBuilder{ Runner: cmdRunner, GitBin: "/usr/local/bin/git", diff --git a/pkg/settings/config.go b/pkg/settings/config.go index 81fec2ae..d5fe8605 100644 --- a/pkg/settings/config.go +++ b/pkg/settings/config.go @@ -231,8 +231,7 @@ func NewConfig(version string) (*Configuration, error) { newConfig.expandEnv() - errPE := newConfig.setPrivilegeElevator() - if errPE != nil { + if errPE := newConfig.setPrivilegeElevator(); errPE != nil { return nil, errPE } diff --git a/pkg/settings/exe/cmd_builder.go b/pkg/settings/exe/cmd_builder.go index 91402abc..85ddfc5f 100644 --- a/pkg/settings/exe/cmd_builder.go +++ b/pkg/settings/exe/cmd_builder.go @@ -139,15 +139,13 @@ func (c *CmdBuilder) deElevateCommand(ctx context.Context, cmd *exec.Cmd) *exec. } } - path, err := exec.LookPath(cmd.Args[0]) - if err != nil { - panic("path should have already been validated") - } + path, _ := exec.LookPath(cmd.Args[0]) cmdArgs = append(cmdArgs, path) cmdArgs = append(cmdArgs, cmd.Args[1:]...) systemdCmd := exec.CommandContext(ctx, "systemd-run", cmdArgs...) + systemdCmd.Dir = cmd.Dir return systemdCmd }