DEVELOPMENT ENVIRONMENT

~liljamo/gandalf

ref: c4b6b2551ab961b415b6ad29c225003e635bff55 gandalf/src/programs.rs -rw-r--r-- 1.4 KiB
c4b6b255Jonni Liljamo feat: initial working(tm) version 1 year, 8 months ago
                                                                                
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
use std::{env, path::PathBuf};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Program {
    pub path: String,
    pub exec: String,
    pub flatpak: bool,
}

pub async fn fetch() -> Vec<Program> {
    let mut programs: Vec<Program> = vec![];

    match env::var("PATH") {
        Ok(paths) => {
            for path in paths.split(":") {
                let mut path = PathBuf::from(path);

                if path.exists() {
                    // resolve symlinks
                    if path.is_symlink() {
                        path = path.canonicalize().unwrap();
                    }

                    for binary in path.read_dir().unwrap() {
                        let binary = binary.unwrap();
                        programs.push(Program {
                            path: binary.path().into_os_string().into_string().unwrap(),
                            exec: binary.file_name().into_string().unwrap(),
                            flatpak: false,
                        });
                    }
                }
            }
        }
        Err(_) => {}
    }

    // sort alphabetically and deduplicate

    // TODO: do this some way that doesn't require cloning for each one.
    programs.sort_unstable_by_key(|p| p.exec.to_lowercase().clone());
    programs.dedup();

    programs
}

pub fn launch(path_to_binary: &str) -> std::io::Result<std::process::Child> {
    std::process::Command::new(path_to_binary).spawn()
}