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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::{home, input};
use std::env::{consts::OS, current_dir, set_current_dir};
use std::fs;
use std::process::Command;

pub static VERSION: &str = "0.1.0";

pub fn run() {
    println!("Core Shell {}", VERSION);
    println!(
        "You are provided only a few basic commands for your use!
Type 'help' to list them."
    );

    let mut current_directory: String;

    'shellloop: loop {
        current_directory = current_dir()
            .unwrap()
            .into_os_string()
            .into_string()
            .unwrap_or(String::from("~Unknown~"));
        let command = input(&format!("{}>", current_directory));
        let command: Vec<&str> = command.split_ascii_whitespace().collect();

        if command[0].trim() != "" {
            match command[0] {
                "help" => {
                    println!(
                        "A good list of basic commands:
    cd {{dir}}            Change working directory
    rm {{file}}           Remove a file
    rmdir {{dir}}         Remove a Directory (Recursivly if needed)
    exit                Exit the shell
    exec {{bin}} {{args}}   Execute a binary from /bin
    ls                  List current directory
    mkdir {{dir}}         Make a new directory
    mk {{file}}           Make an empty file
    reset               Reset the system to enter setup on next boot"
                    );
                }
                "cd" => {
                    if command.len() >= 2 {
                        let directory_to_switch = command[1..].join(" ");
                        set_current_dir(directory_to_switch).unwrap();
                    } else {
                        println!("Not enough arguments to 'cd'");
                    }
                }
                "rm" => {
                    if command.len() >= 2 {
                        let file_to_remove = command[1..].join(" ");
                        fs::remove_file(file_to_remove).unwrap();
                    } else {
                        println!("Not enough arguments to 'rm'");
                    }
                }
                "rmdir" => {
                    if command.len() >= 2 {
                        let dir_to_remove = command[1..].join(" ");
                        fs::remove_dir_all(dir_to_remove).unwrap();
                    } else {
                        println!("Not enough arguments to 'rmdir'");
                    }
                }
                "exit" => {
                    break 'shellloop;
                }
                "exec" => {
                    if command.len() >= 2 {
                        Command::new(
                            home().clone().to_owned()
                                + &format!("/bin/{}", command[1])
                                + if OS == "windows" { ".exe" } else { "" },
                        )
                        .args(command[1..].iter())
                        .spawn()
                        .unwrap()
                        .wait()
                        .unwrap();
                    } else {
                        println!("Not enough arguments to 'exec'");
                    }
                }
                "ls" => {
                    let paths = fs::read_dir("./").unwrap();

                    for path in paths {
                        println!(
                            "{}: {}",
                            if path.as_ref().unwrap().path().is_dir() {
                                "d"
                            } else {
                                "f"
                            },
                            path.unwrap().path().display()
                        );
                    }
                }
                "mkdir" => {
                    if command.len() >= 2 {
                        let dir_to_make = command[1..].join(" ");
                        fs::create_dir_all(dir_to_make).unwrap();
                    } else {
                        println!("Not enough arguments to 'mkdir'");
                    }
                }
                "mk" => {
                    if command.len() >= 2 {
                        let file_to_make = command[1..].join(" ");
                        fs::write(file_to_make, "").unwrap();
                    } else {
                        println!("Not enough arguments to 'mk'");
                    }
                }
                "reset" => {
                    fs::remove_dir_all(home()).unwrap();
                    println!("Reset Complete! You will enter into configurator on next boot");
                    break 'shellloop;
                }
                _ => {
                    println!("Unknown Command, Use 'help' to list commands");
                }
            }
        } // else just continue
    }
}