Skip to content

Commit 2c032bd

Browse files
committed
use unified positional-arg option for 'connector' input
1 parent ef24790 commit 2c032bd

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

airbyte_cdk/cli/airbyte_cdk/_connector.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,11 @@ def connector_cli_group() -> None:
101101

102102

103103
@connector_cli_group.command()
104-
@click.option(
105-
"--connector-name",
104+
@click.argument(
105+
"connector",
106+
required=False,
106107
type=str,
107-
help="Name of the connector to test. Ignored if --connector-directory is provided.",
108-
)
109-
@click.option(
110-
"--connector-directory",
111-
type=click.Path(exists=True, file_okay=False, path_type=Path),
112-
help="Path to the connector directory.",
108+
metavar="[CONNECTOR]",
113109
)
114110
@click.option(
115111
"--collect-only",
@@ -118,15 +114,17 @@ def connector_cli_group() -> None:
118114
help="Only collect tests, do not run them.",
119115
)
120116
def test(
121-
connector_name: str | None = None,
122-
connector_directory: Path | None = None,
117+
connector: str | Path | None = None,
123118
*,
124119
collect_only: bool = False,
125120
) -> None:
126121
"""Run connector tests.
127122
128123
This command runs the standard connector tests for a specific connector.
129124
125+
[CONNECTOR] can be a connector name (e.g. 'source-pokeapi'), a path to a connector directory, or omitted to use the current working directory.
126+
If a string containing '/' is provided, it is treated as a path. Otherwise, it is treated as a connector name.
127+
130128
If no connector name or directory is provided, we will look within the current working
131129
directory. If the current working directory is not a connector directory (e.g. starting
132130
with 'source-') and no connector name or path is provided, the process will fail.
@@ -136,10 +134,7 @@ def test(
136134
"pytest is not installed. Please install pytest to run the connector tests."
137135
)
138136
click.echo("Connector test command executed.")
139-
connector_name, connector_directory = resolve_connector_name_and_directory(
140-
connector_name=connector_name,
141-
connector_directory=connector_directory,
142-
)
137+
connector_name, connector_directory = resolve_connector_name_and_directory(connector)
143138

144139
connector_test_suite = create_connector_test_suite(
145140
connector_name=connector_name if not connector_directory else None,

airbyte_cdk/cli/airbyte_cdk/_image.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,7 @@ def build(
5959
)
6060
sys.exit(1)
6161

62-
connector_name = None
63-
connector_directory = None
64-
if connector is None:
65-
# Use cwd logic in resolve_connector_name_and_directory
66-
pass
67-
elif "/" in connector or "\\" in connector:
68-
# Assume it's a path to a connector directory
69-
connector_directory = Path(connector)
70-
else:
71-
# Otherwise, assume it's a connector name
72-
connector_name = connector
73-
74-
connector_name, connector_directory = resolve_connector_name_and_directory(
75-
connector_name=connector_name,
76-
connector_directory=connector_directory,
77-
)
62+
connector_name, connector_directory = resolve_connector_name_and_directory(connector)
7863

7964
metadata_file_path: Path = connector_directory / "metadata.yaml"
8065
try:

airbyte_cdk/cli/airbyte_cdk/_secrets.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,11 @@ def secrets_cli_group() -> None:
7373

7474

7575
@secrets_cli_group.command()
76-
@click.option(
77-
"--connector-name",
76+
@click.argument(
77+
"connector",
78+
required=False,
7879
type=str,
79-
help="Name of the connector to fetch secrets for. Ignored if --connector-directory is provided.",
80-
)
81-
@click.option(
82-
"--connector-directory",
83-
type=click.Path(exists=True, file_okay=False, path_type=Path),
84-
help="Path to the connector directory.",
80+
metavar="[CONNECTOR]",
8581
)
8682
@click.option(
8783
"--gcp-project-id",
@@ -97,8 +93,7 @@ def secrets_cli_group() -> None:
9793
default=False,
9894
)
9995
def fetch(
100-
connector_name: str | None = None,
101-
connector_directory: Path | None = None,
96+
connector: str | Path | None = None,
10297
gcp_project_id: str = AIRBYTE_INTERNAL_GCP_PROJECT,
10398
print_ci_secrets_masks: bool = False,
10499
) -> None:
@@ -107,24 +102,24 @@ def fetch(
107102
This command fetches secrets for a connector from Google Secret Manager and writes them
108103
to the connector's secrets directory.
109104
105+
[CONNECTOR] can be a connector name (e.g. 'source-pokeapi'), a path to a connector directory, or omitted to use the current working directory.
106+
If a string containing '/' is provided, it is treated as a path. Otherwise, it is treated as a connector name.
107+
110108
If no connector name or directory is provided, we will look within the current working
111109
directory. If the current working directory is not a connector directory (e.g. starting
112110
with 'source-') and no connector name or path is provided, the process will fail.
113111
114112
The `--print-ci-secrets-masks` option will print the GitHub CI mask for the secrets.
115113
This is useful for masking secrets in CI logs.
116114
117-
WARNING: This action causes the secrets to be printed in clear text to `STDOUT`. For security
118-
reasons, this function will only execute if the `CI` environment variable is set. Otherwise,
119-
masks will not be printed.
115+
WARNING: The `--print-ci-secrets-masks` option causes the secrets to be printed in clear text to
116+
`STDOUT`. For security reasons, this argument will be ignored if the `CI` environment
117+
variable is not set.
120118
"""
121119
click.echo("Fetching secrets...", err=True)
122120

123121
client = _get_gsm_secrets_client()
124-
connector_name, connector_directory = resolve_connector_name_and_directory(
125-
connector_name=connector_name,
126-
connector_directory=connector_directory,
127-
)
122+
connector_name, connector_directory = resolve_connector_name_and_directory(connector)
128123
secrets_dir = _get_secrets_dir(
129124
connector_directory=connector_directory,
130125
connector_name=connector_name,
@@ -289,21 +284,7 @@ def _get_secrets_dir(
289284
connector_name: str,
290285
ensure_exists: bool = True,
291286
) -> Path:
292-
try:
293-
connector_name, connector_directory = resolve_connector_name_and_directory(
294-
connector_name=connector_name,
295-
connector_directory=connector_directory,
296-
)
297-
except FileNotFoundError as e:
298-
raise FileNotFoundError(
299-
f"Could not find connector directory for '{connector_name}'. "
300-
"Please provide the --connector-directory option with the path to the connector. "
301-
"Note: This command requires either running from within a connector directory, "
302-
"being in the airbyte monorepo, or explicitly providing the connector directory path."
303-
) from e
304-
except ValueError as e:
305-
raise ValueError(str(e))
306-
287+
_ = connector_name # Unused, but it may be used in the future for logging
307288
secrets_dir = connector_directory / "secrets"
308289
if ensure_exists:
309290
secrets_dir.mkdir(parents=True, exist_ok=True)

airbyte_cdk/utils/connector_paths.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,43 @@ def _find_in_adjacent_dirs(current_dir: Path) -> Path | None:
7777

7878

7979
def resolve_connector_name_and_directory(
80-
connector_name: str | None = None,
80+
connector_ref: str | Path | None = None,
81+
*,
8182
connector_directory: Path | None = None,
8283
) -> tuple[str, Path]:
8384
"""Resolve the connector name and directory.
8485
8586
This function will resolve the connector name and directory based on the provided
86-
arguments. If no connector name or directory is provided, it will look within the
87+
reference. If no input ref is provided, it will look within the
8788
current working directory. If the current working directory is not a connector
8889
directory (e.g. starting with 'source-') and no connector name or path is provided,
8990
the process will fail.
91+
If ref is sent as a string containing "/" or "\\", it will be treated as a path to the
92+
connector directory.
93+
94+
raises:
95+
ValueError: If the connector name or directory cannot be resolved.
96+
FileNotFoundError: If the connector directory does not exist or cannot be found.
9097
"""
98+
connector_name: str | None = None
99+
100+
# Resolve connector_ref to connector_name or connector_directory (if provided)
101+
if connector_ref:
102+
if isinstance(connector_ref, str):
103+
if "/" in connector_ref or "\\" in connector_ref:
104+
# If the connector name is a path, treat it as a directory
105+
connector_directory = Path(connector_ref)
106+
else:
107+
# Otherwise, treat it as a connector name
108+
connector_name = connector_ref
109+
elif isinstance(connector_ref, Path):
110+
connector_directory = connector_ref
111+
else:
112+
raise ValueError(
113+
"connector_ref must be a string or Path. "
114+
f"Received type '{type(connector_ref).__name__}': {connector_ref!r}",
115+
)
116+
91117
if not connector_directory:
92118
if connector_name:
93119
connector_directory = find_connector_root_from_name(connector_name)
@@ -97,8 +123,9 @@ def resolve_connector_name_and_directory(
97123
connector_directory = cwd
98124
else:
99125
raise ValueError(
100-
"Either connector_name or connector_directory must be provided if not "
101-
"running from a connector directory."
126+
"The 'connector' input must be provided if not "
127+
"running from a connector directory. "
128+
f"Could not infer connector directory from: {cwd}"
102129
)
103130

104131
if not connector_name:
@@ -109,7 +136,9 @@ def resolve_connector_name_and_directory(
109136
elif connector_name:
110137
connector_directory = find_connector_root_from_name(connector_name)
111138
else:
112-
raise ValueError("Either connector_name or connector_directory must be provided.")
139+
raise ValueError(
140+
f"Could not infer connector_name or connector_directory from input ref: {connector}",
141+
)
113142

114143
return connector_name, connector_directory
115144

0 commit comments

Comments
 (0)