Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an update checker #222

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]

members = [
"steamguard"
]
members = ["steamguard"]

[package]
name = "steamguard-cli"
Expand All @@ -16,8 +14,9 @@ repository = "https://github.com/dyc3/steamguard-cli"
license = "GPL-3.0-or-later"

[features]
default = ["qr"]
default = ["qr", "updater"]
qr = ["qrcode"]
updater = ["update-informer"]

# [[bin]]
# name = "steamguard-cli"
Expand Down Expand Up @@ -61,6 +60,7 @@ gethostname = "0.4.3"
secrecy = { version = "0.8", features = ["serde"] }
zeroize = "^1.4.3"
serde_path_to_error = "0.1.11"
update-informer = { version = "1.0.0", optional = true, default-features = false, features = ["github"] }

[dev-dependencies]
tempdir = "0.3"
Expand Down
8 changes: 8 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ pub(crate) struct GlobalArgs {
pub passkey: Option<String>,
#[clap(short, long, arg_enum, default_value_t=Verbosity::Info, help = "Set the log level. Be warned, trace is capable of printing sensitive data.")]
pub verbosity: Verbosity,

#[cfg(feature = "updater")]
#[clap(
long,
help = "Disable checking for updates.",
long_help = "Disable checking for updates. By default, steamguard-cli will check for updates every now and then. This can be disabled with this flag."
)]
pub no_update_check: bool,
}

#[derive(Debug, Clone, Parser)]
Expand Down
51 changes: 38 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,54 @@ mod encryption;
mod errors;
mod secret_string;
pub(crate) mod tui;
#[cfg(feature = "updater")]
mod updater;

fn main() {
std::process::exit(match run() {
let args = commands::Args::parse();

stderrlog::new()
.verbosity(args.global.verbosity as usize)
.module(module_path!())
.module("steamguard")
.init()
.unwrap();
debug!("{:?}", args);
#[cfg(feature = "updater")]
let should_do_update_check = !args.global.no_update_check;

let exit_code = match run(args) {
Ok(_) => 0,
Err(e) => {
error!("{:?}", e);
255
}
});
}
};

fn run() -> anyhow::Result<()> {
let args = commands::Args::parse();
info!("{:?}", args);
#[cfg(feature = "updater")]
if should_do_update_check {
match updater::check_for_update() {
Ok(Some(version)) => {
eprintln!();
info!(
"steamguard-cli v{} is available. Download it here: https://github.com/dyc3/steamguard-cli/releases",
version
);
}
Ok(None) => {
debug!("No update available");
}
Err(e) => {
warn!("Failed to check for updates: {}", e);
}
}
}

let globalargs = args.global;
std::process::exit(exit_code);
}

stderrlog::new()
.verbosity(globalargs.verbosity as usize)
.module(module_path!())
.module("steamguard")
.init()
.unwrap();
fn run(args: commands::Args) -> anyhow::Result<()> {
let globalargs = args.global;

let cmd: CommandType = match args.sub.unwrap_or(Subcommands::Code(args.code)) {
Subcommands::Debug(args) => CommandType::Const(Box::new(args)),
Expand Down
46 changes: 46 additions & 0 deletions src/updater.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::time::Duration;

use serde::de::DeserializeOwned;
use update_informer::{
http_client::{HeaderMap, HttpClient},
registry, Check, Version,
};

use crate::debug;

pub fn check_for_update() -> update_informer::Result<Option<Version>> {
let name = "dyc3/steamguard-cli";
let version = env!("CARGO_PKG_VERSION");
debug!("Checking for updates to {} v{}", name, version);
let informer = update_informer::new(registry::GitHub, name, version)
.http_client(ReqwestHttpClient)
.interval(Duration::from_secs(60 * 60 * 24 * 2));

informer.check_version()
}

struct ReqwestHttpClient;

impl HttpClient for ReqwestHttpClient {
fn get<T: DeserializeOwned>(
url: &str,
timeout: Duration,
headers: HeaderMap,
) -> update_informer::Result<T> {
let mut req = reqwest::blocking::Client::builder()
.timeout(timeout)
.build()?
.get(url)
.header(reqwest::header::USER_AGENT, "steamguard-cli");

for (key, value) in headers {
req = req.header(key, value);
}

let resp = req.send()?;
debug!("Update check response status: {:?}", resp.status());
let json = resp.json()?;

Ok(json)
}
}