Skip to content

Commit

Permalink
Merge pull request #1413 from siddhantk232/feat/check-for-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu authored Oct 23, 2023
2 parents 839b222 + 1990a28 commit daef031
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions fastn-core/tests/01-help/cmd.p1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Commands:
help Print this message or the help of the given subcommand(s)

Options:
-v Sets the level of verbosity
-h, --help Print help
-V, --version Print version
-c, --check-for-updates Check for updates
-v Sets the level of verbosity
-h, --help Print help
-V, --version Print version
3 changes: 3 additions & 0 deletions fastn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ colored.workspace = true
fastn-cloud.workspace = true
fastn-observer.workspace = true
fastn-core.workspace = true
futures.workspace = true
reqwest.workspace = true
serde.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
Expand Down
61 changes: 59 additions & 2 deletions fastn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@ pub enum Error {

async fn async_main() -> Result<(), Error> {
let matches = app(version()).get_matches();

if cloud_commands(&matches).await? {
return Ok(());
}
fastn_core_commands(&matches).await?;
Ok(())

futures::try_join!(
fastn_core_commands(&matches),
check_for_update_cmd(&matches)
)?;

match std::env::var("FASTN_CHECK_FOR_UPDATES") {
Ok(val) => {
if val != "false" && !matches.get_flag("check-for-updates") {
check_for_update(false).await?;
}
Ok(())
}
Err(_) => Ok(()),
}
}

async fn cloud_commands(matches: &clap::ArgMatches) -> Result<bool, commands::cloud::Error> {
Expand Down Expand Up @@ -241,12 +255,55 @@ async fn fastn_core_commands(matches: &clap::ArgMatches) -> fastn_core::Result<(
return fastn_core::post_build_check(&config).await;
}

if matches.get_flag("check-for-updates") {
return check_for_update(true).await;
}

unreachable!("No subcommand matched");
}

async fn check_for_update_cmd(matches: &clap::ArgMatches) -> fastn_core::Result<()> {
if matches.get_flag("check-for-updates") {
check_for_update(false).await?;
}

Ok(())
}

async fn check_for_update(report: bool) -> fastn_core::Result<()> {
#[derive(serde::Deserialize, Debug)]
struct GithubRelease {
tag_name: String,
}

let url = "https://api.github.com/repos/fastn-stack/fastn/releases/latest";
let release: GithubRelease = reqwest::Client::new()
.get(url)
.header(reqwest::header::ACCEPT, "application/vnd.github+json")
.header(reqwest::header::USER_AGENT, "fastn")
.send()
.await?
.json()
.await?;

let current_version = version();

if release.tag_name != current_version {
println!(
"You are using fastn {}, and latest release is {}, visit https://fastn.com/install/ to learn how to upgrade.",
current_version, release.tag_name
);
} else if report {
println!("You are using the latest release of fastn.");
}

Ok(())
}

fn app(version: &'static str) -> clap::Command {
clap::Command::new("fastn: Full-stack Web Development Made Easy")
.version(version)
.arg(clap::arg!(-c --"check-for-updates" "Check for updates"))
.arg_required_else_help(true)
.arg(clap::arg!(verbose: -v "Sets the level of verbosity"))
.arg(clap::arg!(--test "Runs the command in test mode").hide(true))
Expand Down

0 comments on commit daef031

Please sign in to comment.