Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aliel committed Apr 29, 2024
1 parent c293aff commit 68bf4ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
36 changes: 17 additions & 19 deletions src/aleph_client/commands/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
from aleph.sdk.exceptions import DomainConfigurationError
from aleph.sdk.query.filters import MessageFilter
from aleph.sdk.types import AccountFromPrivateKey
from aleph_client.commands import help_strings
from aleph_client.commands.utils import is_environment_interactive
from aleph_client.utils import AsyncTyper
from aleph_message.models import AggregateMessage, MessageType
from rich.console import Console
from rich.prompt import Confirm, Prompt
from rich.table import Table

from aleph_client.commands import help_strings
from aleph_client.commands.utils import is_environment_interactive
from aleph_client.utils import AsyncTyper

app = AsyncTyper(no_args_is_help=True)


Expand Down Expand Up @@ -62,7 +61,7 @@ async def attach_resource(
account: AccountFromPrivateKey,
fqdn: Hostname,
item_hash: Optional[str] = None,
spa: Optional[bool] = None,
default_404_page: Optional[str] = None,
interactive: Optional[bool] = None,
):
interactive = is_environment_interactive() if interactive is None else interactive
Expand All @@ -73,10 +72,6 @@ async def attach_resource(
while not item_hash:
item_hash = Prompt.ask("Enter Hash reference of the resource to attach")

while not spa:
is_spa: str = Prompt.ask("It is an SPA application?", choices=["y", "n"], default="n")
spa = True if is_spa == "y" else False

table = Table(title=f"Attach resource to: {fqdn}")
table.add_column("Current resource", justify="right", style="red", no_wrap=True)
table.add_column("New resource", justify="right", style="green", no_wrap=True)
Expand All @@ -87,6 +82,9 @@ async def attach_resource(
"""
resource_type = await get_target_type(fqdn)

if resource_type == TargetType.IPFS:
default_404_page: str = Prompt.ask("Custom 404 page? ex: /404.html", default=None)

if domain_info is not None and domain_info.get("info"):
current_resource = domain_info["info"]["message_id"]
else:
Expand All @@ -111,11 +109,13 @@ async def attach_resource(
"message_id": item_hash,
"type": resource_type,
# console page compatibility
"programType": resource_type,
"spa": "1" if spa else "0",
"programType": resource_type
}
}

if default_404_page and default_404_page.startswith("/"):
aggregate_content["fqdn"]["default_404_page"] = default_404_page

aggregate_message, message_status = await client.create_aggregate(
key="domains", content=aggregate_content, channel="ALEPH-CLOUDSOLUTIONS"
)
Expand Down Expand Up @@ -274,14 +274,14 @@ async def attach(
item_hash: Optional[str] = typer.Option(
None, help=help_strings.CUSTOM_DOMAIN_ITEM_HASH
),
spa: bool = typer.Option(default=False, help=help_strings.IPFS_SPA),
default_404_page: str = typer.Option(default=None, help=help_strings.IPFS_DEFAULT_404_PAGE),
ask: bool = typer.Option(default=True, help=help_strings.ASK_FOR_CONFIRMATION),
):
"""Attach resource to a Custom Domain."""
account: AccountFromPrivateKey = _load_account(private_key, private_key_file)

await attach_resource(
account, Hostname(fqdn), item_hash, interactive=False if (not ask) else None, spa=True if spa else None
account, Hostname(fqdn), item_hash, interactive=False if (not ask) else None, default_404_page=default_404_page
)
raise typer.Exit()

Expand Down Expand Up @@ -331,22 +331,20 @@ async def info(
table.add_column("Resource type", justify="right", style="cyan", no_wrap=True)
table.add_column("Attached resource", justify="right", style="cyan", no_wrap=True)
table.add_column("Target resource", justify="right", style="cyan", no_wrap=True)
table.add_column("SPA", justify="right", style="cyan", no_wrap=True)
table.add_column("Default 404 page", justify="right", style="cyan", no_wrap=True)

resource_type = TargetType(domain_info["info"]["type"])
final_resource = "Unknown"

is_spa = ""
default_404_page = ""
if resource_type == TargetType.IPFS:
final_resource = ""
is_spa = "True" if "spa" in domain_info["info"] and domain_info["info"]["spa"] == "1" else "False"
default_404_page = domain_info["info"].get("default_404_page")
elif resource_type == TargetType.PROGRAM:
final_resource = domain_info["info"]["message_id"]
if resource_type == TargetType.INSTANCE:
ips = await domain_validator.get_ipv6_addresses(Hostname(fqdn))
final_resource = ",".join([str(ip) for ip in ips])

table.add_row(resource_type, domain_info["info"]["message_id"], final_resource, is_spa)

table.add_row(resource_type, domain_info["info"]["message_id"], final_resource, default_404_page)
console.print(table)
raise typer.Exit()
2 changes: 1 addition & 1 deletion src/aleph_client/commands/help_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
Requires at least a "ref" (message hash) and "mount" path. "use_latest" is True by default, to use the latest version of the volume, if it has been amended. See the docs for more info: https://docs.aleph.im/computing/volumes/immutable/\n
Example: --immutable-volume ref=25a393222692c2f73489dc6710ae87605a96742ceef7b91de4d7ec34bb688d94,mount=/lib/python3.8/site-packages"""
ASK_FOR_CONFIRMATION = "Prompt user for confirmation"
IPFS_SPA = "IPFS CID attached is an SPA application"
IPFS_DEFAULT_404_PAGE = "Choose a relative path for a custom 404 error page"

0 comments on commit 68bf4ff

Please sign in to comment.