Skip to content

Commit

Permalink
feat: Show in output when upgrade skipped due no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian May committed Sep 5, 2024
1 parent ab4419f commit 311dddf
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 54 deletions.
52 changes: 27 additions & 25 deletions src/helm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,6 @@ pub async fn template(
pub enum DiffResult {
NoChanges,
Changes,
Errors,
Unknown,
}

/// Run the helm diff command.
Expand Down Expand Up @@ -421,36 +419,40 @@ pub async fn diff(
let i_result = HelmResult::from_result(installation, result, Command::Diff);

// Evaluate the detailed exit code - any non-zero exit code indicates changes (1) or errors (2).
let diff_result = if let Ok(command_success) = &i_result.result {
match command_success.exit_code {
0 => {
debug!(tx, "No changes detected!").await; // Exit code 0 indicates no changes.
DiffResult::NoChanges
}
1 => {
error!(tx, "Errors encountered!").await; // Exit code 1 indicates errors.
DiffResult::Errors
}
2 => {
debug!(tx, "Changes detected!").await; // Exit code 2 indicates changes.
DiffResult::Changes
}
_ => {
error!(tx, "Unknown exit code").await; // Any other exit code is considered unknown.
DiffResult::Unknown
}
let diff_result = match &i_result.result {
Ok(CommandSuccess { exit_code: 0, .. }) => {
debug!(tx, "No changes detected!").await; // Exit code 0 indicates no changes.
Ok(DiffResult::NoChanges)
}
Ok(CommandSuccess { exit_code: 1, .. }) => {
error!(tx, "errors encountered!").await; // exit code 1 indicates errors.
Err(anyhow::anyhow!("diff operation failed"))
}
Ok(CommandSuccess { exit_code: 2, .. }) => {
debug!(tx, "Changes detected!").await; // Exit code 2 indicates changes.
Ok(DiffResult::Changes)
}
Ok(CommandSuccess { .. }) => {
error!(tx, "Unknown exit code").await; // Any other exit code is considered unknown.
Err(anyhow::anyhow!(
"diff operation returned unknown error code"
))
}
Err(err) => {
debug!(tx, "Other exception encountered").await; // If the command result is an error, return Unknown.
Err(anyhow::anyhow!(
"diff operation failed: {}",
err.to_string()
))
}
} else {
debug!(tx, "Other exception encountered").await; // If the command result is an error, return Unknown.
DiffResult::Unknown
};

// Wrap the HelmResult in an Arc and send it via the MultiOutput channel.
let i_result = Arc::new(i_result);
tx.send(Message::InstallationResult(i_result)).await;
tx.send(Message::InstallationResult(i_result.clone())).await;

// Return the diff result.
Ok(diff_result)
Ok(diff_result?)
}

/// Run the helm upgrade command.
Expand Down
31 changes: 15 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use depends::{is_depends_ok, HashIndex, InstallationSet};

mod output;
use output::{error, trace, warning};
use output::{Message, MultiOutput, Output, Sender};
use output::{JobSuccess, Message, MultiOutput, Output, Sender};

mod config;
use config::Release;
Expand Down Expand Up @@ -194,7 +194,7 @@ async fn run_job(
installation: &Arc<Installation>,
tx: &MultiOutput,
upgrade_control: &UpgradeControl,
) -> Result<()> {
) -> Result<JobSuccess> {
match command {
Request::Upgrade { .. } => {
let diff_result = helm::diff(installation, helm_repos, tx).await?;
Expand All @@ -204,43 +204,42 @@ async fn run_job(
UpgradeControl::BypassAndAssumeYes => {
helm::upgrade(installation, helm_repos, tx, true).await?;
helm::upgrade(installation, helm_repos, tx, false).await?;
Ok(JobSuccess::Completed)
}
UpgradeControl::BypassAndAssumeNo | UpgradeControl::Normal => {
// Do nothing, as these are implicit or default cases
Ok(JobSuccess::Skipped)
}
}
}
DiffResult::Changes => {
helm::upgrade(installation, helm_repos, tx, true).await?;
helm::upgrade(installation, helm_repos, tx, false).await?;
}
DiffResult::Errors | DiffResult::Unknown => {
// Handle errors or unknown cases if needed.
Ok(JobSuccess::Completed)
}
}
Ok(())
}
Request::Diff { .. } => {
helm::diff(installation, helm_repos, tx).await?;
Ok(())
Ok(JobSuccess::Completed)
}
Request::Test { .. } => {
helm::outdated(installation, helm_repos, tx).await?;
helm::lint(installation, tx).await?;
helm::template(installation, helm_repos, tx).await?;
Ok(())
Ok(JobSuccess::Completed)
}
Request::Template { .. } => {
helm::template(installation, helm_repos, tx).await?;
Ok(())
Ok(JobSuccess::Completed)
}
Request::Outdated { .. } => {
helm::outdated(installation, helm_repos, tx).await?;
Ok(())
Ok(JobSuccess::Completed)
}
Request::Update { updates, .. } => {
helm::update(installation, tx, updates).await?;
Ok(())
Ok(JobSuccess::Completed)
}
}
}
Expand Down Expand Up @@ -905,17 +904,17 @@ async fn worker_thread(
// Execute the job
let result = run_job(command, helm_repos, &install, output, upgrade_control).await;
match &result {
Ok(()) => {
tx_dispatch
.send(Dispatch::Done(HashIndex::get_hash_index(&install)))
.await?;
}
Ok(_) => {}
Err(err) => {
error!(output, "job failed: {err}").await;
errors = true;
}
}

tx_dispatch
.send(Dispatch::Done(HashIndex::get_hash_index(&install)))
.await?;

// Update UI
let stop = Instant::now();
output
Expand Down
8 changes: 7 additions & 1 deletion src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub mod slack;
pub mod text;
pub mod tui;

#[derive(Clone, Copy, Debug)]
pub enum JobSuccess {
Completed,
Skipped,
}

/// A message to the output module.
#[derive(Debug)]
pub enum Message {
Expand All @@ -33,7 +39,7 @@ pub enum Message {
/// Notification that we started a job.
StartedJob(Arc<Installation>, Instant),
/// Notification that we finished a job.
FinishedJob(Arc<Installation>, Result<(), String>, Duration),
FinishedJob(Arc<Installation>, Result<JobSuccess, String>, Duration),

/// A Log entry was logged.
Log(LogEntry),
Expand Down
4 changes: 3 additions & 1 deletion src/output/slack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2022 Electronic Arts, Inc. All rights reserved.
use super::JobSuccess;
use super::Message;
use super::Output;
use super::Sender;
Expand Down Expand Up @@ -537,7 +538,8 @@ async fn process_message(msg: &Arc<Message>, state: &mut State, slack: &SlackSta
}
Message::FinishedJob(installation, result, duration) => {
let status = match result {
Ok(()) => Status::Complete,
Ok(JobSuccess::Completed) => Status::Complete,
Ok(JobSuccess::Skipped) => Status::Skipped,
Err(_) => Status::Failed,
};
state
Expand Down
19 changes: 14 additions & 5 deletions src/output/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2022 Electronic Arts, Inc. All rights reserved.
use super::JobSuccess;
use super::Message;
use super::Output;
use super::Sender;
Expand Down Expand Up @@ -234,7 +235,12 @@ fn process_message(msg: &Arc<Message>, state: &mut State) {
}
Message::Log(entry) => println!("{} {} {}", entry.level, entry.name, entry.message),
Message::SkippedJob(installation) => {
state.results.insert(installation.id, JobStatus::Skipped);
state.results.insert(
installation.id,
JobStatus::Skipped {
duration: Duration::ZERO,
},
);
state.jobs.push(installation.clone());
}
Message::InstallationVersion(installation, our_version, upstream_version) => {
Expand All @@ -254,7 +260,10 @@ fn process_message(msg: &Arc<Message>, state: &mut State) {
}
Message::FinishedJob(installation, result, duration) => {
let status = match result {
Ok(()) => JobStatus::Complete {
Ok(JobSuccess::Completed) => JobStatus::Complete {
duration: *duration,
},
Ok(JobSuccess::Skipped) => JobStatus::Skipped {
duration: *duration,
},
Err(_) => JobStatus::Failed {
Expand All @@ -281,7 +290,7 @@ enum JobStatus {
Pending,
InProgress,
Complete { duration: Duration },
Skipped,
Skipped { duration: Duration },
Failed { duration: Duration },
}

Expand All @@ -292,7 +301,7 @@ impl JobStatus {
JobStatus::Pending => None,
JobStatus::InProgress { .. } => None,
JobStatus::Complete { duration } => Some(*duration),
JobStatus::Skipped => None,
JobStatus::Skipped { duration } => Some(*duration),
JobStatus::Failed { duration } => Some(*duration),
}
}
Expand All @@ -302,7 +311,7 @@ impl JobStatus {
JobStatus::Pending => Status::Pending,
JobStatus::InProgress { .. } => Status::InProgress,
JobStatus::Complete { .. } => Status::Complete,
JobStatus::Skipped => Status::Skipped,
JobStatus::Skipped { .. } => Status::Skipped,
JobStatus::Failed { .. } => Status::Failed,
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/output/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use crate::logging::LogEntry;
use crate::logging::LogLevel;
use crate::Request;

use super::JobSuccess;
use super::Message;
use super::Output;
use super::Sender;
Expand Down Expand Up @@ -335,8 +336,13 @@ impl HasRows for Installation {
// Some(JobStatus::Skipped) => Style::default().fg(Color::DarkGray),
Some(JobStatus::New) => Style::default().fg(Color::White),
Some(JobStatus::Started(_)) => Style::default().fg(Color::Yellow),
Some(JobStatus::Finished(true, _)) => Style::default().fg(Color::Green),
Some(JobStatus::Finished(false, _)) => Style::default().fg(Color::Red),
Some(JobStatus::Finished(Ok(JobSuccess::Completed), _)) => {
Style::default().fg(Color::Green)
}
Some(JobStatus::Finished(Ok(JobSuccess::Skipped), _)) => {
Style::default().fg(Color::DarkGray)
}
Some(JobStatus::Finished(_, _)) => Style::default().fg(Color::Red),
None => Style::default().fg(Color::Blue),
};
let duration = match status {
Expand Down Expand Up @@ -366,8 +372,13 @@ impl HasMultilineText for Installation {
// Some(JobStatus::Skipped) => Style::default().fg(Color::DarkGray),
Some(JobStatus::New) => Style::default().fg(Color::White),
Some(JobStatus::Started(_)) => Style::default().fg(Color::Yellow),
Some(JobStatus::Finished(true, _)) => Style::default().fg(Color::Green),
Some(JobStatus::Finished(false, _)) => Style::default().fg(Color::Red),
Some(JobStatus::Finished(Ok(JobSuccess::Completed), _)) => {
Style::default().fg(Color::Green)
}
Some(JobStatus::Finished(Ok(JobSuccess::Skipped), _)) => {
Style::default().fg(Color::DarkGray)
}
Some(JobStatus::Finished(_, _)) => Style::default().fg(Color::Red),
None => Style::default().fg(Color::Blue),
};
Spans::from(Span::styled(self.name.clone(), style))
Expand Down Expand Up @@ -513,7 +524,7 @@ enum JobStatus {
// Skipped,
New,
Started(Instant),
Finished(bool, Duration),
Finished(Result<JobSuccess, String>, Duration),
}

enum DisplayMode {
Expand Down Expand Up @@ -709,7 +720,7 @@ fn process_message(msg: &Arc<Message>, state: &mut State) {
state.logs.add_log(log!(LogLevel::Info, &str));
state
.job_status
.insert(i.id, JobStatus::Finished(result.is_ok(), *duration));
.insert(i.id, JobStatus::Finished(result.clone(), *duration));
if result.is_err() {
state.has_errors = true;
}
Expand Down

0 comments on commit 311dddf

Please sign in to comment.