github_actions/
lib.rs

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