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
//! # shebang_script
//!
//! Runs scripts using the executable defined in the shebang line.
//!

#[cfg(test)]
#[path = "shebang_script_test.rs"]
mod shebang_script_test;

use crate::scriptengine::generic_script;

#[derive(Debug, Clone)]
/// Holds flow information
pub(crate) struct Shebang {
    /// The script runner
    pub(crate) runner: Option<String>,
    /// additional arguments
    pub(crate) arguments: Option<Vec<String>>,
}

impl Shebang {
    /// Creates and returns a new instance.
    fn new() -> Shebang {
        Shebang {
            runner: None,
            arguments: None,
        }
    }
}

pub(crate) fn get_shebang(script_text: &Vec<String>) -> Shebang {
    match script_text.first() {
        Some(line) => {
            if line.starts_with("#!") {
                let mut lines = line.split("\n");
                match lines.next() {
                    Some(first_line) => {
                        let mut shebang_line = first_line.replace("#!", "");
                        shebang_line = shebang_line.trim().to_string();

                        if !shebang_line.is_empty() {
                            let mut values = shebang_line.split_whitespace();
                            let runner = match values.next() {
                                Some(value) => Some(value.trim().to_string()),
                                _ => None,
                            };

                            let mut argument_values = vec![];
                            for arg in values {
                                argument_values.push(arg.trim().to_string());
                            }

                            let arguments = if !argument_values.is_empty() {
                                Some(argument_values)
                            } else {
                                None
                            };

                            Shebang { runner, arguments }
                        } else {
                            Shebang::new()
                        }
                    }
                    _ => Shebang::new(),
                }
            } else {
                Shebang::new()
            }
        }
        None => Shebang::new(),
    }
}

pub(crate) fn execute(
    script_text: &Vec<String>,
    extension: &Option<String>,
    cli_arguments: &Vec<String>,
    validate: bool,
) {
    let shebang = get_shebang(&script_text);

    match shebang.runner {
        Some(runner) => {
            let extension_str = match extension {
                Some(value) => value,
                None => "sh",
            };

            generic_script::execute(
                &script_text,
                runner,
                extension_str.to_string(),
                shebang.arguments,
                &cli_arguments,
                validate,
            );
        }
        None => {
            if validate {
                error!("Unable to execute script using shebang.");
            }
        }
    };
}