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