Skip to content

Commit

Permalink
feat: create-track command
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Dec 11, 2024
1 parent 0215ac0 commit f049aa9
Show file tree
Hide file tree
Showing 7 changed files with 7,330 additions and 13 deletions.
3 changes: 3 additions & 0 deletions charmcraft/application/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
UploadCommand,
ListRevisionsCommand,
# release process, and show status
CreateTrack,
ReleaseCommand,
PromoteBundleCommand,
StatusCommand,
Expand Down Expand Up @@ -88,6 +89,7 @@ def fill_command_groups(app: craft_application.Application) -> None:
UploadCommand,
ListRevisionsCommand,
# release process, and show status
CreateTrack,
ReleaseCommand,
PromoteBundleCommand,
StatusCommand,
Expand Down Expand Up @@ -142,6 +144,7 @@ def fill_command_groups(app: craft_application.Application) -> None:
"ListNamesCommand",
"UploadCommand",
"ListRevisionsCommand",
"CreateTrack",
"ReleaseCommand",
"PromoteBundleCommand",
"StatusCommand",
Expand Down
67 changes: 67 additions & 0 deletions charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2374,3 +2374,70 @@ def _get_architectures_from_bases(
for architecture in base.architectures:
architectures.add(architecture)
return sorted(architectures)


class CreateTrack(CharmcraftCommand):
"""Create one or more tracks."""

name = "create-track"
help_msg = "Create one or more tracks for a charm on Charmhub"
overview = textwrap.dedent(
"""
Create one or more tracks for a charm on Charmhub. Returns
the full list of tracks for that charm.
For example:
$ charmcraft create-track my-charm track-1 track-2
Name Created at Automatic phasing percentage
--------- -------------------- ------------------------------
track-1 2024-12-10T23:48:40Z
track-2 2024-12-11T00:14:24Z
latest 2023-04-17T23:55:07Z
"""
)
format_option = True

def fill_parser(self, parser: argparse.ArgumentParser) -> None:
"""Add own parameters to the general parser."""
super().fill_parser(parser=parser)
parser.add_argument(
"name",
help="The store name onto which to create the track",
)
parser.add_argument(
"track",
nargs="+",
help="The track name to create",
)
parser.add_argument(
"--automatic-phasing-percentage",
type=int,
default=None,
help="Automatic phasing percentage",
)

def run(self, parsed_args: argparse.Namespace) -> int | None:
"""Run the command."""
emit.progress(f"Creating {len(parsed_args.track)} tracks on the store")
pct = parsed_args.automatic_phasing_percentage
tracks = [
{"name": track, "automatic-phasing-percentage": pct}
for track in parsed_args.track
]
tracks = self._services.store.create_tracks(parsed_args.name, *tracks)

if fmt := parsed_args.format:
emit.message(cli.format_content(tracks, fmt))
return
data = [
{
"Name": track["name"],
"Created at": utils.format_timestamp(
datetime.datetime.fromisoformat(track["created-at"])
),
"Automatic phasing percentage": track["automatic-phasing-percentage"],
}
for track in tracks
]
emit.message(tabulate(data, headers="keys"))
21 changes: 20 additions & 1 deletion charmcraft/services/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import craft_application
import craft_store
from craft_cli import emit
from craft_store import models
from craft_store import models, publishergateway
from overrides import override

from charmcraft import const, env, errors, store
Expand Down Expand Up @@ -193,6 +193,25 @@ def setup(self) -> None:
api_base_url=self._base_url,
storage_base_url=self._storage_url,
)
self._auth = craft_store.Auth(
application_name=self._app.name,
host=self._base_url,
environment_auth=self._environment_auth,
)
self._publisher = craft_store.publishergateway.PublisherGateway(
base_url=self._base_url,
namespace="charm",
auth=self._auth,
)

def create_tracks(
self, name: str, *tracks: publishergateway.CreateTrackRequest
) -> Sequence[publishergateway.TrackMetadata]:
"""Create tracks in the store."""
self._publisher.create_tracks(name, *tracks)

metadata = self._publisher.get_package_metadata(name)
return metadata["tracks"]

def set_resource_revisions_architectures(
self, name: str, resource_name: str, updates: dict[int, list[str]]
Expand Down
4 changes: 2 additions & 2 deletions charmcraft/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import sys
from collections.abc import Collection, Iterable
from dataclasses import dataclass
from typing import Literal, overload
from typing import Any, overload

import tabulate
from craft_cli import emit
Expand Down Expand Up @@ -182,7 +182,7 @@ class OutputFormat(enum.Enum):

@overload
def format_content(
content: dict[str, str], fmt: Literal[OutputFormat.TABLE, "table"]
content: dict[str, Any] | list[dict[str, Any]], fmt: OutputFormat | str | None
) -> str: ...


Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ dependencies = [
"craft-providers>=2.0.0",
"craft-platforms~=0.3",
"craft-providers>=2.0.0",
"craft-store>=3.0.0",
# "craft-store>=3.0.0",
"craft-store @ git+https://github.com/canonical/craft-store@work/charmcraft-1901",
"distro>=1.7.0",
"docker>=7.0.0",
"humanize>=2.6.0",
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ craft-grammar==2.0.1
craft-parts==2.1.3
craft-platforms==0.4.0
craft-providers==2.0.4
craft-store==3.0.2
craft-store @ git+https://github.com/canonical/craft-store@work/charmcraft-1901
cryptography==43.0.3
dill==0.3.9
distro==1.9.0
Expand Down
Loading

0 comments on commit f049aa9

Please sign in to comment.