Skip to content

Commit 0fb01c4

Browse files
Copilotshueybubbles
andcommitted
Update isConsoleInitializationRequired to return both console and interactive status
Co-authored-by: shueybubbles <[email protected]>
1 parent fca3fb9 commit 0fb01c4

3 files changed

Lines changed: 39 additions & 24 deletions

File tree

cmd/sqlcmd/sqlcmd.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -714,25 +714,32 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
714714
}
715715
}
716716

717-
func isConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) bool {
717+
func isConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) (bool, bool) {
718718
// Password input always requires console initialization
719719
if connect.RequiresPassword() {
720-
return true
720+
// Need console for password, but that doesn't mean we're in interactive mode
721+
return true, false
721722
}
722723

723724
// Check if stdin is from a terminal or a redirection
725+
isStdinRedirected := false
724726
file, err := os.Stdin.Stat()
725727
if err == nil {
726728
// If stdin is not a character device, it's coming from a pipe or redirect
727729
if (file.Mode() & os.ModeCharDevice) == 0 {
728-
// Non-interactive: stdin is redirected
729-
return false
730+
isStdinRedirected = true
730731
}
731732
}
732733

733-
// If we get here, stdin is from a terminal or we couldn't determine
734-
iactive := args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0
735-
return iactive
734+
// Determine if we're in interactive mode
735+
iactive := args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0 && !isStdinRedirected
736+
737+
// If stdin is redirected, we don't need a console
738+
if isStdinRedirected {
739+
return false, false
740+
}
741+
742+
return iactive, iactive
736743
}
737744

738745
func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
@@ -744,7 +751,8 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
744751
var connectConfig sqlcmd.ConnectSettings
745752
setConnect(&connectConfig, args, vars)
746753
var line sqlcmd.Console = nil
747-
if isConsoleInitializationRequired(&connectConfig, args) {
754+
needsConsole, isInteractive := isConsoleInitializationRequired(&connectConfig, args)
755+
if needsConsole {
748756
line = console.NewConsole("")
749757
defer line.Close()
750758
}
@@ -835,15 +843,9 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
835843
}
836844
iactive := args.InputFile == nil && args.Query == ""
837845
if iactive || s.Query != "" {
838-
processAll := false
839-
if iactive {
840-
// Check if stdin is a pipe rather than a terminal
841-
fi, _ := os.Stdin.Stat()
842-
if fi != nil && (fi.Mode()&os.ModeCharDevice) == 0 {
843-
// Stdin is not a terminal, it's being piped in
844-
processAll = true
845-
}
846-
}
846+
// If we're not in interactive mode and stdin is redirected,
847+
// we want to process all input without requiring GO statements
848+
processAll := !isInteractive
847849
err = s.Run(once, processAll)
848850
} else {
849851
for f := range args.InputFile {

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ func TestConditionsForPasswordPrompt(t *testing.T) {
506506
setConnect(&connectConfig, &args, vars)
507507
connectConfig.AuthenticationMethod = testcase.authenticationMethod
508508
connectConfig.Password = testcase.pwd
509-
assert.Equal(t, testcase.expectedResult, isConsoleInitializationRequired(&connectConfig, &args), "Unexpected test result encountered for console initialization")
509+
needsConsole, _ := isConsoleInitializationRequired(&connectConfig, &args)
510+
assert.Equal(t, testcase.expectedResult, needsConsole, "Unexpected test result encountered for console initialization")
510511
assert.Equal(t, testcase.expectedResult, connectConfig.RequiresPassword() && connectConfig.Password == "", "Unexpected test result encountered for password prompt conditions")
511512
}
512513
}

cmd/sqlcmd/stdin_console_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,25 @@ func TestIsConsoleInitializationRequiredWithRedirectedStdin(t *testing.T) {
5656
t.Logf("RequiresPassword() returns: %v", connectConfig.RequiresPassword())
5757

5858
// Test with SQL authentication that requires a password
59-
res := isConsoleInitializationRequired(&connectConfig, args)
60-
// Should be true since password is required, even with redirected stdin
61-
assert.True(t, res, "Console initialization should be required when SQL authentication is used")
59+
needsConsole, isInteractive := isConsoleInitializationRequired(&connectConfig, args)
60+
// Should need console since password is required, but not be interactive
61+
assert.True(t, needsConsole, "Console should be needed when SQL authentication is used")
62+
assert.False(t, isInteractive, "Should not be interactive mode with redirected stdin")
6263

6364
// Now test with no authentication (no password required)
6465
connectConfig = sqlcmd.ConnectSettings{}
65-
res = isConsoleInitializationRequired(&connectConfig, args)
66-
// Should be false since stdin is redirected and no password is required
67-
assert.False(t, res, "Console initialization should not be required with redirected stdin and no password")
66+
needsConsole, isInteractive = isConsoleInitializationRequired(&connectConfig, args)
67+
// Should not need console and not be interactive
68+
assert.False(t, needsConsole, "Console should not be needed with redirected stdin and no password")
69+
assert.False(t, isInteractive, "Should not be interactive mode with redirected stdin")
70+
71+
// Test with direct terminal input (simulated by restoring original stdin)
72+
os.Stdin = originalStdin
73+
connectConfig = sqlcmd.ConnectSettings{} // No password needed
74+
needsConsole, isInteractive = isConsoleInitializationRequired(&connectConfig, args)
75+
// If no input file or query is specified, it should be interactive mode
76+
assert.Equal(t, args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0, needsConsole,
77+
"Console needs should match interactive mode requirements with terminal stdin")
78+
assert.Equal(t, args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0, isInteractive,
79+
"Interactive mode should be true with terminal stdin and no input files or queries")
6880
}

0 commit comments

Comments
 (0)