github_actions/
lib.rs

1mod command;
2mod file_command;
3mod group;
4mod input;
5mod state;
6mod stop_commands;
7mod summary;
8mod vars;
9
10mod macros;
11
12pub use command::*;
13pub use file_command::*;
14pub use group::*;
15pub use input::*;
16pub use state::*;
17pub use stop_commands::*;
18pub use summary::*;
19pub use vars::*;
20
21#[cfg(windows)]
22const EOL: &str = "\r\n";
23#[cfg(not(windows))]
24const EOL: &str = "\n";
25
26fn delimiter() -> String {
27    format!("ghadelimiter_{}", uuid::Uuid::new_v4())
28}
29
30fn does_env_exist(envname: &str) -> bool {
31    std::env::var(envname).is_ok()
32}
33
34pub fn is_debug() -> bool {
35    std::env::var(RUNNER_DEBUG).unwrap_or_default() == "1"
36}
37
38pub fn export_variable<K, V>(key: K, value: V) -> Result<(), FileCommandError>
39where
40    K: AsRef<str>,
41    V: AsRef<str>,
42{
43    if does_env_exist(crate::vars::GITHUB_ENV) {
44        issue_file_command(
45            crate::vars::GITHUB_ENV,
46            &prepare_key_value_message(key, value.as_ref(), delimiter().as_str()).unwrap(),
47        )
48    } else {
49        println!(
50            "{}",
51            Command {
52                command: "set-env",
53                value: value.as_ref(),
54                properties: Some([("name", key.as_ref())].into())
55            }
56        );
57        Ok(())
58    }
59}
60
61pub fn set_output<K, V>(key: K, value: V) -> Result<(), FileCommandError>
62where
63    K: AsRef<str>,
64    V: AsRef<str>,
65{
66    if does_env_exist(crate::vars::GITHUB_OUTPUT) {
67        issue_file_command(
68            crate::vars::GITHUB_OUTPUT,
69            &prepare_key_value_message(key, value.as_ref(), delimiter().as_str()).unwrap(),
70        )
71    } else {
72        println!(
73            "{}",
74            Command {
75                command: "set-output",
76                value: value.as_ref(),
77                properties: Some([("name", key.as_ref())].into())
78            }
79        );
80        Ok(())
81    }
82}
83
84pub fn save_state<K, V>(key: K, value: V) -> Result<(), FileCommandError>
85where
86    K: AsRef<str>,
87    V: AsRef<str>,
88{
89    if does_env_exist(crate::vars::GITHUB_STATE) {
90        issue_file_command(
91            crate::vars::GITHUB_STATE,
92            &prepare_key_value_message(key, value.as_ref(), delimiter().as_str()).unwrap(),
93        )
94    } else {
95        println!(
96            "{}",
97            Command {
98                command: "save-state",
99                value: value.as_ref(),
100                properties: Some([("name", key.as_ref())].into())
101            }
102        );
103        Ok(())
104    }
105}
106
107pub fn add_path<P>(path: P) -> Result<(), FileCommandError>
108where
109    P: AsRef<str>,
110{
111    if does_env_exist(crate::vars::GITHUB_PATH) {
112        issue_file_command(crate::vars::GITHUB_PATH, path.as_ref())
113    } else {
114        println!(
115            "{}",
116            Command {
117                command: "add-path",
118                value: path.as_ref(),
119                properties: None,
120            }
121        );
122        Ok(())
123    }
124}
125
126pub fn set_secret<S>(secret: S)
127where
128    S: AsRef<str>,
129{
130    println!(
131        "{}",
132        Command {
133            command: "add-mask",
134            value: secret.as_ref(),
135            properties: None,
136        }
137    )
138}