| 352 | |
| 353 | |
| 354 | def preprocess_cli_args( |
| 355 | cli_args: CliArgs, |
| 356 | is_valid_connection_scheme: Callable[[str], tuple[bool, str | None]], |
| 357 | ) -> int: |
| 358 | if cli_args.database is None and isinstance(cli_args.password, str) and '://' in cli_args.password: |
| 359 | is_valid_scheme, scheme = is_valid_connection_scheme(cli_args.password) |
| 360 | if not is_valid_scheme: |
| 361 | click.secho(f'Error: Unknown connection scheme provided for DSN URI ({scheme}://)', err=True, fg='red') |
| 362 | sys.exit(1) |
| 363 | cli_args.database = cli_args.password |
| 364 | cli_args.password = EMPTY_PASSWORD_FLAG_SENTINEL |
| 365 | |
| 366 | if cli_args.password is None and cli_args.password_file: |
| 367 | password_from_file = get_password_from_file(cli_args.password_file) |
| 368 | if password_from_file is not None: |
| 369 | cli_args.password = password_from_file |
| 370 | |
| 371 | if cli_args.password is None and os.environ.get('MYSQL_PWD') is not None: |
| 372 | cli_args.password = os.environ.get('MYSQL_PWD') |
| 373 | |
| 374 | if cli_args.resume and not cli_args.checkpoint: |
| 375 | click.secho('Error: --resume requires a --checkpoint file.', err=True, fg='red') |
| 376 | sys.exit(1) |
| 377 | |
| 378 | if cli_args.resume and not cli_args.batch: |
| 379 | click.secho('Error: --resume requires a --batch file.', err=True, fg='red') |
| 380 | sys.exit(1) |
| 381 | |
| 382 | if ( |
| 383 | cli_args.checkpoint |
| 384 | and os.path.exists(cli_args.checkpoint) |
| 385 | and cli_args.batch |
| 386 | and cli_args.batch != '-' |
| 387 | and os.path.exists(cli_args.batch) |
| 388 | ): |
| 389 | if os.path.samefile(cli_args.batch, cli_args.checkpoint): |
| 390 | click.secho('Error: --batch and --checkpoint must be different files.', err=True, fg='red') |
| 391 | sys.exit(1) |
| 392 | |
| 393 | if ( |
| 394 | cli_args.logfile |
| 395 | and os.path.exists(cli_args.logfile.name) |
| 396 | and cli_args.batch |
| 397 | and cli_args.batch != '-' |
| 398 | and os.path.exists(cli_args.batch) |
| 399 | ): |
| 400 | if os.path.samefile(cli_args.batch, cli_args.logfile.name): |
| 401 | click.secho('Error: --batch and --logfile must be different files.', err=True, fg='red') |
| 402 | sys.exit(1) |
| 403 | |
| 404 | if cli_args.verbose and cli_args.quiet: |
| 405 | click.secho('Error: --verbose and --quiet are incompatible.', err=True, fg='red') |
| 406 | sys.exit(1) |
| 407 | elif cli_args.verbose: |
| 408 | return int(cli_args.verbose) |
| 409 | elif cli_args.quiet: |
| 410 | return -1 |
| 411 | return 0 |