proc_heim/process/model/
script.rs

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use super::{Cmd, CmdOptions, Runnable};
use std::path::Path;

/// Constant used as a placeholder for a script file path. See [`ScriptRunConfig`] docs.
pub const SCRIPT_FILE_PATH_PLACEHOLDER: &str = "@FILE_PATH";

/// Enum type representing a scripting language.
///
/// `ScriptingLanguage` provides run configuration for 8 most popular scripting languages.
/// If you want to use other language, see [`ScriptingLanguage::Other`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ScriptingLanguage {
    /// Executes script with `bash` command.
    Bash,
    /// Executes script with `python` command.
    Python,
    /// Executes script with `php -f` command.
    Php,
    /// Executes script with `node` command.
    JavaScript,
    /// Executes script with `perl` command.
    Perl,
    /// Executes script with `lua` command.
    Lua,
    /// Executes script with `ruby` command.
    Ruby,
    /// Executes script with `groovy` command.
    Groovy,
    /// Executes script with provided configuration. See [`ScriptRunConfig`] docs.
    Other(ScriptRunConfig),
}

impl From<ScriptingLanguage> for ScriptRunConfig {
    fn from(value: ScriptingLanguage) -> Self {
        match value {
            ScriptingLanguage::Bash => {
                ScriptRunConfig::new("bash", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "sh")
            }
            ScriptingLanguage::Python => {
                ScriptRunConfig::new("python", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "py")
            }
            ScriptingLanguage::Php => {
                ScriptRunConfig::new("php", vec!["-f", SCRIPT_FILE_PATH_PLACEHOLDER], "php")
            }
            ScriptingLanguage::JavaScript => {
                ScriptRunConfig::new("node", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "js")
            }
            ScriptingLanguage::Perl => {
                ScriptRunConfig::new("perl", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "pl")
            }
            ScriptingLanguage::Lua => {
                ScriptRunConfig::new("lua", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "lua")
            }
            ScriptingLanguage::Ruby => {
                ScriptRunConfig::new("ruby", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "rb")
            }
            ScriptingLanguage::Groovy => {
                ScriptRunConfig::new("groovy", vec![SCRIPT_FILE_PATH_PLACEHOLDER], "groovy")
            }
            ScriptingLanguage::Other(run_config) => run_config,
        }
    }
}

/// `ScriptRunConfig` allows to define own configuration used to run a script.
///
/// It describes command name, its arguments needed to run a script and also
/// a file extension typical for a given scripting language.
/// # Examples
/// Run configuration for PHP language (equivalent to [`ScriptingLanguage::Php`]):
/// ```
/// use proc_heim::model::script::ScriptRunConfig;
/// use proc_heim::model::script::SCRIPT_FILE_PATH_PLACEHOLDER;
///
/// ScriptRunConfig::new("php", ["-f", SCRIPT_FILE_PATH_PLACEHOLDER], "php");
///
/// ```
/// [`SCRIPT_FILE_PATH_PLACEHOLDER`] constant is used to mark that in this argument should be a path to a script file.
/// Before spawning a script, the placeholder will be replaced by proper file path to the script (with extension provided in `file_extension` argument).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScriptRunConfig {
    cmd: String,
    args: Vec<String>,
    file_extension: String,
}

impl ScriptRunConfig {
    /// Creates a new run configuration.
    pub fn new<C, T, I, F>(cmd: C, args: I, file_extension: F) -> Self
    where
        C: Into<String>,
        T: Into<String>,
        I: IntoIterator<Item = T>,
        F: Into<String>,
    {
        Self {
            cmd: cmd.into(),
            args: args.into_iter().map(Into::into).collect(),
            file_extension: file_extension.into(),
        }
    }

    pub(crate) fn replace_path_placeholder(&mut self, file_path: &str) {
        self.args = self
            .args
            .iter()
            .map(|arg| {
                if arg == SCRIPT_FILE_PATH_PLACEHOLDER {
                    file_path
                } else {
                    arg
                }
                .to_owned()
            })
            .collect();
    }
}

/// `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`](struct@crate::model::command::Cmd) provided by [`Runnable`](trait@crate::model::Runnable) trait implementation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Script {
    pub(crate) lang: ScriptingLanguage,
    pub(crate) content: String,
    pub(crate) args: Option<Vec<String>>,
    pub(crate) options: CmdOptions,
}

