skydive/
env.rs

1pub fn local_guess_curl_ca_bundle() {
2    // See https://github.com/rust-bio/rust-htslib/issues/404
3    let ca_file = "/etc/ssl/certs/ca-certificates.crt";
4
5    if std::env::var("CURL_CA_BUNDLE").is_err() && std::path::Path::new(ca_file).exists() {
6        std::env::set_var("CURL_CA_BUNDLE", ca_file);
7    }
8}
9
10#[must_use]
11pub fn gcs_gcloud_is_installed() -> bool {
12    // Check if gcloud is installed on the PATH
13    // Suppress stdout and stderr to prevent them from printing to the screen
14    let mut cmd = std::process::Command::new("gcloud");
15    cmd.arg("version")
16        .stdout(std::process::Stdio::null())
17        .stderr(std::process::Stdio::null())
18        .status()
19        .is_ok()
20}
21
22/// Authorize data access to Google Cloud Storage (GCS) using gcloud.
23/// The OAuth token is stored in the environment variable `GCS_OAUTH_TOKEN`.
24///
25/// # Panics
26///
27/// If gcloud is not installed on the PATH.
28pub fn gcs_authorize_data_access() {
29    // Check if gcloud is installed on the PATH
30    if !gcs_gcloud_is_installed() {
31        panic!("gcloud is not installed on the PATH");
32    }
33
34    // Execute the command and capture the output
35    let output = std::process::Command::new("gcloud")
36        .args(["auth", "application-default", "print-access-token"])
37        .output()
38        .expect("Failed to execute command");
39
40    if !output.status.success() {
41        panic!("{}", String::from_utf8_lossy(&output.stderr));
42    }
43
44    // Decode the output and remove trailing newline
45    let token = String::from_utf8(output.stdout)
46        .expect("Failed to decode output")
47        .trim_end()
48        .to_string();
49
50    // Set the environment variable
51    std::env::set_var("GCS_OAUTH_TOKEN", token);
52}