hyper_scripter/util/
shebang_handle.rs

1use crate::error::{Error, Result};
2use shlex::Shlex;
3use std::fs::File;
4use std::io::{BufRead, BufReader};
5use std::path::Path;
6
7const SHEBANG: &str = "#!";
8
9pub fn handle(p: &Path) -> Result<(String, Vec<String>)> {
10    let file = File::open(p)?;
11    let buff = BufReader::new(file);
12    if let Some(first_line) = buff.lines().next() {
13        let first_line = first_line?;
14        if first_line.starts_with(SHEBANG) {
15            let mut iter = Shlex::new(&first_line[SHEBANG.len()..]);
16            if let Some(first) = iter.next() {
17                return Ok((first, iter.collect()));
18            }
19        }
20    }
21    Err(Error::PermissionDenied(vec![p.to_path_buf()]))
22}