crud_path/
github.rs

1use std::env;
2use std::fs;
3use std::io::Write;
4
5#[cfg(target_os = "windows")]
6const DELIMITER: &str = ";";
7
8#[cfg(not(target_os = "windows"))]
9const DELIMITER: &str = ":";
10
11pub fn is_github() -> bool {
12    std::env::var("GITHUB_ACTIONS") == Ok("true".to_string())
13}
14
15pub fn add_github_path(input_path: &str) -> Option<String> {
16    if let Ok(file_path) = env::var("GITHUB_PATH") {
17        if !file_path.is_empty() {
18            issue_file_command("PATH", input_path);
19        }
20    }
21
22    let current_path = env::var("PATH").ok()?;
23    let new_path = format!("{}{}{}", input_path, DELIMITER, current_path);
24    env::set_var("PATH", &new_path);
25    Some(new_path)
26}
27
28fn issue_file_command(command: &str, message: &str) {
29    let env_var_name = format!("GITHUB_{}", command);
30    if let Ok(file_path) = env::var(&env_var_name) {
31        if fs::metadata(&file_path).is_err() {
32            panic!("Missing file at path: {}", file_path);
33        }
34
35        let mut file = fs::OpenOptions::new()
36            .append(true)
37            .open(&file_path)
38            .expect("Failed to open file");
39
40        writeln!(file, "{}", message).expect("write file error");
41    } else {
42        panic!(
43            "Unable to find environment variable for file command {}",
44            command
45        );
46    }
47}