Skip to content

Commit

Permalink
feat(task): add indicatif progress bars for task execution
Browse files Browse the repository at this point in the history
Shows what is currently running.
  • Loading branch information
gibfahn committed Apr 20, 2024
1 parent 63e01bf commit 51ad144
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 41 deletions.
114 changes: 114 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ git2 = { version = "0.18.3", features = [
] }
hex = "0.4.3"
itertools = "0.12.1"
indicatif = { version = "0.17.8", features = ["rayon"] }
log = "0.4.21"
plist = "1.6.1"
rayon = "1.10.0"
Expand All @@ -77,6 +78,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = "2.5.0"
users = "0.11.0"
walkdir = "2.5.0"
tracing-indicatif = "0.3.6"

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
7 changes: 3 additions & 4 deletions src/generate/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ pub fn run(configs: &[GenerateGitConfig]) -> Result<TaskStatus> {

/// Run a single git config generation.
pub fn run_single(generate_git_config: &GenerateGitConfig) -> Result<TaskStatus> {
debug!(
"Generating git config for: {path}",
path = generate_git_config.path
);
let _span =
tracing::info_span!("generate_git", repo = &generate_git_config.path.as_str()).entered();
debug!("Generating git config");
let mut git_task = Task::from(&generate_git_config.path)?;
debug!("Existing git config: {git_task:?}");
let name = git_task.name.as_str();
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ use tracing::trace;
use tracing::warn;
use tracing::Level;
use tracing_error::ErrorLayer;
use tracing_indicatif::IndicatifLayer;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::prelude::*;
use tracing_subscriber::util::SubscriberInitExt;
use up_rs::log;
use up_rs::opts::Opts;

Expand Down Expand Up @@ -91,11 +94,13 @@ fn main() -> Result<()> {
/// Set up logging to stderr and to a temp file path.
/// Returns the log level filter chosen by the user if available, and the path to the log file.
fn set_up_logging(opts: &Opts) -> (Utf8PathBuf, Option<LevelFilter>) {
let indicatif_layer = IndicatifLayer::new();

let stderr_log = tracing_subscriber::fmt::layer()
.compact()
.with_target(false)
.without_time()
.with_writer(std::io::stderr);
.with_writer(indicatif_layer.get_stderr_writer());

let (log_path, log_file) = get_log_file_and_path(opts);
let log_file_setup = log_file.is_some();
Expand Down Expand Up @@ -123,6 +128,7 @@ fn set_up_logging(opts: &Opts) -> (Utf8PathBuf, Option<LevelFilter>) {
tracing_subscriber::registry()
.with(file_log.with_filter(file_envfilter))
.with(stderr_log.with_filter(stderr_envfilter))
.with(indicatif_layer)
// Adds a color_eyre spantrace layer. This isn't used unless we start adding `#[instrument]`
// to functions.
.with(ErrorLayer::default())
Expand Down
6 changes: 4 additions & 2 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ fn run_tasks(
.into_par_iter()
.filter(|(_, task)| task.config.auto_run.unwrap_or(true))
.map(|(_, task)| {
let task_tempdir = create_task_tempdir(temp_dir, task.name.as_str())?;
let task_name = task.name.as_str();
let _span = tracing::info_span!("task", task = task_name).entered();
let task_tempdir = create_task_tempdir(temp_dir, task_name)?;
Ok(run_task(task, env, &task_tempdir))
})
.collect::<Result<Vec<Task>>>()?,
Expand Down Expand Up @@ -315,7 +317,7 @@ fn run_task(mut task: Task, env: &HashMap<String, String>, task_tempdir: &Utf8Pa
task.run(env_fn, env, task_tempdir);
let elapsed_time = now.elapsed();
if elapsed_time > Duration::from_secs(60) {
warn!("Task {} took {:?}", task.name, elapsed_time);
warn!("Task took {elapsed_time:?}");
}
task
}
Expand Down
31 changes: 6 additions & 25 deletions src/tasks/git/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::tasks::git::branch::get_push_branch;
use crate::tasks::git::cherry::unmerged_commits;
use crate::tasks::git::errors::GitError as E;
use crate::utils::files::to_utf8_path;
use camino::Utf8Path;
use color_eyre::eyre::ensure;
use color_eyre::eyre::eyre;
use color_eyre::eyre::Result;
Expand Down Expand Up @@ -50,16 +49,12 @@ pub(super) fn ensure_repo_clean(repo: &Repository) -> Result<()> {
pub(super) fn warn_for_unpushed_changes(
repo: &mut Repository,
user_git_config: &Config,
git_path: &Utf8Path,
) -> Result<()> {
// Warn for uncommitted changes.
{
let statuses = repo_statuses(repo)?;
if !statuses.is_empty() {
warn!(
"Repo {git_path} has uncommitted changes:\n{}",
status_short(repo, &statuses)?
);
warn!("Uncommitted changes:\n{}", status_short(repo, &statuses)?);
}
}

Expand All @@ -71,10 +66,7 @@ pub(super) fn warn_for_unpushed_changes(
true
})?;
if !stash_messages.is_empty() {
warn!(
"Repo {git_path} has stashed changes:\n{:#?}",
stash_messages
);
warn!("Stashed changes:\n{:#?}", stash_messages);
}
}

Expand All @@ -84,27 +76,19 @@ pub(super) fn warn_for_unpushed_changes(
if let Some(push_branch) = get_push_branch(repo, &branch_name, user_git_config)? {
// Warn for any commits not in @{push}
if unmerged_commits(repo, &push_branch, &branch)? {
warn!(
"Repo {git_path} branch '{branch_name}' has changes that aren't in @{{push}}.",
);
warn!("Branch '{branch_name}' has changes that aren't in @{{push}}.",);
}
} else {
match branch.upstream() {
Ok(upstream_branch) => {
// If no push, warn for any commits not in @{upstream}
if unmerged_commits(repo, &upstream_branch, &branch)? {
warn!(
"Repo {git_path} branch '{branch_name}' has changes that aren't in \
@{{upstream}}.",
);
warn!("Branch '{branch_name}' has changes that aren't in @{{upstream}}.",);
}
}
Err(e) if e.code() == ErrorCode::NotFound => {
// Warn for any branches with no @{upstream} or @{push}
warn!(
"Repo {git_path} branch '{branch_name}' has no @{{upstream}} or @{{push}} \
branch.",
);
warn!("Branch '{branch_name}' has no @{{upstream}} or @{{push}} branch.",);
}
Err(e) => {
// Something else went wrong, raise error.
Expand Down Expand Up @@ -136,10 +120,7 @@ pub(super) fn warn_for_unpushed_changes(
}
}
if !unmerged_branches.is_empty() {
warn!(
"Repo {git_path} has unmerged fork branches: {} .",
unmerged_branches.join(" "),
);
warn!("Unmerged fork branches: {} .", unmerged_branches.join(" "),);
}

Ok(())
Expand Down
8 changes: 3 additions & 5 deletions src/tasks/git/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use url::Url;
/// Update a git repo.
pub(crate) fn update(git_config: &GitConfig) -> Result<TaskStatus> {
let now = Instant::now();
let _span = tracing::info_span!("git", repo = &git_config.path.as_str()).entered();
let result = real_update(git_config)
.map(|did_work| {
if did_work {
Expand All @@ -52,10 +53,7 @@ pub(crate) fn update(git_config: &GitConfig) -> Result<TaskStatus> {
let elapsed_time = now.elapsed();
// TODO(gib): configurable logging for long actions.
if elapsed_time > Duration::from_secs(60) {
warn!(
"Git update for {path} took {elapsed_time:?}",
path = git_config.path
);
warn!("Git update took {elapsed_time:?}",);
}
result
}
Expand Down Expand Up @@ -213,7 +211,7 @@ pub(crate) fn real_update(git_config: &GitConfig) -> Result<bool> {
};
drop(default_remote); // Can't mutably use repo while this value is around.
if !newly_created_repo {
warn_for_unpushed_changes(&mut repo, &user_git_config, &git_path)?;
warn_for_unpushed_changes(&mut repo, &user_git_config)?;
}
Ok(did_work)
}
Expand Down
Loading

0 comments on commit 51ad144

Please sign in to comment.