Skip to content

Commit

Permalink
Select package automatically if there's only one
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fcampbell committed Sep 19, 2024
1 parent 5b2c31a commit 4b796b8
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/snowflake/cli/_plugins/nativeapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from snowflake.cli._plugins.nativeapp.application_entity_model import (
ApplicationEntityModel,
)
from snowflake.cli._plugins.nativeapp.application_package_entity_model import (
ApplicationPackageEntityModel,
)
from snowflake.cli._plugins.nativeapp.common_flags import (
ForceOption,
InteractiveOption,
Expand Down Expand Up @@ -338,35 +341,48 @@ def app_teardown(
processor.process(interactive, force, cascade)
else:
# New behaviour, multi-app aware so teardown all the apps created from the package
# in the project definition by copying the implementation of `snow ws drop`
# Determine the package entity to drop, there must be one
app_package_definition: Optional[ApplicationPackageEntityModel] = None
packages: dict[
str, ApplicationPackageEntityModel
] = project.get_entities_by_type(ApplicationPackageEntityModel.get_type())
if package_entity_id:
# If the user specified a package entity ID (or we inferred one from the app entity), use that one directly
app_package_definition = packages.get(package_entity_id)
elif len(packages) == 1:
# Otherwise, if there is only one package entity, fall back to that one
app_package_definition = next(iter(packages.values()))
elif len(packages) > 1:
# If there are multiple package entities, the user must specify which one to use
raise ClickException(
"More than one application package entity exists in the project definition file, "
"specify --package-entity-id to choose which one to operate on."
)

# If we don't have a package entity to convert, error out since it's not optional
if not app_package_definition:
with_id = f'with ID "{package_entity_id}" ' if package_entity_id else ""
raise ClickException(
f"Could not find an application package entity {with_id}in the project definition file."
)

ws = WorkspaceManager(
project_definition=cli_context.project_definition,
project_root=cli_context.project_root,
)
try:
# Make sure the package entity exists in the project definition
ws.get_entity(package_entity_id)
except ValueError:
# Same as what @nativeapp_definition_v2_to_v1 does
raise ClickException(
f'Could not find an application package entity with ID "{package_entity_id}" '
f"in the project definition file."
)
for app_model in project.get_entities_by_type(
ApplicationEntityModel.get_type()
):
for app_id in project.get_entities_by_type(ApplicationEntityModel.get_type()):
# Drop each app
if app_model.from_.target == package_entity_id:
if app_id.from_.target == app_package_definition.entity_id:
ws.perform_action(
app_model.entity_id,
app_id,
EntityActions.DROP,
force_drop=force,
interactive=interactive,
cascade=cascade,
)
# Then drop the package
ws.perform_action(
package_entity_id,
app_package_definition.entity_id,
EntityActions.DROP,
force_drop=force,
interactive=interactive,
Expand Down

0 comments on commit 4b796b8

Please sign in to comment.