aboutsummaryrefslogtreecommitdiff
path: root/src/bin/create-chroot.rs
blob: 185a629740f96d4b57d887326e6dfcba4559ce84 (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
use std::process::{Child, Command, Output};

fn main() {
    let chroot_dir = std::env::var("PKGBUILD_CHROOT_DIR")
        .unwrap_or_else(|_| String::from("/var/lib/pkgbuild"))
        .trim_end_matches('/')
        .to_string();

    let mut command = Command::new("mkdir");
    command.arg("-p").arg(&chroot_dir);
    dbg!(&command);
    print_status(command.spawn());


    let mut command = Command::new("chown");
    command
        .arg(format!(
            "{}:{}",
            get_username().unwrap(),
            get_groupname().unwrap()
        ))
        .arg(&chroot_dir);
    dbg!(&command);
    print_status(command.spawn());


    let mut command = Command::new("mkarchroot");
    command
        .arg("-C")
        .arg("pacman.conf")
        .arg("-M")
        .arg("makepkg.conf")
        .arg(format!("{chroot_dir}/root"))
        .args(["base-devel"]);
    dbg!(&command);
    print_status(command.spawn());
}

fn print_status(output: Result<Child, std::io::Error>) {
    match output {
        Ok(mut child) => match child.wait() {
            Ok(status) => {
                println!("STATUS: {}", status.code().unwrap_or(-1));
            }
            Err(e) => {
                println!("ERROR: {e}");
            }
        },
        Err(e) => {
            println!("ERROR: {e}");
        }
    }
}

fn get_username() -> anyhow::Result<String> {
    let output = Command::new("id").arg("-un").output()?;
    Ok(String::from_utf8(output.stdout)?.trim().to_string())
}

fn get_groupname() -> anyhow::Result<String> {
    let output = Command::new("id").arg("-gn").output()?;
    Ok(String::from_utf8(output.stdout)?.trim().to_string())
}