Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run child process in foreground on interactive #216

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/hashicorp/vault/api/auth/kubernetes v0.6.0
github.com/samber/lo v1.39.0
github.com/stretchr/testify v1.9.0
golang.org/x/term v0.15.0
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"syscall"
"time"

"github.com/samber/lo"
"golang.org/x/term"
)

var (
Expand Down Expand Up @@ -169,9 +169,9 @@ func run(ctx context.Context, name string, args, env []string, l *slog.Logger) i
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if !lo.Contains([]string{"sh", "bash", "zsh", "fish"}, name) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
}

isTerm := term.IsTerminal(int(os.Stdin.Fd()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎 !!

cmd.SysProcAttr = &syscall.SysProcAttr{Foreground: isTerm, Setsid: !isTerm}

if err := cmd.Start(); err != nil {
l.ErrorContext(ctx, "Could not start command", "error", err, "cmd", cmd.String())
Expand All @@ -187,8 +187,7 @@ func run(ctx context.Context, name string, args, env []string, l *slog.Logger) i

// forward signals to the child process
go func() {
for {
s := <-sigch
for s := range sigch {
if s == syscall.SIGCHLD {
continue
}
Expand Down