Skip to content

Commit 8b4aff5

Browse files
feat(cli): Enhance validate command with trial migration and granular exit codes
- Add trial migration logic using copy.deepcopy() with no side effects - Implement granular exit codes: 0=valid+current, 1=valid+needs migration, 2=fixable via migration, 3=non-fixable - Document exit codes in command help text - Provide clear feedback about fixable vs non-fixable validation issues Co-Authored-By: AJ Steers <[email protected]>
1 parent f0bca51 commit 8b4aff5

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

airbyte_cdk/cli/airbyte_cdk/_manifest.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Airbyte CDK manifests.
66
"""
77

8+
import copy
89
import sys
910
from importlib import metadata
1011
from pathlib import Path
@@ -42,6 +43,13 @@ def validate_manifest(manifest_path: Path) -> None:
4243
4344
This command validates the manifest file and checks version compatibility.
4445
If validation fails, it will suggest running the migrate command if needed.
46+
47+
Exit codes:
48+
\\b
49+
0: Manifest is valid and up to date
50+
1: Manifest is valid but needs migration, or general errors
51+
2: Manifest has validation errors that are fixable via migration
52+
3: Manifest has validation errors that are NOT fixable via migration
4553
"""
4654
try:
4755
with open(manifest_path, "r") as f:
@@ -56,37 +64,55 @@ def validate_manifest(manifest_path: Path) -> None:
5664

5765
schema = _get_declarative_component_schema()
5866

59-
validate(manifest_dict, schema)
67+
validation_error = None
68+
try:
69+
validate(manifest_dict, schema)
70+
original_is_valid = True
71+
except ValidationError as e:
72+
original_is_valid = False
73+
validation_error = e
6074

61-
migration_handler = ManifestMigrationHandler(manifest_dict)
75+
migration_handler = ManifestMigrationHandler(copy.deepcopy(manifest_dict))
6276
migrated_manifest = migration_handler.apply_migrations()
63-
64-
if migrated_manifest != manifest_dict:
65-
click.echo(
66-
f"⚠️ Manifest {manifest_path} is valid but could benefit from migration to the latest version.",
67-
err=True,
68-
)
69-
click.echo(
70-
"Run 'airbyte-cdk manifest migrate' to apply available migrations.", err=True
71-
)
72-
sys.exit(1)
73-
74-
click.echo(f"✅ Manifest {manifest_path} is valid and up to date.")
77+
78+
migration_available = migrated_manifest != manifest_dict
79+
80+
if original_is_valid:
81+
if migration_available:
82+
click.echo(
83+
f"⚠️ Manifest {manifest_path} is valid but could benefit from migration to the latest version.",
84+
err=True,
85+
)
86+
click.echo(
87+
"Run 'airbyte-cdk manifest migrate' to apply available migrations.", err=True
88+
)
89+
sys.exit(1)
90+
else:
91+
click.echo(f"✅ Manifest {manifest_path} is valid and up to date.")
92+
return
93+
94+
if migration_available:
95+
try:
96+
validate(migrated_manifest, schema)
97+
click.echo(f"❌ Validation failed for {manifest_path}:", err=True)
98+
click.echo(f" {validation_error.message}", err=True)
99+
click.echo("✅ Issues are fixable via migration. Run 'airbyte-cdk manifest migrate' to fix these issues.", err=True)
100+
sys.exit(2) # Fixable issues
101+
except ValidationError:
102+
click.echo(f"❌ Validation failed for {manifest_path}:", err=True)
103+
click.echo(f" {validation_error.message}", err=True)
104+
sys.exit(3) # Non-fixable issues
105+
else:
106+
click.echo(f"❌ Validation failed for {manifest_path}:", err=True)
107+
click.echo(f" {validation_error.message}", err=True)
108+
sys.exit(3) # Non-fixable issues
75109

76110
except FileNotFoundError:
77111
click.echo(f"❌ Error: Manifest file {manifest_path} not found", err=True)
78112
sys.exit(1)
79113
except yaml.YAMLError as e:
80114
click.echo(f"❌ Error: Invalid YAML in {manifest_path}: {e}", err=True)
81115
sys.exit(1)
82-
except ValidationError as e:
83-
click.echo(f"❌ Validation failed for {manifest_path}:", err=True)
84-
click.echo(f" {e.message}", err=True)
85-
click.echo(
86-
"Run 'airbyte-cdk manifest migrate' to apply available migrations that might fix this issue.",
87-
err=True,
88-
)
89-
sys.exit(1)
90116
except Exception as e:
91117
click.echo(f"❌ Unexpected error validating {manifest_path}: {e}", err=True)
92118
sys.exit(1)

0 commit comments

Comments
 (0)