1use crate::utils::get_platform;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum ArtifactKind {
18 Gui,
19 Cli,
20 Deb,
21 Rpm,
22}
23
24pub fn release_file_name(
30 version: &str,
31 branch: &str,
32 os: &str,
33 arch: &str,
34 kind: ArtifactKind,
35) -> Option<String> {
36 let file_arch = match (os, arch) {
40 ("android", "aarch64") => "arm64",
41 _ => arch,
42 };
43 let platform = format!("{os}-{file_arch}");
44 let name = match (os, arch, kind) {
45 ("linux", _, ArtifactKind::Cli) => format!("clo-{version}-{branch}-{platform}.tar.gz"),
48 ("linux", "x86_64", ArtifactKind::Deb) => {
49 format!("cloudpub-{version}-{branch}-{platform}.deb")
50 }
51 ("linux", "x86_64", ArtifactKind::Rpm) => {
52 format!("cloudpub-{version}-{branch}-{platform}.rpm")
53 }
54 ("macos", _, ArtifactKind::Gui) => format!("cloudpub-{version}-{branch}-{platform}.dmg"),
56 ("macos", _, ArtifactKind::Cli) => format!("clo-{version}-{branch}-{platform}.tar.gz"),
57 ("windows", "x86_64", ArtifactKind::Gui) => {
59 format!("cloudpub-{version}-{branch}-{platform}.msi")
60 }
61 ("windows", "x86_64", ArtifactKind::Cli) => {
62 format!("clo-{version}-{branch}-{platform}.zip")
63 }
64 ("android", "aarch64", ArtifactKind::Cli) => {
66 format!("clo-{version}-{branch}-{platform}.tar.gz")
67 }
68 _ => return None,
69 };
70 Some(name)
71}
72
73pub fn agent_platform_parts(agent_platform: &str) -> Option<(&'static str, &'static str)> {
80 match agent_platform {
81 "linux-x86_64" => Some(("linux", "x86_64")),
82 "linux-i686" => Some(("linux", "i686")),
83 "linux-aarch64" => Some(("linux", "aarch64")),
84 "linux-armv7" => Some(("linux", "arm")),
86 "linux-mips" => Some(("linux", "mips")),
87 "linux-mipsel" => Some(("linux", "mipsel")),
88 "macos-x86_64" => Some(("macos", "x86_64")),
89 "macos-aarch64" => Some(("macos", "aarch64")),
90 "windows-x86_64" => Some(("windows", "x86_64")),
91 "windows-i686" => Some(("windows", "i686")),
92 "android-aarch64" => Some(("android", "aarch64")),
93 _ => None,
94 }
95}
96
97pub fn current_platform() -> String {
100 get_platform()
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106
107 const V: &str = "3.5.1126";
123 const B: &str = "beta";
124
125 fn name(os: &str, arch: &str, kind: ArtifactKind) -> Option<String> {
126 release_file_name(V, B, os, arch, kind)
127 }
128
129 #[test]
134 fn file_names_are_exactly_the_clients_download_scheme() {
135 assert_eq!(
137 name("macos", "aarch64", ArtifactKind::Gui),
138 Some("cloudpub-3.5.1126-beta-macos-aarch64.dmg".to_string())
139 );
140 assert_eq!(
141 name("macos", "aarch64", ArtifactKind::Cli),
142 Some("clo-3.5.1126-beta-macos-aarch64.tar.gz".to_string())
143 );
144 assert_eq!(
145 name("macos", "x86_64", ArtifactKind::Gui),
146 Some("cloudpub-3.5.1126-beta-macos-x86_64.dmg".to_string())
147 );
148 assert_eq!(
149 name("macos", "x86_64", ArtifactKind::Cli),
150 Some("clo-3.5.1126-beta-macos-x86_64.tar.gz".to_string())
151 );
152 assert_eq!(
154 name("windows", "x86_64", ArtifactKind::Gui),
155 Some("cloudpub-3.5.1126-beta-windows-x86_64.msi".to_string())
156 );
157 assert_eq!(
158 name("windows", "x86_64", ArtifactKind::Cli),
159 Some("clo-3.5.1126-beta-windows-x86_64.zip".to_string())
160 );
161 for arch in [
163 "arm", "armv5te", "aarch64", "mips", "mipsel", "i686", "x86_64",
164 ] {
165 assert_eq!(
166 name("linux", arch, ArtifactKind::Cli),
167 Some(format!("clo-3.5.1126-beta-linux-{arch}.tar.gz")),
168 "the linux {arch} CLI archive name changed"
169 );
170 }
171 assert_eq!(
173 name("linux", "x86_64", ArtifactKind::Deb),
174 Some("cloudpub-3.5.1126-beta-linux-x86_64.deb".to_string())
175 );
176 assert_eq!(
177 name("linux", "x86_64", ArtifactKind::Rpm),
178 Some("cloudpub-3.5.1126-beta-linux-x86_64.rpm".to_string())
179 );
180 assert_eq!(
182 name("android", "aarch64", ArtifactKind::Cli),
183 Some("clo-3.5.1126-beta-android-arm64.tar.gz".to_string())
184 );
185 assert_eq!(name("linux", "x86_64", ArtifactKind::Gui), None);
187 for kind in [ArtifactKind::Deb, ArtifactKind::Rpm] {
188 assert_eq!(name("macos", "aarch64", kind), None);
189 assert_eq!(name("windows", "x86_64", kind), None);
190 assert_eq!(name("android", "aarch64", kind), None);
191 }
192 for kind in [ArtifactKind::Gui, ArtifactKind::Deb, ArtifactKind::Rpm] {
193 assert_eq!(name("linux", "aarch64", kind), None);
194 assert_eq!(name("linux", "arm", kind), None);
195 assert_eq!(name("linux", "i686", kind), None);
196 }
197 assert_eq!(name("windows", "i686", ArtifactKind::Cli), None);
199 assert_eq!(name("plan9", "x86_64", ArtifactKind::Cli), None);
200 }
201
202 #[test]
206 fn agent_platforms_map_to_the_download_selection_pairs() {
207 let cases = [
208 ("linux-x86_64", ("linux", "x86_64")),
209 ("linux-i686", ("linux", "i686")),
210 ("linux-aarch64", ("linux", "aarch64")),
211 ("linux-armv7", ("linux", "arm")),
212 ("linux-mips", ("linux", "mips")),
213 ("linux-mipsel", ("linux", "mipsel")),
214 ("macos-x86_64", ("macos", "x86_64")),
215 ("macos-aarch64", ("macos", "aarch64")),
216 ("windows-x86_64", ("windows", "x86_64")),
217 ("windows-i686", ("windows", "i686")),
218 ("android-aarch64", ("android", "aarch64")),
219 ];
220 for (report, expected) in cases {
221 assert_eq!(
222 agent_platform_parts(report),
223 Some(expected),
224 "the agent report {report} must select the {expected:?} downloads"
225 );
226 }
227 assert_eq!(agent_platform_parts("linux-armv6"), None);
228 assert_eq!(agent_platform_parts(""), None);
229 }
230
231 #[test]
232 fn every_reported_platform_resolves_to_a_download_pair() {
233 for report in [
237 "linux-x86_64",
238 "linux-armv7",
239 "linux-aarch64",
240 "linux-i686",
241 "linux-mips",
242 "linux-mipsel",
243 "windows-x86_64",
244 "windows-i686",
245 "macos-x86_64",
246 "macos-aarch64",
247 "android-aarch64",
248 ] {
249 assert!(
250 agent_platform_parts(report).is_some(),
251 "no download selection for the reported platform {report}"
252 );
253 }
254 }
255}