Skip to content

Commit

Permalink
feat: update notice in background
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Jun 16, 2023
1 parent eb3553c commit a866efe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/commands/update/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn check_version(current: &Version, beta: bool) -> Result<(bool, Versi
// static time to check for updates
const HOUR_IN_SECONDS: u64 = 60 * 60;

pub async fn version_notice(mut ctx: &mut Context) -> Result<()> {
pub async fn version_notice(mut ctx: Context) -> Result<()> {
let now = now_secs()?;

let last_check = ctx
Expand Down
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,33 @@ pub async fn run() -> Result<()> {

utils::sudo::fix().await?;

let mut state = State::new(StateOptions {
let state = State::new(StateOptions {
override_project: std::env::var("PROJECT_ID").ok().or(cli.project),
override_token: std::env::var("TOKEN").ok(),
})
.await?;

match cli.commands {
#[cfg(feature = "update")]
Commands::Update(_) => None,
Commands::Update(_) => {}

// do not show the notice if we are in completions mode
// since it could break the shell
Commands::Completions(_) => None,
Commands::Completions(_) => {}

// only show the notice if we are not in debug mode or in CI
_ if cfg!(debug_assertions) || state.is_ci => None,

// its okay for the notice to fail
_ => version_notice(&mut state.ctx).await.ok(),
_ if cfg!(debug_assertions) || state.is_ci => {}

// async block to spawn the update check in the background :)
_ => {
let ctx = state.ctx.clone();

tokio::spawn(async move {
if let Err(e) = version_notice(ctx).await {
log::debug!("Failed to check for updates: {e}");
}
});
}
};

if let Err(error) = handle_command(cli.commands, state).await {
Expand Down

0 comments on commit a866efe

Please sign in to comment.