-
Notifications
You must be signed in to change notification settings - Fork 578
Add CLI enhancements: -c/--command, -f/--file, -y/--yes flags and .pgpass SSH tunnel support #1538
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
Conversation
Implements the -f/--file command-line option similar to psql's behavior. This allows executing SQL commands from a file and then exiting. Features: - Support for -f and --file (short and long forms) - Multiple files can be specified (-f file1 -f file2) - Can be combined with -c option (-c commands execute first, then -f files) - Pager is disabled in file mode (consistent with -c behavior) - Comprehensive BDD tests added for all scenarios - Version bumped to 4.3.1
When using SSH tunnels, PostgreSQL's .pgpass file was not being used because the connection was using '127.0.0.1' as the hostname instead of the original database hostname. This change preserves the original hostname using PostgreSQL's host/ hostaddr parameters: - host: original database hostname (used for .pgpass lookup and SSL) - hostaddr: 127.0.0.1 (actual connection endpoint via SSH tunnel) Additionally: - Fix connect_uri() to pass DSN parameter for proper .pgpass handling - Add paramiko version constraint to avoid DSSKey compatibility issues - Add SSH tunnel configuration options (ssh_config_file, allow_agent) - Preserve hostaddr parameter when using DSN connections - Add comprehensive test coverage for .pgpass + SSH tunnel scenarios Fixes: SSH tunnel connections now work seamlessly with .pgpass files
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This commit implements a new --tuples-only option similar to psql's -t flag, which prints query results without status messages or timing information. Features: - `-t` or `--tuples-only` without value defaults to csv-noheader format - `-t <format>` allows specifying any table format (e.g., `-t minimal`) - Suppresses "SELECT X" status messages when enabled - Suppresses "Time: X.XXXs" timing output when enabled - Does not affect normal output when option is not used Documentation improvements: - Added missing table formats to pgclirc config file comments: - csv-noheader (CSV without headers) - tsv_noheader (TSV without headers) - csv-tab-noheader (same as tsv_noheader) - minimal (aligned columns without headers or borders) Implementation details: - Added tuples_only parameter to PGCli.__init__() - Added tuples_only field to OutputSettings namedtuple - Modified format_output() to skip status when tuples_only is True - Modified timing output logic to skip when tuples_only is True - All existing tests pass without modifications Example usage: pgcli -t -c "SELECT oid FROM pg_roles WHERE rolname='user';" # Output: 2124219 (nothing else) pgcli -t minimal -c "SELECT oid, rolname FROM pg_roles LIMIT 3;" # Output: aligned columns without headers Made with ❤️ and 🤖 Claude Code
Created comprehensive behavioral tests for the -t/--tuples-only feature: Tests cover: - Basic -t flag usage (default csv-noheader) - Long form --tuples-only - Custom format specification (-t minimal, -t tsv_noheader) - Verification that status messages are suppressed - Verification that timing info is suppressed - Normal mode comparison (without -t) - Multiple rows handling - Special commands with -t Test files: - tests/features/tuples_only.feature: 7 test scenarios - tests/features/steps/tuples_only.py: Step implementations Run tests with: cd tests && behave features/tuples_only.feature Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
Changes: - Removed duplicate step definitions in tuples_only.py that already exist in command_option.py to avoid AmbiguousStep errors - Updated version from 4.3.4 to 4.3.5 All BDD tests passing: 7 scenarios, 32 steps ✓ Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
Added to Upcoming (TBD) section: - New tuples-only option feature description - Documentation section for newly documented table formats Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <[email protected]>
When using the --yes flag to auto-confirm destructive commands, pgcli should not display the "Your call!" message since no user interaction occurred. This message is now only shown when the user manually confirms the destructive warning prompt. Changes: - Modified pgcli/main.py to only show "Your call!" when user manually confirms - Updated tests in tests/features/steps/force_yes.py to verify the message is suppressed - Bumped version to 4.3.6 - Updated changelog.rst with bug fix entry 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…-only-option # Conflicts: # pgcli/__init__.py
Improvements to logging system: - Implement automatic daily log rotation at midnight - Keep 30 days of log history - Change default log location to /var/log/pgcli/pgcli.log - Automatic fallback to ~/.config/pgcli/log if no system permissions - Use TimedRotatingFileHandler for better log management - Log files formatted as: pgcli.log.YYYY-MM-DD Changes: - pgcli/main.py: Added logging.handlers import and updated initialize_logging() - pgcli/pgclirc: Updated documentation for new log behavior - pgcli/__init__.py: Bumped version to 4.3.7 - changelog.rst: Added Internal section entry 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Changed the log file rotation format from pgcli.log.YYYY-MM-DD to pgcli.YYYY-MM-DD.log for better file naming conventions. Changes: - pgcli/main.py: Updated suffix format and added logic to strip .log extension - pgcli/pgclirc: Updated documentation to reflect new format 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
| @then("we see the command output") | ||
| def step_see_command_output(context): | ||
| """Verify that the special command output is present.""" | ||
| output = context.cmd_output.decode('utf-8') |
Check notice
Code scanning / CodeQL
Unused local variable Note test
| # Should contain indicators from both commands | ||
| assert output.count("ALTER TABLE") >= 2, \ | ||
| f"Expected indicators from both ALTER TABLE commands, but got: {output}" | ||
|
|
Check notice
Code scanning / CodeQL
Unused local variable Note test
|
Thanks for your contribution. This pull request has many unsquashed commits and implement multiple features. Would you mind breaking it into multiple pull requests (one per feature)? That would be easier to review (and, possibly, comment and enhance). |
|
Yes, you are right, I will close this PR (and the over, and I will send it again! Sorry & Thank you! |
Summary
This PR adds four major enhancements to pgcli:
-c/--commandsupport: Execute multiple SQL commands sequentially from command line-f/--fileoption: Execute SQL commands from one or more files-y/--yesflag: Bypass confirmation prompts for destructive operations.pgpasssupport with SSH tunnels: Properly use.pgpassauthentication when connecting through SSH tunnelsMotivation
These features improve pgcli's scriptability and automation capabilities:
-cflags allow complex command sequences without temp files-fflag enables SQL script execution similar to psql-yflag removes interactive prompts for CI/CD pipelines.pgpassSSH tunnel support fixes authentication issues when using tunnelsChanges
1. Multiple
-c/--commandparameters (PR #1530)main.pyto accept multiple-coptions2.
-f/--fileoption (PR #1531)--fileoption to execute SQL from files-foptions-cflag (files execute after commands)3.
-y/--yesflag (PR #1533)--yesflag to force destructive commands4.
.pgpassSSH tunnel support (PR #1534).pgpasslookuphost+hostaddrparameters correctlyhostpreserves original hostname for.pgpasshostaddruses 127.0.0.1 for actual connectionpgexecute.pyto supporthostaddrparameterTest Plan
All features include comprehensive tests:
tests/test_main.pytests/test_ssh_tunnel.pyFiles Modified
pgcli/main.py: CLI argument parsing and execution logicpgcli/pgexecute.py: SSH tunnel connection handlingtests/: Comprehensive test coverage for all featureschangelog.rst: Documented changespyproject.toml: Updated dependenciesVersion
Bumped to 4.3.4
🤖 Generated with Claude Code