Skip to content

Commit d9c1ced

Browse files
committed
lean into pydantic model
1 parent 49c9045 commit d9c1ced

File tree

3 files changed

+28
-95
lines changed

3 files changed

+28
-95
lines changed

airbyte_cdk/cli/airbyte_cdk/_image.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
import click
3838

39+
from airbyte_cdk.models.connector_metadata import MetadataFile
3940
from airbyte_cdk.utils.docker.build import (
4041
build_from_base_image,
4142
build_from_dockerfile,
42-
read_metadata,
4343
set_up_logging,
4444
verify_docker_installation,
4545
verify_image,
@@ -80,11 +80,11 @@ def build(
8080
sys.exit(1)
8181

8282
try:
83-
metadata = read_metadata(connector_directory)
84-
click.echo(f"Connector: {metadata.dockerRepository}")
85-
click.echo(f"Version: {metadata.dockerImageTag}")
83+
metadata = MetadataFile.from_file(connector_directory / "metadata.yaml")
84+
click.echo(f"Connector: {metadata.data.dockerRepository}")
85+
click.echo(f"Version: {metadata.data.dockerImageTag}")
8686

87-
if metadata.language:
87+
if metadata.data.language:
8888
click.echo(f"Connector language from metadata: {metadata.language}")
8989
else:
9090
click.echo("Connector language not specified in metadata")
@@ -111,7 +111,7 @@ def build(
111111
platforms = "linux/amd64"
112112
click.echo(f"Multi-platform build check failed. Building for platform: {platforms}")
113113

114-
if metadata.connectorBuildOptions and metadata.connectorBuildOptions.baseImage:
114+
if metadata.data.connectorBuildOptions and metadata.data.connectorBuildOptions.baseImage:
115115
image_name = build_from_base_image(connector_dir, metadata, tag, platforms)
116116
else:
117117
image_name = build_from_dockerfile(connector_dir, metadata, tag, platforms)

airbyte_cdk/models/connector_metadata.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from __future__ import annotations
44

55
from enum import Enum
6+
from pathlib import Path
67
from typing import Any
78

9+
import yaml
810
from pydantic import BaseModel, Field
911

1012

@@ -54,3 +56,23 @@ class MetadataFile(BaseModel):
5456
model_config = {"extra": "allow"}
5557

5658
data: ConnectorMetadata = Field(..., description="Connector metadata")
59+
60+
@classmethod
61+
def from_file(
62+
cls,
63+
file_path: Path,
64+
) -> MetadataFile:
65+
"""Load metadata from a YAML file."""
66+
if not file_path.exists():
67+
raise FileNotFoundError(f"Metadata file not found: {file_path!s}")
68+
69+
metadata_content = file_path.read_text()
70+
metadata_dict = yaml.safe_load(metadata_content)
71+
72+
if not metadata_dict or "data" not in metadata_dict:
73+
raise ValueError(
74+
"Invalid metadata format: missing 'data' field in YAML file '{file_path!s}'"
75+
)
76+
77+
metadata_file = MetadataFile.model_validate(metadata_dict)
78+
return metadata_file

airbyte_cdk/utils/docker/build.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,6 @@
1818
logger = logging.getLogger("airbyte-cdk.utils.docker.build")
1919

2020

21-
def run_command(
22-
connector_dir: Path,
23-
tag: str = "dev",
24-
platform: str = "linux/amd64",
25-
no_verify: bool = False,
26-
verbose: bool = False,
27-
) -> int:
28-
"""Run the build command with the given arguments.
29-
30-
Args:
31-
connector_dir: Path to the connector directory.
32-
tag: Tag to apply to the built image.
33-
platform: Platform to build for.
34-
no_verify: Whether to skip verification of the built image.
35-
verbose: Whether to enable verbose logging.
36-
37-
Returns:
38-
Exit code (0 for success, non-zero for failure).
39-
"""
40-
set_up_logging(verbose)
41-
42-
if not verify_docker_installation():
43-
logger.error("Docker is not installed or not running. Please install Docker and try again.")
44-
return 1
45-
46-
if not connector_dir.exists():
47-
logger.error(f"Connector directory not found: {connector_dir}")
48-
return 1
49-
50-
try:
51-
metadata = read_metadata(connector_dir)
52-
logger.info(f"Connector: {metadata.dockerRepository}")
53-
logger.info(f"Version: {metadata.dockerImageTag}")
54-
except (FileNotFoundError, ValueError) as e:
55-
logger.error(f"Error reading connector metadata: {e}")
56-
return 1
57-
58-
try:
59-
if metadata.connectorBuildOptions and metadata.connectorBuildOptions.baseImage:
60-
image_name = build_from_base_image(connector_dir, metadata, tag, platform)
61-
else:
62-
image_name = build_from_dockerfile(connector_dir, metadata, tag, platform)
63-
64-
if not no_verify:
65-
if verify_image(image_name):
66-
logger.info(f"Build completed successfully: {image_name}")
67-
return 0
68-
else:
69-
logger.error(f"Built image failed verification: {image_name}")
70-
return 1
71-
else:
72-
logger.info(f"Build completed successfully (without verification): {image_name}")
73-
return 0
74-
75-
except Exception as e:
76-
logger.error(f"Error building Docker image: {e}")
77-
if verbose:
78-
import traceback
79-
logger.error(traceback.format_exc())
80-
return 1
81-
82-
8321
def set_up_logging(verbose: bool = False) -> None:
8422
"""Set up logging configuration.
8523
@@ -94,33 +32,6 @@ def set_up_logging(verbose: bool = False) -> None:
9432
)
9533

9634

97-
def read_metadata(connector_dir: Path) -> ConnectorMetadata:
98-
"""Read and parse connector metadata from metadata.yaml.
99-
100-
Args:
101-
connector_dir: Path to the connector directory.
102-
103-
Returns:
104-
The parsed connector metadata.
105-
106-
Raises:
107-
FileNotFoundError: If the metadata.yaml file doesn't exist.
108-
ValueError: If the metadata is invalid.
109-
"""
110-
metadata_path = connector_dir / "metadata.yaml"
111-
if not metadata_path.exists():
112-
raise FileNotFoundError(f"Metadata file not found: {metadata_path}")
113-
114-
metadata_content = metadata_path.read_text()
115-
metadata_dict = yaml.safe_load(metadata_content)
116-
117-
if not metadata_dict or "data" not in metadata_dict:
118-
raise ValueError("Invalid metadata format: missing 'data' field")
119-
120-
metadata_file = MetadataFile.model_validate(metadata_dict)
121-
return metadata_file.data
122-
123-
12435
def run_docker_command(cmd: List[str], check: bool = True) -> Tuple[int, str, str]:
12536
"""Run a Docker command as a subprocess.
12637

0 commit comments

Comments
 (0)