aboutsummaryrefslogtreecommitdiffstats
path: root/src/action.rs
blob: e69d79dd377967ae8ec51829a4049588fe2ca31d (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use crate::config::{self, Config, ConfigCommand, TEMP_CONFIG_PATH};
use crate::lock::{self, Lockfile, Package};
use crate::util::{self, BASE_CONFIG_PATH, BASE_REPO_PATH, PackageList};
use git2::Repository;
use std::fs;
use std::path::PathBuf;

pub enum Action {
    Add { url: String },
    Update,
    Remove { packages: Vec<String> },
    List,
    Search { term: String },
    Clean { packages: Vec<String> },
    Show { package: String },
    Version,
}

impl Action {
    pub fn parse(args: &[String]) -> Result<Self, String> {
        let cmd = args.get(1).ok_or("no command provided")?.as_str();

        match cmd {
            "add" => {
                let url = args.get(2).ok_or("add requires <repo>")?.clone();
                Ok(Action::Add { url })
            }
            "update" => Ok(Action::Update),
            "remove" => {
                let packages = args[2..].to_vec();

                if packages.is_empty() {
                    Err("remove requires a package".into())
                } else {
                    Ok(Action::Remove { packages })
                }
            }
            "list" => Ok(Action::List),
            "search" => {
                let term = args.get(2).ok_or("search requires <term>")?.clone();
                Ok(Action::Search { term })
            }
            "clean" => {
                let packages = args[2..].to_vec();
                Ok(Action::Clean { packages })
            }
            "show" => {
                let package = args.get(2).ok_or("show requires <package>")?.clone();
                Ok(Action::Show { package })
            }
            "--version" => Ok(Action::Version),
            _ => Err(format!("unknown command {}", cmd)),
        }
    }

    pub fn execute(self) -> Result<(), String> {
        match self {
            Action::Add { url } => add(url.as_str()),
            Action::Update => update(),
            Action::Remove { packages } => remove(packages),
            Action::List => list(),
            Action::Search { term } => Ok(search(term)),
            Action::Clean { packages } => clean(packages),
            Action::Show { package } => show(package),
            Action::Version => Ok(version()),
        }
    }
}

fn add(url: &str) -> Result<(), String> {
    if !nix::unistd::geteuid().is_root() {
        return Err("add must be run as root".to_string());
    }

    let repo_name = {
        let last_segment = url.rsplit('/').next().unwrap_or(url);
        last_segment.strip_suffix(".git").unwrap_or(last_segment)
    };
    let config_name = format!("{repo_name}.toml");

    println!("Creating config: {}", config_name);
    config::create_config(repo_name)?;

    let editor = util::get_editor();
    let config_temp = format!("{}/{}", TEMP_CONFIG_PATH, config_name);
    util::open_in_editor(&editor, &config_temp)?;

    let clone_path = PathBuf::from(BASE_REPO_PATH).join(repo_name);
    let repo = Repository::clone(url, &clone_path)
        .map_err(|e| format!("failed to clone {}: {}", repo_name, e))?;

    let mut config_path = PathBuf::from(BASE_CONFIG_PATH);
    if !config_path.exists() {
        fs::create_dir_all(&config_path)
            .map_err(|e| format!("failed to create config directory: {}", e))?;
    }

    config_path.push(config_name);

    fs::rename(config_temp, &config_path)
        .map_err(|e| format!("failed to place config in system directory: {}", e))?;

    println!(
        "New package initialized at: {}\n",
        repo.path().to_str().unwrap()
    );

    // if configured for tag mode move the head to the tag before build
    let config = Config::new(&config_path).ok_or("config not found".to_string())?;
    if let Some(u) = config.update {
        if u.as_str() == "tagged" {
            util::pull_latest_tag(&clone_path)
                .map_err(|e| format!("failed to update repo: {e}"))?;
        }
    }

    if util::yn_prompt("Run build and install commands?") {
        println!("Building...");
        config::run_config_command(&config_path, &clone_path, ConfigCommand::Build)?;
        println!("Installing...");
        config::run_config_command(&config_path, &clone_path, ConfigCommand::Install)?;
        config::run_config_command(&config_path, &clone_path, ConfigCommand::PostInstall)?;
    }

    // only add to lockfile if installed
    let oid = util::get_commit_hash_full(&clone_path)
        .map_err(|e| format!("could not get commit hash: {e}"))?;

    let mut lockfile = Lockfile::new();
    lockfile.update_pkg(Package {
        name: repo_name.to_string(),
        source: url.to_string(),
        checksum: oid.to_string(),
    })?;

    Ok(())
}

fn update() -> Result<(), String> {
    if !nix::unistd::geteuid().is_root() {
        return Err("update must be run as root".to_string());
    }

    let package_paths = util::collect_packages()?;

    let mut update_pkgs: PackageList = vec![];
    let mut lockfile = Lockfile::new();
    for (name, path, config_path) in package_paths {
        let config = Config::new(&config_path).ok_or("config not found".to_string())?;
        if let Some(u) = config.update {
            match u.as_str() {
                "live" => {
                    util::pull_repo(&path).map_err(|e| format!("failed to update repo: {e}"))?;
                    let oid = util::get_commit_hash_full(&path)
                        .map_err(|e| format!("could not get commit hash: {e}"))?;
                    let url = util::get_remote_url(&path)
                        .map_err(|e| format!("failed to get url for remote origin: {e}"))?;
                    let p = Package {
                        name: name.clone(),
                        source: url,
                        checksum: oid.to_string(),
                    };
                    if lockfile.out_of_date(p) {
                        update_pkgs.push((name, path, config_path));
                    }
                }
                "tagged" => {
                    util::pull_latest_tag(&path)
                        .map_err(|e| format!("failed to update repo: {e}"))?;
                    let oid = util::get_commit_hash_full(&path)
                        .map_err(|e| format!("could not get commit hash: {e}"))?;
                    let url = util::get_remote_url(&path)
                        .map_err(|e| format!("failed to get url for remote origin: {e}"))?;
                    let p = Package {
                        name: name.clone(),
                        source: url,
                        checksum: oid.to_string(),
                    };
                    if lockfile.out_of_date(p) {
                        update_pkgs.push((name, path, config_path));
                    }
                }
                "none" => continue,
                e => return Err(format!("unknown update mode {e}")),
            }
        }
    }

    if !update_pkgs.is_empty() {
        util::print_collected_packages(&update_pkgs, "Packages to update");
        if util::yn_prompt("Proceed with update?") {
            for (name, path, cfg_path) in update_pkgs {
                config::run_config_command(&cfg_path, &path, ConfigCommand::Build)?;
                config::run_config_command(&cfg_path, &path, ConfigCommand::Install)?;

                let oid = util::get_commit_hash_full(&path)
                    .map_err(|e| format!("could not get commit hash: {e}"))?;
                let url = util::get_remote_url(&path)
                    .map_err(|e| format!("failed to get url for remote origin: {e}"))?;
                lockfile.update_pkg(lock::Package {
                    name: name.clone(),
                    source: url,
                    checksum: oid.to_string(),
                })?;

                println!("Upgraded {}", name);
            }
        }
    } else {
        println!("No packages to update.");
    }

    Ok(())
}

fn remove(packages: Vec<String>) -> Result<(), String> {
    if !nix::unistd::geteuid().is_root() {
        return Err("remove must be run as root".to_string());
    }

    let package_paths: PackageList = util::collect_named_packages(packages)?;

    util::print_collected_packages(&package_paths, "Packages to remove");

    let total_size: u64 = package_paths
        .iter()
        .map(|(_, path, _)| util::dir_size(path).unwrap_or(0))
        .sum();

    println!(
        "Total remove size: {:.2} MB\n",
        total_size as f64 / (1024.0 * 1024.0)
    );

    if util::yn_prompt("Proceed with removal?") {
        for (name, path, cfg_path) in package_paths {
            config::run_config_command(&cfg_path, &path, ConfigCommand::Uninstall)?;
            config::run_config_command(&cfg_path, &path, ConfigCommand::PostUninstall)?;
            fs::remove_dir_all(&path).map_err(|e| format!("failed to remove {}: {}", name, e))?;
            fs::remove_file(&cfg_path).map_err(|e| format!("failed to remove {}: {}", name, e))?;
            println!("Removed {}", name);
        }
    }

    Ok(())
}

fn list() -> Result<(), String> {
    if !nix::unistd::geteuid().is_root() {
        return Err("list must be run as root".to_string());
    }

    for entry in fs::read_dir(BASE_REPO_PATH)
        .map_err(|e| format!("failed to iterate package directory: {}", e))?
    {
        let entry = entry.map_err(|e| e.to_string())?;
        let path = entry.path();
        if path.is_dir() {
            let oid = util::get_commit_hash_short(&path)
                .map_err(|e| format!("failed to get commit hash: {e}"))?;
            let oid = oid.as_str().unwrap();
            if let Some(stem) = path.file_stem() {
                println!("{} ({})", stem.to_string_lossy(), oid);
            }
        }
    }
    Ok(())
}

fn search(term: String) {
    println!("searching: {}", term);
}

fn clean(packages: Vec<String>) -> Result<(), String> {
    if !nix::unistd::geteuid().is_root() {
        return Err("clean must be run as root".to_string());
    }

    let package_paths: PackageList = if packages.is_empty() {
        util::collect_packages()?
    } else {
        util::collect_named_packages(packages)?
    };

    util::print_collected_packages(&package_paths, "Packages to clean");

    if util::yn_prompt("Proceed with cleanup?") {
        for (name, path, cfg_path) in package_paths {
            config::run_config_command(&cfg_path, &path, ConfigCommand::Clean)?;
            println!("Cleaned {}", name);
        }
    }

    Ok(())
}

fn show(package: String) -> Result<(), String> {
    let config_path = PathBuf::from(BASE_CONFIG_PATH).join(format!("{package}.toml"));
    let config = Config::new(&config_path).ok_or("config not found".to_string())?;
    config.log_config();
    Ok(())
}

fn version() {
    println!(
        r#"
.-------..___    Forge v{}
'-._     :_.-'   Copyright (C) 2026 Lance Borden
    ) _ (
   '-' '-'       This program is free software
                 under the MIT license.
    "#,
        env!("CARGO_PKG_VERSION")
    );
}