(mycli: 'MyCli', cli_args: 'CliArgs')
| 14 | |
| 15 | |
| 16 | def main_execute_from_cli(mycli: 'MyCli', cli_args: 'CliArgs') -> int: |
| 17 | if cli_args.execute is None: |
| 18 | return 1 |
| 19 | if not sys.stdin.isatty(): |
| 20 | click.secho('Ignoring STDIN since --execute was also given.', err=True, fg='red') |
| 21 | if cli_args.batch: |
| 22 | click.secho('Ignoring --batch since --execute was also given.', err=True, fg='red') |
| 23 | try: |
| 24 | execute_sql = cli_args.execute |
| 25 | if cli_args.format == 'csv': |
| 26 | mycli.main_formatter.format_name = 'csv' |
| 27 | if execute_sql.endswith(r'\G'): |
| 28 | execute_sql = execute_sql[:-2] |
| 29 | elif cli_args.format == 'tsv': |
| 30 | mycli.main_formatter.format_name = 'tsv' |
| 31 | if execute_sql.endswith(r'\G'): |
| 32 | execute_sql = execute_sql[:-2] |
| 33 | elif cli_args.format == 'table': |
| 34 | mycli.main_formatter.format_name = 'ascii' |
| 35 | if execute_sql.endswith(r'\G'): |
| 36 | execute_sql = execute_sql[:-2] |
| 37 | else: |
| 38 | mycli.main_formatter.format_name = 'tsv' |
| 39 | |
| 40 | execution_confirmed: bool | None = True |
| 41 | if cli_args.warn_batch and is_destructive(mycli.destructive_keywords, execute_sql): |
| 42 | try: |
| 43 | sys.stdin = open('/dev/tty') |
| 44 | execution_confirmed = confirm_destructive_query(mycli.destructive_keywords, execute_sql) |
| 45 | except (IOError, OSError) as e: |
| 46 | mycli.logger.warning('Unable to open TTY as stdin.') |
| 47 | raise e |
| 48 | if execution_confirmed: |
| 49 | mycli.run_query(execute_sql, checkpoint=cli_args.checkpoint) |
| 50 | return 0 |
| 51 | else: |
| 52 | return 1 |
| 53 | except Exception as e: |
| 54 | click.secho(str(e), err=True, fg="red") |
| 55 | return 1 |
no test coverage detected