Skip to content

Commit

Permalink
Disable custom signal handling if not on foreground
Browse files Browse the repository at this point in the history
If pscale is used in scripts with the explicit
PSCALE_ALLOW_NONINTERACTIVE_SHELL env flag, disable the signal changes
we do for making the shell work in interactive mode.

This check also needs to verify that all of stdin, stdout & stderr are a
proper TTY and not just stdout, since if you pipe in only input for
example we still need to disable this since there's no foregrounding.
  • Loading branch information
dbussink committed Apr 23, 2024
1 parent 823251d commit a813f97
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 a813f97

Please sign in to comment.