Skip to content

Commit

Permalink
xtask: Use anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop committed Jun 3, 2024
1 parent 963e9b7 commit 25ab38e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
82 changes: 82 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ edition = "2021"
publish = false

[dependencies]
anyhow = { version = "1.0.86", features = ["backtrace"] }
clap = { version = "4.5.0", default-features = false, features = ["derive", "help", "std"] }
nix = { version = "0.29.0", features = ["user"] }
21 changes: 13 additions & 8 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use anyhow::{bail, Result};
use clap::{Parser, Subcommand};
use std::fs;
use std::path::{Path, PathBuf};
Expand All @@ -17,7 +18,7 @@ struct DiskParams {
}

impl DiskParams {
fn create(&self) {
fn create(&self) -> Result<()> {
let uid = nix::unistd::getuid();
let gid = nix::unistd::getgid();

Expand All @@ -29,16 +30,18 @@ impl DiskParams {
.args(["-E", &format!("root_owner={uid}:{gid}")])
.arg(&self.path)
.arg(format!("{}k", self.size_in_kilobytes))
.status()
.unwrap();
assert!(status.success());
.status()?;
if !status.success() {
bail!("mkfs.ext4 failed");
}
Ok(())
}
}

fn create_test_data() {
fn create_test_data() -> Result<()> {
let dir = Path::new("test_data");
if !dir.exists() {
fs::create_dir(dir).unwrap();
fs::create_dir(dir)?;
}

let path = dir.join("test_disk1.bin");
Expand All @@ -47,10 +50,12 @@ fn create_test_data() {
path: path.to_owned(),
size_in_kilobytes: 1024 * 64,
};
disk.create();
disk.create()?;
// TODO(nicholasbishop): mount the filesystem and fill it with
// test data.
}

Ok(())
}

#[derive(Parser)]
Expand All @@ -68,7 +73,7 @@ enum Action {
CreateTestData,
}

fn main() {
fn main() -> Result<()> {
let opt = Opt::parse();

match &opt.action {
Expand Down

0 comments on commit 25ab38e

Please sign in to comment.