Browse Source

chore(su): prefer short form if

jguer 3 years atrás
parent
commit
adb74b9252
4 changed files with 45 additions and 10 deletions
  1. 21 2
      pkg/download/abs_test.go
  2. 21 2
      pkg/download/aur_test.go
  3. 1 2
      pkg/settings/config.go
  4. 2 4
      pkg/settings/exe/cmd_builder.go

+ 21 - 2
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",

+ 21 - 2
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",

+ 1 - 2
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
 	}
 

+ 2 - 4
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
 }