Skip to content

Commit

Permalink
Add --quiet sync flag to silence stderr and stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyGolden committed Jun 23, 2024
1 parent cb5c400 commit 10dce51
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.8.0] - 2024-06-23

### Added

- Add `--quiet` flag for the `sync` subcommand to silence stdout

## [0.7.0] - 2024-06-22

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ tinted-builder-rust build path/to/base16-template

The following is a table of the available subcommands for the CLI tool (tinted-builder-rust), including the descriptions and any notable arguments.

| Subcommand | Description | Arguments | Example Usage |
|------------|--------------------------------------|----------------------|--------------------------------------------|
| `sync` | Installs and or updates latest schemes. | - | `tinted-builder-rust sync` |
| `build` | Builds the themes of a template. | `template_path`: Path to template directory. | `tinted-builder-rust build ./path/to/base16-template` |
| Subcommand | Description | Arguments | Example Usage | Flags |
|------------|--------------------------------------|----------------------|--------------------------------------------|-------|
| `sync` | Installs and or updates latest schemes. | - | `tinted-builder-rust sync` | `--quiet` or `-q` to silence output |
| `build` | Builds the themes of a template. | `template_path`: Path to template directory. | `tinted-builder-rust build ./path/to/base16-template` | `--quiet` or `-q` to silence output |

## Flags

Expand Down
2 changes: 1 addition & 1 deletion tinted-builder-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tinted-builder-rust"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
authors = ["Jamy Golden <[email protected]>", "Tinted Theming <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
9 changes: 8 additions & 1 deletion tinted-builder-rust/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ fn build_cli() -> Command {
)
.subcommand(
Command::new("sync")
.about("Clones {} and if it exists it does a git pull on the local clone"),
.about("Clones {} and if it exists it does a git pull on the local clone")
.arg(
Arg::new("quiet")
.long("quiet")
.short('q')
.help("Silence stdout")
.action(ArgAction::SetTrue),
),
)
}

Expand Down
8 changes: 6 additions & 2 deletions tinted-builder-rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ fn main() -> Result<()> {
operations::build::build(&template_path, &schemes_path, *is_quiet)?;
}
}
Some(("sync", _)) => {
operations::sync::sync(&schemes_path)?;
Some(("sync", sub_matches)) => {
let is_quiet: bool = sub_matches
.get_one::<bool>("quiet")
.map(|b| b.to_owned())
.unwrap_or(false);
operations::sync::sync(&schemes_path, is_quiet)?;
}
_ => {
println!("Basic usage: {} apply <SCHEME_NAME>", REPO_NAME);
Expand Down
32 changes: 20 additions & 12 deletions tinted-builder-rust/src/operations/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const REPO_NAME: &str = env!("CARGO_PKG_NAME");
const SCHEMES_REPO_NAME: &str = "schemes";
const SCHEMES_URL: &str = "https://github.com/tinted-theming/schemes";

fn git_clone(repo_url: &str, target_dir: &Path) -> Result<()> {
fn git_clone(repo_url: &str, target_dir: &Path, is_quiet: bool) -> Result<()> {
if target_dir.exists() {
return Err(anyhow!(
"Error cloning {}. Target directory '{}' already exists",
Expand All @@ -16,12 +16,15 @@ fn git_clone(repo_url: &str, target_dir: &Path) -> Result<()> {
));
}

Command::new("git")
.arg("clone")
.arg(repo_url)
.arg(target_dir)
.stdout(Stdio::null())
.status()
let mut cmd = Command::new("git");

cmd.arg("clone").arg(repo_url).arg(target_dir);

if is_quiet {
cmd.stdout(Stdio::null()).stderr(Stdio::null());
};

cmd.status()
.with_context(|| format!("Failed to clone repository from {}", repo_url))?;

Ok(())
Expand Down Expand Up @@ -66,7 +69,7 @@ fn git_diff(target_dir: &Path) -> Result<bool> {
}

/// Sync schemes repo; Install if it does not exist, otherwise update
pub(crate) fn sync(schemes_path: &Path) -> Result<()> {
pub(crate) fn sync(schemes_path: &Path, is_quiet: bool) -> Result<()> {
if schemes_path.is_dir() {
let is_diff = git_diff(schemes_path)?;

Expand All @@ -75,13 +78,18 @@ pub(crate) fn sync(schemes_path: &Path) -> Result<()> {
format!("Error pulling {} from {}", SCHEMES_REPO_NAME, SCHEMES_URL)
})?;

println!("{} up to date", SCHEMES_REPO_NAME);
} else {
if !is_quiet {
println!("{} up to date", SCHEMES_REPO_NAME);
}
} else if !is_quiet {
println!("{} contains uncommitted changes, please commit or remove and then run `{} update` again.", SCHEMES_REPO_NAME, REPO_NAME);
}
} else {
git_clone(SCHEMES_URL, schemes_path)?;
println!("{} installed", SCHEMES_REPO_NAME);
git_clone(SCHEMES_URL, schemes_path, is_quiet)?;

if !is_quiet {
println!("{} installed", SCHEMES_REPO_NAME);
}
}

Ok(())
Expand Down
41 changes: 41 additions & 0 deletions tinted-builder-rust/tests/operation_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ fn operation_sync_first_time() -> Result<()> {
Ok(())
}

#[test]
fn operation_sync_first_time_with_quiet_flag() -> Result<()> {
// -------
// Arrange
// -------
let name = "test_operation_sync_first_time_with_quiet_flag";
let expected_schemes_path = PathBuf::from(format!("./{}/schemes", name));
let expected_data_path = PathBuf::from(name);
if expected_data_path.exists() {
fs::remove_dir_all(&expected_data_path)?;
fs::create_dir(expected_data_path)?;
}

// ---
// Act
// ---
let (stdout, stderr) = utils::run_command(vec![
COMMAND_NAME.to_string(),
format!("--data-dir={}", name),
"sync".to_string(),
"--quiet".to_string(),
])
.unwrap();
let is_schemes_dir_empty = fs::read_dir(&expected_schemes_path)?.next().is_none();

// ------
// Assert
// ------
assert!(
stdout.is_empty(),
"stdout does not contain the expected output"
);
assert!(
stderr.is_empty(),
"stderr does not contain the expected output"
);
assert!(expected_schemes_path.exists() && !is_schemes_dir_empty,);

Ok(())
}

/// Update - Install has already completed
#[test]
fn operation_sync_update() -> Result<()> {
Expand Down

0 comments on commit 10dce51

Please sign in to comment.