dockerbuild 0.1.3

Build linux binary under macOS using Docker
use std::env;
use std::path::PathBuf;
use rust_util::XResult;

pub enum Builder {
    Cargo,
    // Maven,
    // Gradle,
    // Buildj,
    Unknown,
}

impl Builder {
    pub fn from(opt_current_dir: &Option<String>) -> XResult<Self> {
        inner_get_builder(opt_current_dir)
    }

    pub fn get_name(&self) -> Option<String> {
        match self {
            Builder::Cargo => Some("cargo".into()),
            // Builder::Maven => Some("mvn".into()),
            // Builder::Gradle => Some("gradle".into()),
            // Builder::Buildj => Some("buildj".into()),
            Builder::Unknown => None,
        }
    }
}

fn inner_get_builder(opt_current_dir: &Option<String>) -> XResult<Builder> {
    let work_dir = get_work_dir(opt_current_dir)?;
    let work_dir_path_buf = PathBuf::from(&work_dir);

    let check_files_exists = |f: &str| -> bool {
        let mut work_dir_path_buf_f = work_dir_path_buf.clone();
        work_dir_path_buf_f.push(f);
        work_dir_path_buf_f.exists()
    };

    if check_files_exists("Cargo.toml") {
        return Ok(Builder::Cargo);
    }
    // if check_files_exists("pom.xml") {
    //     return Ok(Builder::Maven);
    // }
    // if check_files_exists("build.gradle") {
    //     return Ok(Builder::Gradle);
    // }
    // if check_files_exists("build.json") {
    //     return Ok(Builder::Buildj);
    // }

    Ok(Builder::Unknown)
}

pub fn get_work_dir(opt_current_dir: &Option<String>) -> XResult<String> {
    match opt_current_dir {
        Some(dir) => Ok(dir.clone()), None => match env::current_dir() {
            Ok(dir) =>  match dir.as_path().to_str() {
                Some(dir) => Ok(dir.into()), None => Err(rust_util::new_box_error("Error in get current dir!")),
            },
            Err(e) => Err(rust_util::new_box_error(&format!("Error in get current dir: {}", e))),
        },
    }
}