Skip to main content

arcbox_docker_tools/
registry.rs

1//! Download URL construction and extraction logic for each ArcBox-managed host 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        "kubectl" => {
59            let kubectl_arch = match arch {
60                "arm64" => "arm64",
61                _ => "amd64",
62            };
63            format!("https://dl.k8s.io/release/v{version}/bin/darwin/{kubectl_arch}/kubectl")
64        }
65        _ => panic!("unknown host tool: {name}"),
66    }
67}
68
69/// Returns the artifact format for a given tool name.
70#[must_use]
71pub fn artifact_format(name: &str) -> ArtifactFormat {
72    match name {
73        "docker" => ArtifactFormat::Tgz,
74        _ => ArtifactFormat::Binary,
75    }
76}
77
78/// Returns the name of the binary inside the archive for tgz artifacts.
79/// For example, the `docker` tgz contains `docker/docker`.
80#[must_use]
81pub fn tgz_inner_path(name: &str) -> &'static str {
82    match name {
83        "docker" => "docker/docker",
84        _ => panic!("no tgz inner path for {name}"),
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn docker_url_arm64() {
94        let url = download_url("docker", "29.3.1", "arm64");
95        assert_eq!(
96            url,
97            "https://download.docker.com/mac/static/stable/aarch64/docker-29.3.1.tgz"
98        );
99    }
100
101    #[test]
102    fn docker_url_x86() {
103        let url = download_url("docker", "29.3.1", "x86_64");
104        assert_eq!(
105            url,
106            "https://download.docker.com/mac/static/stable/x86_64/docker-29.3.1.tgz"
107        );
108    }
109
110    #[test]
111    fn buildx_url() {
112        let url = download_url("docker-buildx", "0.33.0", "arm64");
113        assert!(url.contains("buildx-v0.33.0.darwin-arm64"));
114    }
115
116    #[test]
117    fn compose_url() {
118        let url = download_url("docker-compose", "5.1.1", "arm64");
119        assert!(url.contains("docker-compose-darwin-aarch64"));
120    }
121
122    #[test]
123    fn credential_url() {
124        let url = download_url("docker-credential-osxkeychain", "0.9.5", "x86_64");
125        assert!(url.contains("docker-credential-osxkeychain-v0.9.5.darwin-amd64"));
126    }
127
128    #[test]
129    fn artifact_formats() {
130        assert_eq!(artifact_format("docker"), ArtifactFormat::Tgz);
131        assert_eq!(artifact_format("docker-buildx"), ArtifactFormat::Binary);
132        assert_eq!(artifact_format("kubectl"), ArtifactFormat::Binary);
133    }
134
135    #[test]
136    fn kubectl_url_arm64() {
137        let url = download_url("kubectl", "1.35.3", "arm64");
138        assert_eq!(
139            url,
140            "https://dl.k8s.io/release/v1.35.3/bin/darwin/arm64/kubectl"
141        );
142    }
143}