Skip to content

Commit

Permalink
Merge pull request #857 from planetscale/dbussink/no-custom-signals-i…
Browse files Browse the repository at this point in the history
…f-not-foreground

Disable custom signal handling if not on foreground
  • Loading branch information
dbussink authored Apr 23, 2024
2 parents 823251d + a813f97 commit d8b065d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions internal/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ second argument:

database := args[0]

runForeground := true
if !printer.IsTTY || ch.Printer.Format() != printer.Human {
if _, exists := os.LookupEnv("PSCALE_ALLOW_NONINTERACTIVE_SHELL"); !exists {
return errors.New("pscale shell only works in interactive mode")
}
runForeground = false
}

mysqlPath, err := cmdutil.MySQLClientPath()
Expand Down Expand Up @@ -212,7 +214,7 @@ second argument:
}()

go func() {
errCh <- m.Run(ctx, sigc, signals, mysqlArgs...)
errCh <- m.Run(ctx, sigc, signals, runForeground, mysqlArgs...)
}()

go func() {
Expand Down Expand Up @@ -255,7 +257,7 @@ type mysql struct {
}

// Run runs the `mysql` client with the given arguments.
func (m *mysql) Run(ctx context.Context, sigc chan os.Signal, signals []os.Signal, args ...string) error {
func (m *mysql) Run(ctx context.Context, sigc chan os.Signal, signals []os.Signal, runForeground bool, args ...string) error {
c := exec.CommandContext(ctx, m.mysqlPath, args...)
if m.dir != "" {
c.Dir = m.dir
Expand All @@ -271,10 +273,12 @@ func (m *mysql) Run(ctx context.Context, sigc chan os.Signal, signals []os.Signa
c.Stderr = os.Stderr
c.Stdin = os.Stdin

c.SysProcAttr = sysProcAttr()
cancel := setupSignals(ctx, c, sigc, signals)
if cancel != nil {
defer cancel()
if runForeground {
c.SysProcAttr = sysProcAttr()
cancel := setupSignals(ctx, c, sigc, signals)
if cancel != nil {
defer cancel()
}
}

return c.Run()
Expand Down
3 changes: 2 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"github.com/mattn/go-isatty"
)

var IsTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
var IsTTY = (isatty.IsTerminal(os.Stdout.Fd()) && isatty.IsTerminal(os.Stderr.Fd()) && isatty.IsTerminal(os.Stdin.Fd())) ||
(isatty.IsCygwinTerminal(os.Stdout.Fd()) && isatty.IsCygwinTerminal(os.Stderr.Fd()) && isatty.IsCygwinTerminal(os.Stdin.Fd()))

// Format defines the option output format of a resource.
type Format int
Expand Down

0 comments on commit d8b065d

Please sign in to comment.