summaryrefslogtreecommitdiff
path: root/commands/src/touch.rs
blob: 04ef811717320840fc05660ae42018e60ba19d03 (plain)
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
use alloc::string::String;
use toolkit::Toolkit;
use toolkit::error::*;
use crate::parser::Command;

pub fn run(tk: &mut Toolkit, command: Command) {
    if command.names.len() == 1 {
        let path = tk.resolve_path(&command.names[0]);
        let mut original = String::from("");

        if tk.file_exists(&path) {
            if !tk.is_dir(&path) {
                match tk.read_file(&path) {
                    None => {
                        display_error(tk, Error::E13);
                    }
                    Some(str) => {
                        original = str;
                    }
                }
            } else {
                display_error(tk, Error::E12);
            }
        }

        tk.write_file(&path, &original);
    } else if command.names.is_empty() {
        display_error(tk, Error::E03);
    } else if command.names.len() > 1 {
        display_error(tk, Error::E08);
    }
}