summaryrefslogtreecommitdiff
path: root/commands/src/cat.rs
blob: 17d31b81973c787e21e14a1839bf1c108dcb5622 (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
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]);

        if tk.file_exists(&path) {
            if tk.is_file(&path) {
                match tk.read_file(&path) {
                    None => {
                        display_error(tk, Error::E13);
                    }
                    Some(str) => {
                        tk.println(&str.replace('\n', "\r\n").replace("\r\r\n", "\r\n"));
                    }
                }
            } else {
                display_error(tk, Error::E12);
            }
        } else {
            display_error(tk, Error::E04);
        }
    } else if command.names.is_empty() {
        display_error(tk, Error::E03);
    } else if command.names.len() > 1 {
        display_error(tk, Error::E08);
    }
}