From e3a9c561711ff2cc08e88dbc611e2334c1963deb Mon Sep 17 00:00:00 2001 From: Piotr Karasinski <7767011+Caleb9@users.noreply.github.com> Date: Fri, 3 May 2024 16:32:22 +0200 Subject: [PATCH] Add check-update command --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 9 ++++--- src/commands/check_update.rs | 46 ++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/lib.rs | 6 +++-- src/main.rs | 9 ++++++- 7 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/commands/check_update.rs diff --git a/Cargo.lock b/Cargo.lock index cf6d6d2..6b17a19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1449,7 +1449,7 @@ checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" [[package]] name = "syno-photos-util" -version = "0.1.2" +version = "0.2.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index ba7d957..7684324 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "syno-photos-util" -version = "0.1.2" +version = "0.2.0" edition = "2021" description = "Helper for a number of tasks unavailable in Synology Photos web interface" license = "GPL-3.0-or-later" diff --git a/src/cli.rs b/src/cli.rs index 5df52fc..ae8149d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -79,9 +79,6 @@ pub enum Command { folder_path: String, }, - /// Check DSM sign-in status - Status, - /// Sign out of DSM /// /// Removes session key from $HOME/.syno-photo-util file @@ -93,6 +90,12 @@ pub enum Command { #[arg(long)] forget: bool, }, + + /// Check DSM sign-in status + Status, + + /// Check if new version is available + CheckUpdate, } fn try_parse_duration(arg: &str) -> Result { diff --git a/src/commands/check_update.rs b/src/commands/check_update.rs new file mode 100644 index 0000000..a9fbc9a --- /dev/null +++ b/src/commands/check_update.rs @@ -0,0 +1,46 @@ +use crate::http::{HttpClient, HttpResponse}; +use crate::io::Io; +use anyhow::{anyhow, bail, Result}; +use io::Write; +use std::io; + +pub async fn handle( + installed_version: &str, + client: &C, + io: &mut I, +) -> Result<()> { + let response = client + .get("https://index.crates.io/sy/no/syno-photos-util") + .await?; + let status = response.status(); + if !status.is_success() { + bail!(status + .canonical_reason() + .unwrap_or(status.as_str()) + .to_string()) + } + let remote_crate = response + .text() + .await? + .lines() + .map(serde_json::from_str::) + .filter_map(|r| r.ok()) + .rfind(|c| !c.yanked) + .ok_or(anyhow!("Unable to read creates.io response"))?; + if remote_crate.vers != installed_version { + writeln!(io.stdout(), "Version {} is available!", remote_crate.vers)?; + } else { + writeln!(io.stdout(), "Everything up to date")?; + } + Ok(()) +} + +mod dto { + use serde::Deserialize; + + #[derive(Debug, PartialEq, Deserialize)] + pub struct Crate { + pub vers: String, + pub yanked: bool, + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8580cb2..b18899a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -9,6 +9,7 @@ use syno_api::foto::search::dto::Search; use syno_api::foto::setting::user::dto::UserSettings; mod api_client; +pub mod check_update; mod error; pub mod export; pub mod list; diff --git a/src/lib.rs b/src/lib.rs index 58fe137..da0c25c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use crate::http::HeaderValue; pub use crate::{cli::Cli, fs::FsImpl, http::CookieClient, io::IoImpl}; use crate::{ cli::Command, - commands::{export, list, login, logout, status}, + commands::{check_update, export, list, login, logout, status}, conf::Conf, fs::Fs, http::{CookieStore, HttpClient}, @@ -25,6 +25,7 @@ pub async fn run( io: &mut I, client: &mut CookieClient, fs: &F, + installed_version: &str, ) -> Result<()> { let mut conf = Conf::try_load(fs).unwrap_or_else(Conf::new); if let Some(session) = &conf.session { @@ -50,7 +51,6 @@ pub async fn run( ) .await } - Command::Status => status::handle(&conf, io), Command::List { album_name } => { list::handle(album_name.as_str(), &conf, &client.client, io).await } @@ -68,5 +68,7 @@ pub async fn run( .await } Command::Logout { forget } => logout::handle(conf, forget, fs), + Command::Status => status::handle(&conf, io), + Command::CheckUpdate => check_update::handle(installed_version, &client.client, io).await, } } diff --git a/src/main.rs b/src/main.rs index c70cb98..f9837fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,11 @@ async fn main() -> Result<()> { .without_timestamps() .env() .init()?; + let cli = Cli::parse(); + let mut io = IoImpl::new(); + let cookie_store = Arc::new(Jar::default()); let mut client = CookieClient { client: ClientBuilder::default() @@ -24,5 +27,9 @@ async fn main() -> Result<()> { .build()?, cookie_store, }; - syno_photos_util::run(cli, &mut io, &mut client, &FsImpl).await + + /* This crate version */ + let installed_version = env!("CARGO_PKG_VERSION"); + + syno_photos_util::run(cli, &mut io, &mut client, &FsImpl, installed_version).await }