File size: 8,003 Bytes
dcbeeb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
extern crate alloc;

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use spin::Mutex;

#[derive(Debug, Clone)]
pub struct DebianPackage {
    pub name: &'static str,
    pub version: &'static str,
    pub size_kb: usize,
    pub description: &'static str,
    pub is_installed: bool,
}

pub static DEBIAN_REPO: Mutex<[DebianPackage; 10]> = Mutex::new([
    DebianPackage { name: "neofetch", version: "7.1.0-4", size_kb: 128, description: "Fast, highly customizable system info script", is_installed: false },
    DebianPackage { name: "curl", version: "7.88.1-10", size_kb: 420, description: "Command line tool for transferring data with URL syntax", is_installed: false },
    DebianPackage { name: "htop", version: "3.2.2-1", size_kb: 310, description: "Interactive high-resolution process viewer", is_installed: false },
    DebianPackage { name: "vim", version: "9.0.1378-2", size_kb: 1450, description: "Vi IMproved - enhanced vi text editor", is_installed: false },
    DebianPackage { name: "git", version: "2.39.2-1", size_kb: 6800, description: "Fast, scalable, distributed revision control system", is_installed: false },
    DebianPackage { name: "python3", version: "3.11.2-1", size_kb: 14200, description: "Interactive high-level object-oriented language", is_installed: false },
    DebianPackage { name: "rustc", version: "1.70.0-1", size_kb: 48000, description: "Rust systems programming language compiler", is_installed: false },
    DebianPackage { name: "gcc", version: "12.2.0-14", size_kb: 32000, description: "GNU C compiler suite", is_installed: false },
    DebianPackage { name: "doom", version: "1.10-3", size_kb: 4500, description: "Classic retro 1993 3D arcade first-person shooter", is_installed: false },
    DebianPackage { name: "snapd", version: "2.58.3-1", size_kb: 18500, description: "Daemon and tooling for transactional snap packages", is_installed: true },
]);

pub fn init() {
    // Initialize Debian APT package cache & mirrors
    let _repo = DEBIAN_REPO.lock();
}

pub fn run_apt(args: &[&str]) -> Vec<String> {
    let mut out = Vec::new();

    if args.is_empty() {
        out.push(String::from("apt 2.6.1 (x86_64) - Debian APT Package Manager"));
        out.push(String::from("Usage: apt [command] [package]"));
        out.push(String::from("Commands:"));
        out.push(String::from("  update              - Update list of available packages"));
        out.push(String::from("  install <pkg>       - Install a new Debian package"));
        out.push(String::from("  remove <pkg>        - Remove an installed package"));
        out.push(String::from("  list [--installed]  - List packages"));
        out.push(String::from("  search <query>      - Search package names and descriptions"));
        out.push(String::from("Note: 'aptitude' is aliased with identical functionality."));
        return out;
    }

    match args[0] {
        "update" => {
            out.push(String::from("Hit:1 http://deb.debian.org/debian bookworm InRelease"));
            out.push(String::from("Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]"));
            out.push(String::from("Get:3 http://security.debian.org/debian-security bookworm-security InRelease [48.0 kB]"));
            out.push(String::from("Get:4 http://deb.debian.org/debian bookworm/main Sources [9,634 kB]"));
            out.push(String::from("Get:5 http://deb.debian.org/debian bookworm/main amd64 Packages [8,780 kB]"));
            out.push(String::from("Fetched 18.5 MB in 1s (18.5 MB/s)"));
            out.push(String::from("Reading package lists... Done"));
            out.push(String::from("Building dependency tree... Done"));
            out.push(String::from("All 68,412 packages are up to date."));
        }
        "install" => {
            if args.len() < 2 {
                out.push(String::from("E: Command line error: Must specify at least one package to install."));
                return out;
            }
            let target = args[1];
            let mut repo = DEBIAN_REPO.lock();
            let mut found = false;

            for pkg in repo.iter_mut() {
                if pkg.name == target {
                    found = true;
                    if pkg.is_installed {
                        out.push(format!("{} is already the newest version ({}).", pkg.name, pkg.version));
                    } else {
                        out.push(String::from("Reading package lists... Done"));
                        out.push(String::from("Building dependency tree... Done"));
                        out.push(format!("The following NEW packages will be installed: {}", pkg.name));
                        out.push(format!("Need to get {} kB of archives.", pkg.size_kb));
                        out.push(format!("Get:1 http://deb.debian.org/debian bookworm/main amd64 {} amd64 {} [{} kB]", pkg.name, pkg.version, pkg.size_kb));
                        out.push(format!("Unpacking {} ({})...", pkg.name, pkg.version));
                        out.push(format!("Setting up {} ({})...", pkg.name, pkg.version));
                        out.push(format!("[+] Successfully installed {} into /bin/{}", pkg.name, pkg.name));
                        pkg.is_installed = true;
                    }
                    break;
                }
            }

            if !found {
                out.push(format!("E: Unable to locate package {}", target));
            }
        }
        "remove" => {
            if args.len() < 2 {
                out.push(String::from("E: Must specify at least one package to remove."));
                return out;
            }
            let target = args[1];
            let mut repo = DEBIAN_REPO.lock();
            let mut found = false;

            for pkg in repo.iter_mut() {
                if pkg.name == target {
                    found = true;
                    if !pkg.is_installed {
                        out.push(format!("Package '{}' is not installed, so not removed", pkg.name));
                    } else {
                        out.push(format!("Removing {} ({})...", pkg.name, pkg.version));
                        out.push(format!("[+] Successfully removed /bin/{}", pkg.name));
                        pkg.is_installed = false;
                    }
                    break;
                }
            }
            if !found {
                out.push(format!("E: Unable to locate package {}", target));
            }
        }
        "list" => {
            let filter_installed = args.iter().any(|&a| a == "--installed");
            let repo = DEBIAN_REPO.lock();
            out.push(String::from("Listing packages... Done"));

            for pkg in repo.iter() {
                if filter_installed && !pkg.is_installed {
                    continue;
                }
                let status = if pkg.is_installed { "[installed]" } else { "" };
                out.push(format!("{}/bookworm {} amd64 {}", pkg.name, pkg.version, status));
            }
        }
        "search" => {
            if args.len() < 2 {
                out.push(String::from("E: Must specify search keyword."));
                return out;
            }
            let query = args[1];
            let repo = DEBIAN_REPO.lock();
            out.push(String::from("Sorting... Done"));
            out.push(String::from("Full Text Search... Done"));

            let mut hits = 0;
            for pkg in repo.iter() {
                if pkg.name.contains(query) || pkg.description.contains(query) {
                    out.push(format!("{} - {}", pkg.name, pkg.description));
                    hits += 1;
                }
            }
            if hits == 0 {
                out.push(format!("No packages found matching '{}'", query));
            }
        }
        cmd => {
            out.push(format!("apt: unknown command '{}'", cmd));
            out.push(String::from("See 'apt --help' for available commands."));
        }
    }

    out
}