clickhousectl 0.3.1

The CLI for ClickHouse: local and cloud.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use crate::error::{Error, Result};
use crate::version_manager::list::Channel;
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Os {
    MacOS,
    Linux,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
    X86_64,
    Aarch64,
}

impl fmt::Display for Os {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Os::MacOS => write!(f, "macos"),
            Os::Linux => write!(f, "linux"),
        }
    }
}

impl fmt::Display for Arch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Arch::X86_64 => write!(f, "x86_64"),
            Arch::Aarch64 => write!(f, "aarch64"),
        }
    }
}

/// Detected platform (OS + architecture)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Platform {
    pub os: Os,
    pub arch: Arch,
}

impl Platform {
    /// Detects the current platform
    pub fn detect() -> Result<Self> {
        let os = match std::env::consts::OS {
            "macos" => Os::MacOS,
            "linux" => Os::Linux,
            other => {
                return Err(Error::UnsupportedPlatform {
                    os: other.to_string(),
                    arch: std::env::consts::ARCH.to_string(),
                });
            }
        };

        let arch = match std::env::consts::ARCH {
            "x86_64" => Arch::X86_64,
            "aarch64" => Arch::Aarch64,
            other => {
                return Err(Error::UnsupportedPlatform {
                    os: std::env::consts::OS.to_string(),
                    arch: other.to_string(),
                });
            }
        };

        Ok(Platform { os, arch })
    }

    /// builds.clickhouse.com platform path segment (e.g., "amd64", "macos-aarch64")
    pub fn builds_path(&self) -> &'static str {
        match (self.os, self.arch) {
            (Os::Linux, Arch::X86_64) => "amd64",
            (Os::Linux, Arch::Aarch64) => "aarch64",
            (Os::MacOS, Arch::X86_64) => "macos",
            (Os::MacOS, Arch::Aarch64) => "macos-aarch64",
        }
    }

    /// packages.clickhouse.com arch string (e.g., "amd64", "arm64")
    /// Only valid for Linux — macOS packages are not available
    pub fn packages_arch(&self) -> Option<&'static str> {
        match (self.os, self.arch) {
            (Os::Linux, Arch::X86_64) => Some("amd64"),
            (Os::Linux, Arch::Aarch64) => Some("arm64"),
            (Os::MacOS, _) => None,
        }
    }

    /// GitHub releases arch/suffix for download URLs
    pub fn github_suffix(&self) -> &'static str {
        match (self.os, self.arch) {
            (Os::Linux, Arch::X86_64) => "amd64",
            (Os::Linux, Arch::Aarch64) => "arm64",
            (Os::MacOS, Arch::X86_64) => "x86_64",
            (Os::MacOS, Arch::Aarch64) => "aarch64",
        }
    }
}

/// Where to download a ClickHouse binary from
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DownloadSource {
    /// builds.clickhouse.com — single binary, no extraction needed on any platform
    Builds {
        /// Version path segment: "master", "25.12", etc.
        version_path: String,
    },
    /// packages.clickhouse.com — tgz tarball (Linux only)
    Packages { channel: Channel, version: String },
    /// GitHub releases — tgz (Linux) or bare binary (macOS)
    GitHub { version: String, channel: Channel },
}

impl DownloadSource {
    /// Construct the full download URL for the given platform
    pub fn url(&self, platform: &Platform) -> String {
        match self {
            DownloadSource::Builds { version_path } => {
                format!(
                    "https://builds.clickhouse.com/{}/{}/clickhouse",
                    version_path,
                    platform.builds_path()
                )
            }
            DownloadSource::Packages { channel, version } => {
                let arch = platform
                    .packages_arch()
                    .expect("Packages source should only be used for Linux");
                format!(
                    "https://packages.clickhouse.com/tgz/{}/clickhouse-common-static-{}-{}.tgz",
                    channel, version, arch
                )
            }
            DownloadSource::GitHub { version, channel } => {
                let base = format!(
                    "https://github.com/ClickHouse/ClickHouse/releases/download/v{}-{}",
                    version, channel
                );
                match platform.os {
                    Os::Linux => {
                        format!(
                            "{}/clickhouse-common-static-{}-{}.tgz",
                            base,
                            version,
                            platform.github_suffix()
                        )
                    }
                    Os::MacOS => {
                        format!("{}/clickhouse-macos-{}", base, platform.github_suffix())
                    }
                }
            }
        }
    }

    /// Whether this source downloads a tarball that needs extraction
    pub fn is_tarball(&self, platform: &Platform) -> bool {
        match self {
            DownloadSource::Builds { .. } => false, // Always a single binary
            DownloadSource::Packages { .. } => true, // Always a tgz
            DownloadSource::GitHub { .. } => platform.os == Os::Linux, // tgz on Linux, binary on macOS
        }
    }
}

