Skip to content

Commit

Permalink
Add check-update command
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb9 committed May 3, 2024
1 parent 8f80129 commit e3a9c56
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 6 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Duration> {
Expand Down
46 changes: 46 additions & 0 deletions src/commands/check_update.rs
Original file line number Diff line number Diff line change
@@ -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<C: HttpClient, I: Io>(
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::<dto::Crate>)
.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,
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -25,6 +25,7 @@ pub async fn run<I: Io, C: HttpClient, S: CookieStore, F: Fs>(
io: &mut I,
client: &mut CookieClient<C, S>,
fs: &F,
installed_version: &str,
) -> Result<()> {
let mut conf = Conf::try_load(fs).unwrap_or_else(Conf::new);
if let Some(session) = &conf.session {
Expand All @@ -50,7 +51,6 @@ pub async fn run<I: Io, C: HttpClient, S: CookieStore, F: Fs>(
)
.await
}
Command::Status => status::handle(&conf, io),
Command::List { album_name } => {
list::handle(album_name.as_str(), &conf, &client.client, io).await
}
Expand All @@ -68,5 +68,7 @@ pub async fn run<I: Io, C: HttpClient, S: CookieStore, F: Fs>(
.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,
}
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

0 comments on commit e3a9c56

Please sign in to comment.