1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
use std::env; use std::ffi::OsStr; use std::io; pub use crate::core::*; pub use crate::logger::*; mod core; mod logger; mod util; trait AssertStdout<T> { fn assert(self) -> T; } impl<T> AssertStdout<T> for io::Result<T> { fn assert(self) -> T { match self { Ok(v) => v, Err(e) => panic!("failed printing to stdout: {}", e), } } } /// Get an action's input parameter. /// /// ``` /// use actions_core as core; /// /// # std::env::set_var("INPUT_MILLISECONDS", "1000"); /// let ms: u32 = core::input("milliseconds") /// .expect("Failed to get milliseconds") /// .parse() /// .expect("Failed to parse milliseconds"); /// ``` pub fn input<K: ToString>(name: K) -> Result<String, env::VarError> { util::var_from_name("INPUT", name) } /// Sets an action's output parameter. /// /// ``` /// use actions_core as core; /// /// let count = 5; /// /// core::set_output("count", 5); /// ``` pub fn set_output<K: ToString, V: ToString>(k: K, v: V) { Core::new().set_output(k, v).assert(); } /// Creates or updates an environment variable for any actions running next /// in a job. Environment variables are immediately set and available to the /// currently running action. Environment variables are case-sensitive and /// you can include punctuation. /// /// ``` /// use actions_core as core; /// /// core::set_env("MY_GREETING", "hello"); /// /// assert_eq!( /// std::env::var_os("MY_GREETING").as_deref(), /// Some(std::ffi::OsStr::new("hello")), /// ); /// ``` pub fn set_env<K: ToString, V: ToString>(k: K, v: V) { Core::new().set_env(k, v).assert(); } /// Masking a value prevents a string or variable from being printed in the /// log. Each masked word separated by whitespace is replaced with the `*` /// character. /// /// ``` /// use actions_core as core; /// /// core::add_mask("supersecret"); /// ``` pub fn add_mask<V: ToString>(v: V) { Core::new().add_mask(v).assert(); } /// Appends a directory to the system PATH variable for all subsequent /// actions in the current job as well as the currently running action. /// /// ``` /// use actions_core as core; /// /// core::add_path("/opt/my-app/bin"); /// ``` pub fn add_path<P: ToString>(v: P) { Core::new().add_path(v).assert(); } /// Similar to `set_output`, but, shares data from a wrapper action. /// /// ``` /// use actions_core as core; /// /// core::save_state("my_greeting", "hello"); /// ``` pub fn save_state<K: ToString, V: ToString>(k: K, v: V) { Core::new().save_state(k, v).assert(); } /// Similar to `input`, but, gets data shared from a wrapper action. /// /// ``` /// use actions_core as core; /// /// let greeting = core::state("my_greeting") /// .unwrap_or_else(|_| "hello".to_owned()); /// ``` pub fn state<K: ToString>(name: K) -> Result<String, env::VarError> { util::var_from_name("STATE", name) } /// Stops processing workflow commands while the provided function runs. A /// token is randomly generated and used to re-enable commands after /// completion. /// /// ``` /// use actions_core as core; /// /// core::stop_logging(|| { /// println!("::set-env name=ignored::value"); /// }); /// ``` pub fn stop_logging<F, T>(f: F) -> T where F: FnOnce() -> T, { Core::new().stop_logging(f).assert() } /// Returns `true` if debugging is enabled Action debugging may be enabled /// by setting a `ACTION_STEP_DEBUG` secret to `true` in the repo. /// /// ``` /// use actions_core as core; /// /// let is_debug = core::is_debug(); /// ``` pub fn is_debug() -> bool { env::var_os("RUNNER_DEBUG").as_deref() == Some(OsStr::new("1")) } /// Prints a debug message to the log. Action debugging may be enabled by /// setting a `ACTION_STEP_DEBUG` secret to `true` in the repo. You can /// optionally provide a `file`, `line` and `col` with the `log_error` /// function. /// /// ``` /// use actions_core as core; /// /// core::debug("shaving a yak"); /// ``` pub fn debug<M: ToString>(message: M) { Core::new().debug(message).assert(); } /// Prints an error message to the log. You can optionally provide a `file`, /// `line` and `col` with the `log_error` function. /// /// ``` /// use actions_core as core; /// /// core::error("shaving a yak"); /// ``` pub fn error<M: ToString>(message: M) { Core::new().error(message).assert(); } /// Prints a warning message to the log. You can optionally provide a `file`, /// `line` and `col` with the `log_warning` function. /// /// ``` /// use actions_core as core; /// /// core::warning("shaving a yak"); /// ``` pub fn warning<M: ToString>(message: M) { Core::new().warning(message).assert(); } pub fn log_message<M: ToString>(level: LogLevel, message: M) { Core::new().log_message(level, message).assert(); } pub fn log<M: ToString>(level: LogLevel, log: Log<M>) { Core::new().log(level, log).assert(); } pub fn log_debug<M: ToString>(log: Log<M>) { Core::new().log_debug(log).assert(); } pub fn log_error<M: ToString>(log: Log<M>) { Core::new().log_error(log).assert(); } pub fn log_warning<M: ToString>(log: Log<M>) { Core::new().log_warning(log).assert(); }