Skip to content

Add --disable-prompts flag to suppress line prompts in non-interactive mode #582

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 22, 2025

This PR addresses the issue where line prompts (like "1>", "2>", "3>") appear in the output when running sqlcmd as a subprocess from another program.

Changes

  • Added a DisablePrompts field to the Sqlcmd struct in pkg/sqlcmd/sqlcmd.go
  • Added a corresponding DisablePrompts field to the SQLCmdArguments struct in cmd/sqlcmd/sqlcmd.go
  • Added a new command-line flag --disable-prompts that allows users to suppress these line prompts
  • Modified the code that writes prompts to the output to check this flag before writing

Example usage

When running sqlcmd as a subprocess with input/output via pipes, users can now add the --disable-prompts flag to get clean output without line prompts:

cat script.sql | sqlcmd --disable-prompts

This matches the behavior of the original sqlcmd tool when used in subprocess scenarios.

Fixes #580.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • someserver
    • Triggering command: /tmp/go-build1426325623/b001/sqlcmd.test -test.testlogfile=/tmp/go-build1426325623/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@shueybubbles
Copy link
Collaborator

shueybubbles commented May 22, 2025

@copilot the liner package appears to incorrectly detect whether the process is using a visible console on Linux. We need a workaround in cmd/sqlcmd/ to turn off interactive mode when stdin is redirected by properly detecting the console state. Perhaps use https://github.com/jbranchaud/til/blob/master/go/detect-if-stdin-comes-from-a-redirect.md as a guide #Closed

@Copilot Copilot AI changed the title [WIP] Removing line prompts when running sqlcmd as a subprocess from a script on Linux Add --disable-prompts flag to suppress line prompts in non-interactive mode May 22, 2025
@Copilot Copilot AI requested a review from shueybubbles May 22, 2025 13:33
Copy link
Author

Copilot AI commented May 22, 2025

@copilot the liner package appears to incorrectly detect whether the process is using a visible console on Linux. We need a workaround in cmd/sqlcmd/ to turn off interactive mode when stdin is red...

I've implemented the fix to properly detect when stdin is redirected. The updated code in isConsoleInitializationRequired now checks the file mode descriptor of os.Stdin.Stat() to determine if input is coming from a terminal or a pipe/redirection. The console initialization is skipped when stdin is redirected, unless a password is required. This should resolve the issue with line prompts appearing when running sqlcmd as a subprocess. Changes are in commit 2c454b5.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cdn.fwupd.org
    • Triggering command: /usr/bin/fwupdmgr refresh (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

#Closed

@@ -438,6 +439,7 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {
rootCmd.Flags().BoolVarP(&args.EnableColumnEncryption, "enable-column-encryption", "g", false, localizer.Sprintf("Enable column encryption"))
rootCmd.Flags().StringVarP(&args.ChangePassword, "change-password", "z", "", localizer.Sprintf("New password"))
rootCmd.Flags().StringVarP(&args.ChangePasswordAndExit, "change-password-exit", "Z", "", localizer.Sprintf("New password and exit"))
rootCmd.Flags().BoolVar(&args.DisablePrompts, "disable-prompts", false, localizer.Sprintf("Disables line prompts when running as a subprocess"))
Copy link
Collaborator

@shueybubbles shueybubbles May 22, 2025

Choose a reason for hiding this comment

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

Remove this new argument #Closed

Copy link
Author

Choose a reason for hiding this comment

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

I've already removed the --disable-prompts flag as part of the implementation of automatic stdin redirection detection in commit 36accd3. The code now automatically detects when input is coming from a pipe or redirection and suppresses prompts accordingly without requiring an explicit flag.

return true
}

// Double-check using isatty
Copy link
Collaborator

@shueybubbles shueybubbles May 23, 2025

Choose a reason for hiding this comment

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

the comment says isatty but the code uses IsTerminal. This file is identical to console_windows.go. If the code for both platforms is identical, there's no need for separate code files. #Closed

Copy link
Author

Choose a reason for hiding this comment

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

I've consolidated the duplicate platform-specific files into a single console_redirect.go file and fixed the comment to correctly refer to term.IsTerminal instead of isatty. The code is now more maintainable with the same functionality. The changes are in commit b9540ca.

@shueybubbles shueybubbles marked this pull request as ready for review May 25, 2025 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing line prompts when running sqlcmd as a subprocess from a script on Linux
2 participants