Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rm subcommand to locally remove output artifacts #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,53 @@ impl<'a> Cache<'a> {
Ok((true, req_obj))
}

/// Remove local output of artifact
pub fn rm(
&self,
artifact: &'a Artifact,
dry_run: bool,
) -> std::result::Result<bool, std::io::Error> {
for oup in &artifact.outputs {
let dst = self.repo.path.as_path().join(oup);
// Get metadata to check if file exists and is file or directory
let md = match std::fs::metadata(&dst) {
Ok(md) => md,
Err(_e) => {
continue;
}
};
// If output is a directory, remove it recursively
if md.is_dir() {
if dry_run {
println!("Would remove {:?}", dst);
continue;
}
debug!("Removing {:?}", dst);
match fs::remove_dir_all(&dst) {
Ok(()) => (),
Err(e) => {
error!("Failed to remove directory {:?}: {:?}", &dst, e);
return Err(e);
}
}
} else if md.is_file() {
if dry_run {
println!("Would remove {:?}", dst);
continue;
}
debug!("Removing {:?}", dst);
match fs::remove_file(&dst) {
Ok(()) => (),
Err(e) => {
error!("Failed to remove file {:?}: {:?}", &dst, e);
return Err(e);
}
}
}
}
Ok(true)
}

fn objects_identical_for_path(&self, a: &Object, b: &Object, path: &Path) -> bool {
let key = (a.oid.clone(), b.oid.clone(), path.to_path_buf());
if let Some(entry) = self.objects_path_identity_cache.borrow().get(&key) {
Expand Down
35 changes: 35 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ pub fn main() -> Result<bool> {
.takes_value(true)
.required(true)
)
)
.subcommand(SubCommand::with_name("rm")
.about("Locally remove the outputs of an artifact.")
.arg(Arg::with_name("artifact")
.takes_value(true)
.required(true)
)
.arg(Arg::with_name("dry-run")
.long("dry-run")
.short("n")
.help("Print the files that would be deleted, but do not remove them.")
)
);

// Parse command-line arguments.
Expand Down Expand Up @@ -159,6 +171,10 @@ pub fn main() -> Result<bool> {
false => lookup(&cache, matches, ignore_uncommitted_changes),
true => Ok(false),
},
("rm", Some(matches)) => match disabled {
false => rm(&cache, matches),
true => Ok(false),
},
_ => Error::result("Unknown combination of subcommand and arguments!"),
}
}
Expand Down Expand Up @@ -230,3 +246,22 @@ pub fn lookup(
}
}
}

pub fn rm(cache: &Cache, matches: &ArgMatches) -> Result<bool> {
let artifact_name = artifact_name(matches)?;
let artifact = cache.artifact(artifact_name)?;
let dry_run = matches.is_present("dry-run");

if dry_run {
info!("Dry-run");
}

match cache.rm(&artifact, dry_run) {
Ok(true) => {
info!("Output removal successful.");
Ok(true)
},
Ok(false) => Ok(false),
Err(_e) => Ok(false),
}
}