Skip to content

Commit

Permalink
Don't hardcode root when running lifecycle scripts
Browse files Browse the repository at this point in the history
Continued from #1313
  • Loading branch information
nrontsis authored and pascalbreuninger committed Oct 23, 2024
1 parent 5f0042a commit ba22fa9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/devcontainer/setup/lifecyclehooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"os/user"
"regexp"
"slices"
"strings"
Expand Down Expand Up @@ -96,8 +97,12 @@ func run(commands []types.LifecycleHook, user, dir string, remoteEnv map[string]

for k, c := range cmd {
log.Infof("Run command %s: %s...", k, strings.Join(c, " "))
currentUser, err := user.Current()
if err != nil {
log.Fatalf("Error fetching the current user: %v", err)
}
args := []string{}
if user != "root" {
if user != currentUser {
args = append(args, "su", user, "-c", command.Quote(c))
} else {
args = append(args, "sh", "-c", command.Quote(c))
Expand Down

0 comments on commit ba22fa9

Please sign in to comment.