Skip to content

Commit

Permalink
cleanup-package-files: switch from structopt to clap
Browse files Browse the repository at this point in the history
  • Loading branch information
lilydjwg committed Dec 10, 2024
1 parent 345d914 commit 2072a86
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 122 deletions.
152 changes: 45 additions & 107 deletions cleanup-package-files/Cargo.lock

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

8 changes: 2 additions & 6 deletions cleanup-package-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ edition = "2021"
eyre = "*"
expanduser = "*"
# since 1.4.0 it uses thiserror instead of failure
pwd = "1.4"
pwd = "*"
nix = { version = "*", features = ["fs", "user"] }

[dependencies.structopt]
version = "*"
default-features = false
features = []
clap = { version = "*", default-features = false, features = ["std", "derive", "help", "usage", "error-context"] }

[profile.release]
lto = true
17 changes: 8 additions & 9 deletions cleanup-package-files/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs::File;

use expanduser::expanduser;
use eyre::{Result, ensure, eyre};
use structopt::StructOpt;
use clap::Parser;
use nix::fcntl::Flock;

const LILAC_LOCK: &str = "~lilydjwg/.lilac/.lock";
Expand Down Expand Up @@ -35,29 +35,28 @@ fn git_ls_files() -> Result<Vec<OsString>> {
)
}

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[derive(Parser, Debug)]
struct Cli {
/// remove files for real; or only print what will be removed
#[structopt(long="real")]
#[arg(long="real")]
real: bool,
pkgname: String,
}

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

let pwd = pwd::Passwd::from_name(USER)
.map_err(|e| eyre!("cannot get passwd entry for user {}: {:?}", USER, e))?
.unwrap();
nix::unistd::setuid(nix::unistd::Uid::from_raw(pwd.uid))?;

let _lock;
if opt.real {
if cli.real {
_lock = flock(expanduser(LILAC_LOCK)?)?;
}
let mut path = expanduser(LILAC_REPO)?;
path.push(&opt.pkgname);
path.push(&cli.pkgname);

std::env::set_current_dir(&path)?;
let tracked_files = git_ls_files()?;
Expand All @@ -68,7 +67,7 @@ fn main() -> Result<()> {
if tracked_files.contains(&file_name) {
continue;
}
if opt.real {
if cli.real {
println!("rm -rf {}", entry.path().display());
Command::new("rm").arg("-rf").arg(&file_name).spawn()?;
} else {
Expand Down

0 comments on commit 2072a86

Please sign in to comment.