|
1 | 1 | use std::{error::Error, path::PathBuf, str::FromStr, thread}; |
2 | 2 |
|
| 3 | +use chrono::{DateTime, Local, TimeDelta}; |
3 | 4 | use clap::{Args, Command, Parser, Subcommand, ValueHint, builder::TypedValueParser}; |
4 | 5 | use clap_complete::{ArgValueCompleter, CompletionCandidate, Generator, Shell, generate}; |
5 | 6 | use color_eyre::{Report, Result}; |
6 | 7 | use itertools::Itertools; |
| 8 | +use self_update::cargo_crate_version; |
7 | 9 | use strum::VariantNames; |
8 | 10 | use tokio::sync::mpsc; |
9 | 11 |
|
@@ -568,3 +570,45 @@ fn is_bare_string(value_str: &str) -> bool { |
568 | 570 | pub fn print_completions<G: Generator>(generator: G, cmd: &mut Command) { |
569 | 571 | generate(generator, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); |
570 | 572 | } |
| 573 | + |
| 574 | +pub fn update() -> Result<()> { |
| 575 | + let status = self_update::backends::github::Update::configure() |
| 576 | + .repo_owner("SwissDataScienceCenter") |
| 577 | + .repo_name("coman") |
| 578 | + .bin_name("coman") |
| 579 | + .bin_path_in_archive("coman") |
| 580 | + .show_download_progress(true) |
| 581 | + .current_version(cargo_crate_version!()) |
| 582 | + .build()? |
| 583 | + .update()?; |
| 584 | + if status.updated() { |
| 585 | + println!("Successfully updated to version: `{}`", status.version()); |
| 586 | + } else { |
| 587 | + println!("Already up to date at version: `{}`", status.version()); |
| 588 | + } |
| 589 | + Ok(()) |
| 590 | +} |
| 591 | + |
| 592 | +pub async fn check_update() -> () { |
| 593 | + let Ok(config) = Config::new() else { |
| 594 | + return; |
| 595 | + }; |
| 596 | + let data_dir = get_data_dir(); |
| 597 | + let stamp_path = data_dir.join("selfupdate.stamp"); |
| 598 | + let Ok(update_stamp) = std::fs::read_to_string(&stamp_path) else { |
| 599 | + std::fs::write(&stamp_path, Local::now().to_rfc3339()).unwrap(); |
| 600 | + return; |
| 601 | + }; |
| 602 | + let Ok(update_stamp) = DateTime::parse_from_rfc3339(&update_stamp) else { |
| 603 | + return; |
| 604 | + }; |
| 605 | + |
| 606 | + let now = Local::now(); |
| 607 | + if now.naive_local() - update_stamp.naive_local() |
| 608 | + > TimeDelta::hours(config.values.update_check_interval_hours as i64) |
| 609 | + { |
| 610 | + println!("checking for updates"); |
| 611 | + let _ = update(); |
| 612 | + std::fs::write(&stamp_path, Local::now().to_rfc3339()).unwrap(); |
| 613 | + } |
| 614 | +} |
0 commit comments