From 36c9d0f0e59274e704e0e1908ef75a752d1322a0 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 28 Oct 2022 23:16:00 +0200 Subject: [PATCH] fix bugs --- Dockerfile | 6 +++--- twatch/src/main.rs | 11 +++++++---- twatch_core/src/lib.rs | 37 ++++++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 085adf8..28e70d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:slim-bullseye AS builder +FROM harbor.front.kjuulh.io/docker-proxy/library/rust:slim-bullseye AS builder COPY . /app/builder/ @@ -6,11 +6,11 @@ WORKDIR /app/builder/ RUN cargo build --release -FROM debian:bullseye-slim +FROM harbor.front.kjuulh.io/docker-proxy/library/debian:bullseye-slim COPY --from=builder /app/builder/target/release/ /app/ WORKDIR /app/ ENV RUST_LOG info -CMD ["./twatch"] \ No newline at end of file +CMD ["./twatch"] diff --git a/twatch/src/main.rs b/twatch/src/main.rs index 1e86f0c..53ecba2 100644 --- a/twatch/src/main.rs +++ b/twatch/src/main.rs @@ -1,19 +1,22 @@ +use log::info; use std::env; use std::env::VarError; use std::time::Duration; -use log::info; -use twatch_core::App; use tokio::time::sleep; +use twatch_core::App; #[tokio::main] async fn main() -> Result<(), VarError> { env_logger::init(); - dotenv::dotenv(); + dotenv::dotenv().expect("to load .env"); let url = env::var("T_URL")?; let user = env::var("T_USER")?; let password = env::var("T_PASSWORD")?; - let cycle_seconds = env::var("T_CYCLE").unwrap_or("30".to_string()).parse::().unwrap_or(30); + let cycle_seconds = env::var("T_CYCLE") + .unwrap_or("30".to_string()) + .parse::() + .unwrap_or(30); loop { info!("running clean up cycle"); diff --git a/twatch_core/src/lib.rs b/twatch_core/src/lib.rs index 46102e3..25499ae 100644 --- a/twatch_core/src/lib.rs +++ b/twatch_core/src/lib.rs @@ -1,6 +1,6 @@ +use log::info; +use transmission_rpc::types::{BasicAuth, Id, Result, Torrent, Torrents}; use transmission_rpc::TransClient; -use transmission_rpc::types::{Result, BasicAuth, Torrent, Torrents, Id}; -use log::{info}; pub struct App { client: TransClient, @@ -8,29 +8,36 @@ pub struct App { impl App { pub fn new(url: &String, username: &String, password: &String) -> Self { - let auth = BasicAuth { user: username.clone(), password: password.clone() }; + let auth = BasicAuth { + user: username.clone(), + password: password.clone(), + }; let client = TransClient::with_auth(url.as_str(), auth); - Self { - client - } + Self { client } } - pub async fn run(&self) -> Result<()> { + pub async fn run(mut self) -> Result<()> { let response = self.client.torrent_get(None, None).await; match response { Ok(res) => self.scan_files(res.arguments).await.unwrap(), - Err(e) => println!("{}", e.to_string()) + Err(e) => println!("{}", e.to_string()), } return Ok(()); } - async fn scan_files(&self, torrents: Torrents) -> Result<()> { + async fn scan_files(self, torrents: Torrents) -> Result<()> { let mut torrents_to_remove = Vec::new(); for torrent in torrents.torrents { match (torrent.id, torrent.is_stalled) { (Some(id), Some(true)) => { - info!("processing: (id: {}, name: {})", id, torrent.name.unwrap_or("name could not be found from torrent".to_string())); + info!( + "processing: (id: {}, name: {})", + id, + torrent + .name + .unwrap_or("name could not be found from torrent".to_string()) + ); torrents_to_remove.push(id); } _ => { @@ -49,7 +56,11 @@ impl App { return Ok(()); } - async fn clean_up_torrents(&self, torrent_ids: Vec) { - self.client.torrent_remove(torrent_ids.iter().map(|ti| { Id::Id(*ti) }).collect(), true).await; + async fn clean_up_torrents(mut self, torrent_ids: Vec) { + self.client + .torrent_remove(torrent_ids.iter().map(|ti| Id::Id(*ti)).collect(), true) + .await + .expect("to remove torrent"); } -} \ No newline at end of file +} +