Skip to content

Commit

Permalink
Merge #911
Browse files Browse the repository at this point in the history
911: Structify more of the internals to simplify the implementation. r=Emilgardis a=Alexhuszagh

Reduces the number of arguments to the function signatures, making the code much easier to understand, and relocates some things. For example, `docker_in_docker` is now associated with `Engine`, and now with `cli::Args`. Likewise, unstable doctests is now a part of `Environment` and not `cli::Args`. `MessageInfo` now stores mutable state (needs to clear lines) and is no longer copyable, and a single instance is created in lib and passed around everywhere as a mutable reference.

The changes with `MessageInfo` will allow us to erase lines in the future, both from `stderr` and `stdout`, which we currently don't use but will allow us to close #859.

 This also fixes an issue where `-vv+` was not recognized as a valid verbose flag: `cargo` can handle 1 or more `v` characters. Also allowed parsing of `--color=value` args, when we previously only accepted `--color value`.

Co-authored-by: Alex Huszagh <ahuszagh@gmail.com>
  • Loading branch information
bors[bot] and Alexhuszagh authored Jul 6, 2022
2 parents 7331bb9 + 94f1805 commit 3dc0d1f
Show file tree
Hide file tree
Showing 29 changed files with 1,147 additions and 1,004 deletions.
44 changes: 30 additions & 14 deletions src/bin/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use super::containers::*;
use super::images::*;
use clap::Args;
use cross::shell::{self, MessageInfo};
use cross::shell::MessageInfo;

#[derive(Args, Debug)]
pub struct Clean {
Expand Down Expand Up @@ -31,22 +31,22 @@ pub struct Clean {
}

impl Clean {
pub fn run(self, engine: cross::docker::Engine) -> cross::Result<()> {
let msg_info = MessageInfo::create(self.verbose, self.quiet, self.color.as_deref())?;
pub fn run(
self,
engine: cross::docker::Engine,
msg_info: &mut MessageInfo,
) -> cross::Result<()> {
let tempdir = cross::temp::dir()?;
match self.execute {
true => {
if tempdir.exists() {
fs::remove_dir_all(tempdir)?;
}
}
false => shell::print(
format!(
"fs::remove_dir_all({})",
cross::pretty_path(&tempdir, |_| false)
),
msg_info,
)?,
false => msg_info.print(format_args!(
"fs::remove_dir_all({})",
cross::pretty_path(&tempdir, |_| false)
))?,
}

// containers -> images -> volumes -> prune to ensure no conflicts.
Expand All @@ -58,7 +58,7 @@ impl Clean {
execute: self.execute,
engine: None,
};
remove_containers.run(engine.clone())?;
remove_containers.run(engine.clone(), msg_info)?;

let remove_images = RemoveImages {
targets: vec![],
Expand All @@ -70,7 +70,7 @@ impl Clean {
execute: self.execute,
engine: None,
};
remove_images.run(engine.clone())?;
remove_images.run(engine.clone(), msg_info)?;

let remove_volumes = RemoveAllVolumes {
verbose: self.verbose,
Expand All @@ -80,7 +80,7 @@ impl Clean {
execute: self.execute,
engine: None,
};
remove_volumes.run(engine.clone())?;
remove_volumes.run(engine.clone(), msg_info)?;

let prune_volumes = PruneVolumes {
verbose: self.verbose,
Expand All @@ -89,8 +89,24 @@ impl Clean {
execute: self.execute,
engine: None,
};
prune_volumes.run(engine)?;
prune_volumes.run(engine, msg_info)?;

Ok(())
}

pub fn engine(&self) -> Option<&str> {
self.engine.as_deref()
}

pub fn verbose(&self) -> bool {
self.verbose
}

pub fn quiet(&self) -> bool {
self.quiet
}

pub fn color(&self) -> Option<&str> {
self.color.as_deref()
}
}
Loading

0 comments on commit 3dc0d1f

Please sign in to comment.