pub struct Script { /* private fields */ }Expand description
Script represents a single script.
It requires at least to set a scripting language and content. Script’s arguments and options are optional.
ScriptingLanguage defines the language in which the script is implemented.
Currently, library supports 8 most popular scripting languages, but it is possible to support a custom ones via ScriptingLanguage::Other.
Script stores its content in a file and then executes Cmd provided by Runnable trait implementation.
Implementations§
Source§impl Script
impl Script
Sourcepub fn new<S>(lang: ScriptingLanguage, content: S) -> Self
pub fn new<S>(lang: ScriptingLanguage, content: S) -> Self
Creates a new script with given scripting language and content.
§Examples
Script::new(ScriptingLanguage::Bash, r#"
user=$(echo $USER)
echo "Hello $user"
"#);Sourcepub fn with_args<S, T, I>(lang: ScriptingLanguage, content: S, args: I) -> Self
pub fn with_args<S, T, I>(lang: ScriptingLanguage, content: S, args: I) -> Self
Creates a new script with given scripting language, content and arguments.
§Examples
Script::with_args(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2", ["arg1", "arg2"]);Sourcepub fn with_options<S>(
lang: ScriptingLanguage,
content: S,
options: CmdOptions,
) -> Self
pub fn with_options<S>( lang: ScriptingLanguage, content: S, options: CmdOptions, ) -> Self
Creates a new script with given scripting language, content and options.
§Examples
let content = r#"
for dir in "$(ls -d */)"; do
echo "$dir"
done
"#;
let options = CmdOptions::with_logging(LoggingType::StdoutOnly);
Script::with_options(ScriptingLanguage::Bash, content, options);Sourcepub fn with_args_and_options<S, T, I>(
lang: ScriptingLanguage,
content: S,
args: I,
options: CmdOptions,
) -> Self
pub fn with_args_and_options<S, T, I>( lang: ScriptingLanguage, content: S, args: I, options: CmdOptions, ) -> Self
Creates a new script with given scripting language, content, arguments and options.
§Examples
let content = r#"
base_dir="$1"
for dir in "$(ls -d $base_dir/*/)"; do
echo "$dir"
done
"#;
let args = vec!["/some/path"];
let options = CmdOptions::with_logging(LoggingType::StdoutOnly);
Script::with_args_and_options(ScriptingLanguage::Bash, content, args, options);Sourcepub fn set_args<S, I>(&mut self, args: I)
pub fn set_args<S, I>(&mut self, args: I)
Set a script arguments.
§Examples
let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
script.set_args(["arg1", "arg2"]);Sourcepub fn set_options(&mut self, options: CmdOptions)
pub fn set_options(&mut self, options: CmdOptions)
Set a script options.
§Examples
let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
script.set_options(CmdOptions::with_standard_io_messaging());Sourcepub fn add_arg<S>(&mut self, arg: S)
pub fn add_arg<S>(&mut self, arg: S)
Add a new argument to the end of argument list.
If arguments was not specified during Script creation, it will create new argument list with given argument.
§Examples
let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
script.add_arg("arg1");
script.add_arg("arg2");Sourcepub fn language(&self) -> &ScriptingLanguage
pub fn language(&self) -> &ScriptingLanguage
Get script language.
Sourcepub fn options(&self) -> &CmdOptions
pub fn options(&self) -> &CmdOptions
Get script options.
Sourcepub fn options_mut(&mut self) -> &mut CmdOptions
pub fn options_mut(&mut self) -> &mut CmdOptions
Update script options via mutable reference.
§Examples
let mut script = Script::new(ScriptingLanguage::Bash, "echo $TEST_ENV_VAR | cut -d ' ' -f2");
script.options_mut().add_env("TEST_ENV_VAR", "example value");