run_script/
lib.rs

1#![deny(
2    future_incompatible,
3    keyword_idents,
4    let_underscore,
5    nonstandard_style,
6    unused
7)]
8#![warn(unknown_lints)]
9
10//! # run_script
11//!
12//! Run shell scripts in [rust](https://www.rust-lang.org/).
13//!
14//! This library enables to invoke shell scripts based on their content.<br>
15//! While std::process::Command works great to execute standalone command, you need more manual code to take a script
16//! text and execute it.<br>
17//! For this purpose, this library was created.
18//!
19//! # Examples
20//!
21//! ## Basic Example
22//!
23//! ````use run_script;
24//! use run_script::ScriptOptions;
25//!
26//! fn main() {
27//!     let options = ScriptOptions::new();
28//!
29//!     let args = vec![];
30//!
31//!     // run the script and get the script execution output
32//!     let (code, output, error) = run_script::run(
33//!         r#"
34//!          echo "Directory Info:"
35//!          dir
36//!          "#,
37//!         &args,
38//!         &options,
39//!     )
40//!     .unwrap();
41//!
42//!     println!("Exit Code: {}", code);
43//!     println!("Output: {}", output);
44//!     println!("Error: {}", error);
45//!
46//!     // run the script and get a handle to the running child process
47//!     let child = run_script::spawn(
48//!         r#"
49//!          echo "Directory Info:"
50//!          dir
51//!          "#,
52//!         &args,
53//!         &options,
54//!     )
55//!     .unwrap();
56//!
57//!     let spawn_output = child.wait_with_output().unwrap();
58//!
59//!     println!("Success: {}", &spawn_output.status.success());
60//! }
61//! ````
62//!
63//! ## Macro Examples
64//!
65//! ```rust
66//! use run_script::ScriptOptions;
67//!
68//! fn main() {
69//!     // simple call to run script with only the script text
70//!     let (code, output, error) = run_script::run_script!(
71//!         r#"
72//!          echo "Test"
73//!          exit 0
74//!          "#
75//!     )
76//!     .unwrap();
77//!
78//!     println!("Exit Code: {}", code);
79//!     println!("Output: {}", output);
80//!     println!("Error: {}", error);
81//!
82//!     // run script invoked with the script text and options
83//!     let options = ScriptOptions::new();
84//!     let (code, output, error) = run_script::run_script!(
85//!         r#"
86//!          echo "Test"
87//!          exit 0
88//!          "#,
89//!         &options
90//!     )
91//!     .unwrap();
92//!
93//!     println!("Exit Code: {}", code);
94//!     println!("Output: {}", output);
95//!     println!("Error: {}", error);
96//!
97//!     // run script invoked with all arguments
98//!     let options = ScriptOptions::new();
99//!     let (code, output, error) = run_script::run_script!(
100//!         r#"
101//!          echo "Test"
102//!          exit 0
103//!          "#,
104//!         &vec!["ARG1".to_string(), "ARG2".to_string()],
105//!         &options
106//!     )
107//!     .unwrap();
108//!
109//!     println!("Exit Code: {}", code);
110//!     println!("Output: {}", output);
111//!     println!("Error: {}", error);
112//!
113//!     // spawn_script! works the same as run_script! but returns the child process handle
114//!     let child = run_script::spawn_script!(
115//!         r#"
116//!          echo "Test"
117//!          exit 0
118//!          "#
119//!     )
120//!     .unwrap();
121//!
122//!     println!("PID: {}", child.id());
123//! }
124//! ```
125//!
126//! # Installation
127//! In order to use this library, just add it as a dependency:
128//!
129//! ```ini
130//! [dependencies]
131//! run_script = "*"
132//! ```
133//!
134//! # Contributing
135//! See [contributing guide](https://github.com/sagiegurari/run_script/blob/master/.github/CONTRIBUTING.md)
136//!
137//! # License
138//! Developed by Sagie Gur-Ari and licensed under the
139//! [Apache 2](https://github.com/sagiegurari/run_script/blob/master/LICENSE) open source license.
140//!
141
142#[cfg(test)]
143#[path = "./lib_test.rs"]
144mod lib_test;
145
146#[cfg(doctest)]
147doc_comment::doctest!("../README.md");
148
149#[macro_use]
150mod macros;
151mod runner;
152pub mod types;
153
154use crate::types::ScriptResult;
155use std::process::Child;
156
157/// Error struct
158pub type ScriptError = types::ScriptError;
159
160/// Options available for invoking the script
161pub type ScriptOptions = types::ScriptOptions;
162
163/// Io Options available for invoking the script
164pub type IoOptions = types::IoOptions;
165
166/// Invokes the provided script content and returns the invocation output.
167///
168/// # Arguments
169///
170/// * `script` - The script content
171/// * `args` - The script command line arguments
172/// * `options` - Options provided to the script runner
173///
174/// # Example
175///
176/// ````
177/// use run_script::ScriptOptions;
178///
179/// fn main() {
180///     let options = ScriptOptions::new();
181///
182///     let args = vec![];
183///
184///     let (code, output, error) = run_script::run(
185///         r#"
186///         echo "Directory Info:"
187///         dir
188///         "#,
189///         &args,
190///         &options
191///     ).unwrap();
192///
193///     println!("Exit Code: {}", code);
194///     println!("Output: {}", output);
195///     println!("Error: {}", error);
196/// }
197/// ````
198pub fn run(
199    script: &str,
200    args: &Vec<String>,
201    options: &ScriptOptions,
202) -> ScriptResult<(i32, String, String)> {
203    runner::run(script, &args, &options)
204}
205
206/// Invokes the provided script content and returns a process handle.
207///
208/// # Arguments
209///
210/// * `script` - The script content
211/// * `args` - The script command line arguments
212/// * `options` - Options provided to the script runner
213///
214/// # Example
215///
216/// ````
217/// use run_script::ScriptOptions;
218///
219/// fn main() {
220///     let options = ScriptOptions::new();
221///
222///     let args = vec![];
223///
224///     let child = run_script::spawn(
225///         r#"
226///         echo "Directory Info:"
227///         dir
228///         "#,
229///         &args,
230///         &options
231///     ).unwrap();
232/// }
233/// ````
234pub fn spawn(script: &str, args: &Vec<String>, options: &ScriptOptions) -> ScriptResult<Child> {
235    runner::spawn(script, &args, &options)
236}
237
238/// Invokes the provided script content and returns the invocation output.
239/// In case of invocation error or error exit code, this function will exit the main process.
240///
241/// # Arguments
242///
243/// * `script` - The script content
244/// * `args` - The script command line arguments
245/// * `options` - Options provided to the script runner
246///
247/// # Example
248///
249/// ````
250/// use run_script::ScriptOptions;
251///
252/// fn main() {
253///     let options = ScriptOptions::new();
254///
255///     let args = vec![];
256///
257///     let (output, error) = run_script::run_or_exit(
258///         r#"
259///         echo "Hello World"
260///         "#,
261///         &args,
262///         &options
263///     );
264///
265///     println!("Output: {}", output);
266///     println!("Error: {}", error);
267/// }
268/// ````
269pub fn run_or_exit(script: &str, args: &Vec<String>, options: &ScriptOptions) -> (String, String) {
270    runner::run_or_exit(script, &args, &options)
271}