impl Script {
    /// Creates a new script with given scripting language and content.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// Script::new(ScriptingLanguage::Bash, r#"
    ///     user=$(echo $USER)
    ///     echo "Hello $user"
    /// "#);
    /// ```
    pub fn new<S>(lang: ScriptingLanguage, content: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            lang,
            content: content.into(),
            args: None,
            options: CmdOptions::default(),
        }
    }

    /// Creates a new script with given scripting language, content and arguments.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// Script::with_args(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2", ["arg1", "arg2"]);
    /// ```
    pub fn with_args<S, T, I>(lang: ScriptingLanguage, content: S, args: I) -> Self
    where
        S: Into<String>,
        T: Into<String>,
        I: IntoIterator<Item = T>,
    {
        Self {
            lang,
            content: content.into(),
            args: Some(args.into_iter().map(Into::into).collect()),
            options: CmdOptions::default(),
        }
    }

    /// Creates a new script with given scripting language, content and options.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// # use proc_heim::model::command::*;
    /// 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);
    /// ```
    pub fn with_options<S>(lang: ScriptingLanguage, content: S, options: CmdOptions) -> Self
    where
        S: Into<String>,
    {
        Self {
            lang,
            content: content.into(),
            args: None,
            options,
        }
    }

    /// Creates a new script with given scripting language, content, arguments and options.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// # use proc_heim::model::command::*;
    /// 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);
    /// ```
    pub fn with_args_and_options<S, T, I>(
        lang: ScriptingLanguage,
        content: S,
        args: I,
        options: CmdOptions,
    ) -> Self
    where
        S: Into<String>,
        T: Into<String>,
        I: IntoIterator<Item = T>,
    {
        Self {
            lang,
            content: content.into(),
            args: Some(args.into_iter().map(Into::into).collect()),
            options,
        }
    }

    /// Set a script arguments.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
    /// script.set_args(["arg1", "arg2"]);
    /// ```
    pub fn set_args<S, I>(&mut self, args: I)
    where
        S: Into<String>,
        I: IntoIterator<Item = S>,
    {
        self.args = Some(args.into_iter().map(Into::into).collect());
    }

    /// Set a script options.
    /// # Examples
    /// ```
    /// # use proc_heim::model::script::*;
    /// # use proc_heim::model::command::*;
    /// let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
    /// script.set_options(CmdOptions::with_standard_io_messaging());
    /// ```
    pub fn set_options(&mut self, options: CmdOptions) {
        self.options = options;
    }

    /// 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
    /// ```
    /// # use proc_heim::model::script::*;
    /// # use proc_heim::model::command::*;
    /// let mut script = Script::new(ScriptingLanguage::Bash, "echo $@ | cut -d ' ' -f2");
    /// script.add_arg("arg1");
    /// script.add_arg("arg2");
    /// ```
    pub fn add_arg<S>(&mut self, arg: S)
    where
        S: Into<String>,
    {
        self.args.get_or_insert(Vec::new()).push(arg.into());
    }

    /// Update script options via mutable reference.
    /// # Examples
    /// ```
    /// # use proc_heim::model::command::*;
    /// # use proc_heim::model::script::*;
    /// let mut script = Script::new(ScriptingLanguage::Bash, "echo $TEST_ENV_VAR | cut -d ' ' -f2");
    /// script.options_mut().add_env("TEST_ENV_VAR", "example value");
    /// ```
    pub fn options_mut(&mut self) -> &mut CmdOptions {
        &mut self.options
    }
}

impl Runnable for Script {
    fn bootstrap_cmd(&self, process_dir: &Path) -> Result<Cmd, String> {
        let mut run_config: ScriptRunConfig = self.lang.clone().into();
        let file_path = create_script_file(self, &run_config, process_dir)?;
        run_config.replace_path_placeholder(&file_path);

        if let Some(arguments) = &self.args {
            run_config.args.extend_from_slice(arguments);
        }

        let cmd = Cmd {
            cmd: run_config.cmd,
            args: run_config.args.into(),
            options: self.options.clone(),
        };
        Ok(cmd)
    }
}

fn create_script_file(
    script: &Script,
    run_config: &ScriptRunConfig,
    script_file_dir: &Path,
) -> Result<String, String> {
    let file_path = script_file_dir
        .join("script")
        .with_extension(&run_config.file_extension);
    std::fs::write(&file_path, &script.content).map_err(|err| err.to_string())?;
    file_path
        .to_str()
        .ok_or("Script file path cannot be converted to UTF-8 string".to_owned())
        .map(|v| v.to_owned())
}