Skip to main content

arcbox_docker_tools/
registry.rs

1//! Download URL construction and extraction logic for each Docker tool.
2
3/// Format of the downloaded artifact.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ArtifactFormat {
6    /// A `.tgz` archive; the `docker` binary lives inside at `docker/docker`.
7    Tgz,
8    /// A bare binary — download and use directly.
9    Binary,
10}
11
12/// Returns the download URL for the given tool name, version, and architecture.
13///
14/// `arch` should be `"arm64"` or `"x86_64"`.
15///
16/// # Panics
17///
18/// Panics if `name` is not a known tool.
19#[must_use]
20pub fn download_url(name: &str, version: &str, arch: &str) -> String {
21    match name {
22        "docker" => {
23            let docker_arch = match arch {
24                "arm64" => "aarch64",
25                _ => "x86_64",
26            };
27            format!(
28                "https://download.docker.com/mac/static/stable/{docker_arch}/docker-{version}.tgz"
29            )
30        }
31        "docker-buildx" => {
32            let gh_arch = match arch {
33                "arm64" => "arm64",
34                _ => "amd64",
35            };
36            format!(
37                "https://github.com/docker/buildx/releases/download/v{version}/buildx-v{version}.darwin-{gh_arch}"
38            )
39        }
40        "docker-compose" => {
41            let compose_arch = match arch {
42                "arm64" => "aarch64",
43                _ => "x86_64",
44            };
45            format!(
46                "https://github.com/docker/compose/releases/download/v{version}/docker-compose-darwin-{compose_arch}"
47            )
48        }
49        "docker-credential-osxkeychain" => {
50            let gh_arch = match arch {
51                "arm64" => "arm64",
52                _ => "amd64",
53            };
54            format!(
55                "https://github.com/docker/docker-credential-helpers/releases/download/v{version}/docker-credential-osxkeychain-v{version}.darwin-{gh_arch}"
56            )
57        }
58        _ => panic!("unknown docker tool: {name}"),
59    }
60}
61
62/// Returns the artifact format for a given tool name.
63#[must_use]
64pub fn artifact_format(name: &str) -> ArtifactFormat {
65    match name {
66        "docker" => ArtifactFormat::Tgz,
67        _ => ArtifactFormat::Binary,
68    }
69}
70
71/// Returns the name of the binary inside the archive for tgz artifacts.
72/// For example, the `docker` tgz contains `docker/docker`.
73#[must_use]
74pub fn tgz_inner_path(name: &str) -> &'static str {
75    match name {
76        "docker" => "docker/docker",
77        _ => panic!("no tgz inner path for {name}"),
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn docker_url_arm64() {
87        let url = download_url("docker", "27.5.1", "arm64");
88        assert_eq!(
89            url,
90            "https://download.docker.com/mac/static/stable/aarch64/docker-27.5.1.tgz"
91        );
92    }
93
94    #[test]
95    fn docker_url_x86() {
96        let url = download_url("docker", "27.5.1", "x86_64");
97        assert_eq!(
98            url,
99            "https://download.docker.com/mac/static/stable/x86_64/docker-27.5.1.tgz"
100        );
101    }
102
103    #[test]
104    fn buildx_url() {
105        let url = download_url("docker-buildx", "0.21.1", "arm64");
106        assert!(url.contains("buildx-v0.21.1.darwin-arm64"));
107    }
108
109    #[test]
110    fn compose_url() {
111        let url = download_url("docker-compose", "2.33.1", "arm64");
112        assert!(url.contains("docker-compose-darwin-aarch64"));
113    }
114
115    #[test]
116    fn credential_url() {
117        let url = download_url("docker-credential-osxkeychain", "0.9.1", "x86_64");
118        assert!(url.contains("docker-credential-osxkeychain-v0.9.1.darwin-amd64"));
119    }
120
121    #[test]
122    fn artifact_formats() {
123        assert_eq!(artifact_format("docker"), ArtifactFormat::Tgz);
124        assert_eq!(artifact_format("docker-buildx"), ArtifactFormat::Binary);
125    }
126}