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
// Copyright (c) 2021 Ethan Lerner, Caleb Cushing, and the Brix contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

//! Contains [MkdirCommand].

use std::fs::create_dir_all;
use std::path::PathBuf;
use validator::Validate;

use crate::{
    command::{Command, ProcessedCommandParams},
    dir,
};
use brix_common::AppContext;
use brix_errors::BrixError;

#[cfg(test)]
mod tests {
    mod from;
    mod run;
}

#[derive(Debug)]
pub struct MkdirParams {
    destination: PathBuf,
}

impl PartialEq for MkdirParams {
    fn eq(&self, other: &Self) -> bool {
        self.destination == other.destination
    }
}

#[derive(Debug, Validate)]
struct Params {
    #[validate(required)]
    destination: Option<PathBuf>,
}

/// The Brix mkdir command
pub struct MkdirCommand {}

impl MkdirCommand {
    pub fn new() -> Self {
        Self {}
    }
}

impl Command for MkdirCommand {
    fn run(&self, pcp: ProcessedCommandParams, ctx: &AppContext) -> Result<(), BrixError> {
        let cp = Params {
            destination: pcp.destination,
        };
        cp.validate()?;

        let dest = dir!(ctx.config.workdir, cp.destination.unwrap());
        create_dir_all(dest)?;

        Ok(())
    }

    fn name(&self) -> String {
        String::from("mkdir")
    }
}