|
5 | 5 |
|
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
| 8 | +import click |
| 9 | + |
| 10 | +from dda.build.metadata.digests import ArtifactDigest, DigestType |
8 | 11 | from dda.cli.base import dynamic_command, pass_app |
| 12 | +from dda.utils.fs import Path |
9 | 13 |
|
10 | 14 | if TYPE_CHECKING: |
11 | 15 | from dda.cli.application import Application |
12 | 16 |
|
| 17 | +DEFAULT_OUTPUT_PLACEHOLDER = Path("./bin/agent/canonical_filename") |
| 18 | + |
13 | 19 |
|
14 | 20 | @dynamic_command(short_help="Build the `core-agent` binary.") |
| 21 | +@click.option( |
| 22 | + "--output", |
| 23 | + "-o", |
| 24 | + type=click.Path(file_okay=True, dir_okay=False, exists=False, writable=True), |
| 25 | + default=DEFAULT_OUTPUT_PLACEHOLDER, |
| 26 | + help=""" |
| 27 | +The path on which to create the binary. |
| 28 | +Defaults to bin/agent/canonical_filename - the canonical filename of the built artifact. |
| 29 | +This filename contains some metadata about the built artifact, e.g. commit hash, build timestamp, etc. |
| 30 | + """, |
| 31 | +) |
15 | 32 | @pass_app |
16 | | -def cmd(app: Application) -> None: |
| 33 | +def cmd(app: Application, output: Path) -> None: |
| 34 | + import shutil |
| 35 | + |
17 | 36 | from dda.build.artifacts.binaries.core_agent import CoreAgent |
| 37 | + from dda.utils.fs import temp_file |
18 | 38 |
|
19 | 39 | artifact = CoreAgent() |
20 | 40 | app.display_waiting("Building the `core-agent` binary...") |
21 | | - artifact.build(app) |
| 41 | + with temp_file() as tf: |
| 42 | + artifact.build(app, output=tf) |
| 43 | + digest = ArtifactDigest(value=tf.hexdigest(), type=DigestType.FILE_SHA256) |
| 44 | + |
| 45 | + metadata = artifact.compute_metadata(app, digest) |
| 46 | + |
| 47 | + # Special case: if output is the default value, use the canonical filename from the metadata |
| 48 | + if output == DEFAULT_OUTPUT_PLACEHOLDER: |
| 49 | + output = Path("./bin/") / metadata.get_canonical_filename() |
| 50 | + |
| 51 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 52 | + shutil.move(tf, output) |
| 53 | + metadata.to_file(output.with_suffix(".json")) |
0 commit comments