/// Construct the HEAD-check URL for probing builds.clickhouse.com availability
pub fn builds_probe_url(version_path: &str, platform: &Platform) -> String {
    format!(
        "https://builds.clickhouse.com/{}/{}/clickhouse",
        version_path,
        platform.builds_path()
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    // -- Platform detection --

    #[test]
    fn test_detect_platform() {
        let p = Platform::detect().unwrap();
        assert!(p.os == Os::MacOS || p.os == Os::Linux);
        assert!(p.arch == Arch::X86_64 || p.arch == Arch::Aarch64);
    }

    // -- builds.clickhouse.com paths --

    #[test]
    fn test_builds_path_linux_amd64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        assert_eq!(p.builds_path(), "amd64");
    }

    #[test]
    fn test_builds_path_linux_aarch64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::Aarch64,
        };
        assert_eq!(p.builds_path(), "aarch64");
    }

    #[test]
    fn test_builds_path_macos_x86() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::X86_64,
        };
        assert_eq!(p.builds_path(), "macos");
    }

    #[test]
    fn test_builds_path_macos_aarch64() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        assert_eq!(p.builds_path(), "macos-aarch64");
    }

    // -- packages.clickhouse.com arch --

    #[test]
    fn test_packages_arch_linux() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        assert_eq!(p.packages_arch(), Some("amd64"));
        let p = Platform {
            os: Os::Linux,
            arch: Arch::Aarch64,
        };
        assert_eq!(p.packages_arch(), Some("arm64"));
    }

    #[test]
    fn test_packages_arch_macos_none() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::X86_64,
        };
        assert_eq!(p.packages_arch(), None);
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        assert_eq!(p.packages_arch(), None);
    }

    // -- GitHub suffix --

    #[test]
    fn test_github_suffix() {
        assert_eq!(
            Platform {
                os: Os::Linux,
                arch: Arch::X86_64
            }
            .github_suffix(),
            "amd64"
        );
        assert_eq!(
            Platform {
                os: Os::Linux,
                arch: Arch::Aarch64
            }
            .github_suffix(),
            "arm64"
        );
        assert_eq!(
            Platform {
                os: Os::MacOS,
                arch: Arch::X86_64
            }
            .github_suffix(),
            "x86_64"
        );
        assert_eq!(
            Platform {
                os: Os::MacOS,
                arch: Arch::Aarch64
            }
            .github_suffix(),
            "aarch64"
        );
    }

    // -- URL construction: builds.clickhouse.com --

    #[test]
    fn test_builds_url_master_linux_amd64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let src = DownloadSource::Builds {
            version_path: "master".to_string(),
        };
        assert_eq!(
            src.url(&p),
            "https://builds.clickhouse.com/master/amd64/clickhouse"
        );
    }

    #[test]
    fn test_builds_url_minor_macos_aarch64() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::Builds {
            version_path: "25.12".to_string(),
        };
        assert_eq!(
            src.url(&p),
            "https://builds.clickhouse.com/25.12/macos-aarch64/clickhouse"
        );
    }

    #[test]
    fn test_builds_url_minor_linux_aarch64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::Builds {
            version_path: "25.8".to_string(),
        };
        assert_eq!(
            src.url(&p),
            "https://builds.clickhouse.com/25.8/aarch64/clickhouse"
        );
    }

    // -- URL construction: packages.clickhouse.com --

    #[test]
    fn test_packages_url_stable_linux_amd64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let src = DownloadSource::Packages {
            channel: Channel::Stable,
            version: "25.12.9.61".to_string(),
        };
        assert_eq!(
            src.url(&p),
            "https://packages.clickhouse.com/tgz/stable/clickhouse-common-static-25.12.9.61-amd64.tgz"
        );
    }

    #[test]
    fn test_packages_url_lts_linux_arm64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::Packages {
            channel: Channel::Lts,
            version: "24.8.6.70".to_string(),
        };
        assert_eq!(
            src.url(&p),
            "https://packages.clickhouse.com/tgz/lts/clickhouse-common-static-24.8.6.70-arm64.tgz"
        );
    }

    // -- URL construction: GitHub releases --

    #[test]
    fn test_github_url_linux_amd64() {
        let p = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let src = DownloadSource::GitHub {
            version: "25.12.5.44".to_string(),
            channel: Channel::Stable,
        };
        assert_eq!(
            src.url(&p),
            "https://github.com/ClickHouse/ClickHouse/releases/download/v25.12.5.44-stable/clickhouse-common-static-25.12.5.44-amd64.tgz"
        );
    }

    #[test]
    fn test_github_url_macos_aarch64() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::GitHub {
            version: "25.12.5.44".to_string(),
            channel: Channel::Stable,
        };
        assert_eq!(
            src.url(&p),
            "https://github.com/ClickHouse/ClickHouse/releases/download/v25.12.5.44-stable/clickhouse-macos-aarch64"
        );
    }

    #[test]
    fn test_github_url_macos_x86() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::X86_64,
        };
        let src = DownloadSource::GitHub {
            version: "24.8.6.70".to_string(),
            channel: Channel::Lts,
        };
        assert_eq!(
            src.url(&p),
            "https://github.com/ClickHouse/ClickHouse/releases/download/v24.8.6.70-lts/clickhouse-macos-x86_64"
        );
    }

    // -- Tarball detection --

    #[test]
    fn test_builds_never_tarball() {
        let linux = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let macos = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::Builds {
            version_path: "master".to_string(),
        };
        assert!(!src.is_tarball(&linux));
        assert!(!src.is_tarball(&macos));
    }

    #[test]
    fn test_packages_always_tarball() {
        let linux = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let src = DownloadSource::Packages {
            channel: Channel::Stable,
            version: "25.12.9.61".to_string(),
        };
        assert!(src.is_tarball(&linux));
    }

    #[test]
    fn test_github_tarball_linux_only() {
        let linux = Platform {
            os: Os::Linux,
            arch: Arch::X86_64,
        };
        let macos = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        let src = DownloadSource::GitHub {
            version: "25.12.5.44".to_string(),
            channel: Channel::Stable,
        };
        assert!(src.is_tarball(&linux));
        assert!(!src.is_tarball(&macos));
    }

    // -- Probe URL --

    #[test]
    fn test_builds_probe_url() {
        let p = Platform {
            os: Os::MacOS,
            arch: Arch::Aarch64,
        };
        assert_eq!(
            builds_probe_url("25.12", &p),
            "https://builds.clickhouse.com/25.12/macos-aarch64/clickhouse"
        );
    }
}