Avoid code duplication

This commit is contained in:
Roey Darwish Dror 2018-11-05 09:13:39 +02:00
parent ec7f60aed4
commit c674ef5f2c

View File

@ -42,22 +42,25 @@ SystemMaxUse=16M
#[structopt(name = "alma", about = "Arch Linux Mobile Appliance")] #[structopt(name = "alma", about = "Arch Linux Mobile Appliance")]
enum App { enum App {
#[structopt(name = "create", about = "Create a new Arch Linux USB")] #[structopt(name = "create", about = "Create a new Arch Linux USB")]
Create { Create(CreateCommand),
/// Path starting with /dev/disk/by-id for the USB drive
#[structopt(parse(from_os_str),)]
disk: PathBuf,
/// Additional pacakges to install
#[structopt(short = "p", long = "extra-packages", value_name = "package",)]
extra_packages: Vec<String>,
/// Enter interactive chroot before unmounting the drive
#[structopt(short = "i", long = "interactive")]
interactive: bool,
},
} }
fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Result<(), Error> { #[derive(StructOpt)]
struct CreateCommand {
/// Path starting with /dev/disk/by-id for the USB drive
#[structopt(parse(from_os_str),)]
disk: PathBuf,
/// Additional pacakges to install
#[structopt(short = "p", long = "extra-packages", value_name = "package",)]
extra_packages: Vec<String>,
/// Enter interactive chroot before unmounting the drive
#[structopt(short = "i", long = "interactive")]
interactive: bool,
}
fn create(command: CreateCommand) -> Result<(), Error> {
let sgdisk = Tool::find("sgdisk")?; let sgdisk = Tool::find("sgdisk")?;
let sync = Tool::find("sync")?; let sync = Tool::find("sync")?;
let pacstrap = Tool::find("pacstrap")?; let pacstrap = Tool::find("pacstrap")?;
@ -67,8 +70,9 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
let mkbtrfs = Tool::find("mkfs.btrfs")?; let mkbtrfs = Tool::find("mkfs.btrfs")?;
let mut mount_stack = MountStack::new(); let mut mount_stack = MountStack::new();
if !(disk.starts_with("/dev/disk/by-id") if !(command.disk.starts_with("/dev/disk/by-id")
&& (disk && (command
.disk
.file_name() .file_name()
.and_then(|s| s.to_str()) .and_then(|s| s.to_str())
.filter(|ref f| f.starts_with("usb-")) .filter(|ref f| f.starts_with("usb-"))
@ -90,7 +94,7 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
"--largest-new=3", "--largest-new=3",
"--typecode=1:EF02", "--typecode=1:EF02",
"--typecode=2:EF00", "--typecode=2:EF00",
]).arg(&disk) ]).arg(&command.disk)
.run(ErrorKind::Partitioning)?; .run(ErrorKind::Partitioning)?;
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
@ -99,18 +103,18 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
mkfat mkfat
.execute() .execute()
.arg("-F32") .arg("-F32")
.arg(format!("{}-part2", disk.display())) .arg(format!("{}-part2", command.disk.display()))
.run(ErrorKind::Formatting)?; .run(ErrorKind::Formatting)?;
mkbtrfs mkbtrfs
.execute() .execute()
.arg("-f") .arg("-f")
.arg(format!("{}-part3", disk.display())) .arg(format!("{}-part3", command.disk.display()))
.run(ErrorKind::Formatting)?; .run(ErrorKind::Formatting)?;
info!("Mounting filesystems to {}", mount_point.path().display()); info!("Mounting filesystems to {}", mount_point.path().display());
mount_stack mount_stack
.mount( .mount(
&PathBuf::from(format!("{}-part3", disk.display())), &PathBuf::from(format!("{}-part3", command.disk.display())),
&mount_point.path(), &mount_point.path(),
Filesystem::Btrfs, Filesystem::Btrfs,
Some("compress=zstd"), Some("compress=zstd"),
@ -121,7 +125,7 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
mount_stack mount_stack
.mount( .mount(
&PathBuf::from(format!("{}-part2", disk.display())), &PathBuf::from(format!("{}-part2", command.disk.display())),
&boot_point, &boot_point,
Filesystem::Vfat, Filesystem::Vfat,
None, None,
@ -140,7 +144,7 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
"networkmanager", "networkmanager",
"btrfs-progs", "btrfs-progs",
"broadcom-wl", "broadcom-wl",
]).args(extra_packages) ]).args(command.extra_packages)
.run(ErrorKind::Pacstrap)?; .run(ErrorKind::Pacstrap)?;
let fstab = genfstab let fstab = genfstab
@ -178,10 +182,10 @@ fn create(disk: PathBuf, extra_packages: Vec<String>, interactive: bool) -> Resu
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["bash", "-c"]) .args(&["bash", "-c"])
.arg(format!("grub-install --target=i386-pc --boot-directory /boot {} && grub-install --target=x86_64-efi --efi-directory /boot --boot-directory /boot --removable && grub-mkconfig -o /boot/grub/grub.cfg", disk.display())) .arg(format!("grub-install --target=i386-pc --boot-directory /boot {} && grub-install --target=x86_64-efi --efi-directory /boot --boot-directory /boot --removable && grub-mkconfig -o /boot/grub/grub.cfg", command.disk.display()))
.run(ErrorKind::Bootloader)?; .run(ErrorKind::Bootloader)?;
if interactive { if command.interactive {
info!("Dropping you to chroot. Do as you wish to customize the installation"); info!("Dropping you to chroot. Do as you wish to customize the installation");
arch_chroot arch_chroot
.execute() .execute()
@ -219,11 +223,7 @@ fn main() {
} }
let result = match app { let result = match app {
App::Create { App::Create(command) => create(command),
disk,
extra_packages,
interactive,
} => create(disk, extra_packages, interactive),
}; };
match result { match result {