From 6bdc561f696fc7048a771a0678a4ebc8ca47a77f Mon Sep 17 00:00:00 2001 From: "Oleksandr.Zoria" Date: Wed, 14 Aug 2024 16:21:25 +0300 Subject: [PATCH 1/4] feat: print config --- rm-main/src/cli.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/rm-main/src/cli.rs b/rm-main/src/cli.rs index 0aacce7..e3ca301 100644 --- a/rm-main/src/cli.rs +++ b/rm-main/src/cli.rs @@ -11,21 +11,48 @@ use crate::transmission; #[derive(Parser)] #[command(version, about)] +#[derive(Debug)] pub struct Args { #[command(subcommand)] pub command: Option, } +#[derive(Debug)] +pub enum ConfigType { + DefaultConfig, + DefaultKeymap +} + #[derive(Subcommand)] +#[derive(Debug)] pub enum Commands { AddTorrent { torrent: String }, FetchRss { url: String, filter: Option }, + PrintDefaultConfig { }, + PrintDefaultKeyMap { } } pub async fn handle_command(config: &Config, command: Commands) -> Result<()> { match command { Commands::AddTorrent { torrent } => add_torrent(config, torrent).await?, Commands::FetchRss { url, filter } => fetch_rss(config, &url, filter.as_deref()).await?, + Commands::PrintDefaultConfig { } => print_config(ConfigType::DefaultConfig).await?, + Commands::PrintDefaultKeyMap { } => print_config(ConfigType::DefaultKeymap).await?, + } + Ok(()) +} + +async fn print_config(config_type: ConfigType) -> Result<()> { + let conf_path: &str; + match config_type { + ConfigType::DefaultConfig => conf_path = "./rm-config/defaults/config.toml", + ConfigType::DefaultKeymap => conf_path = "./rm-config/defaults/keymap.toml", + } + let mut file = File::open(conf_path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + for line in contents.lines() { + println!("{}", line) } Ok(()) } From 87f92972440cd876cb37dfb962306fd581a0bc04 Mon Sep 17 00:00:00 2001 From: "Oleksandr.Zoria" Date: Thu, 15 Aug 2024 14:39:44 +0300 Subject: [PATCH 2/4] fix: refactored cli logic --- rm-config/src/keymap/mod.rs | 2 +- rm-config/src/main_config.rs | 2 +- rm-main/src/cli.rs | 23 ++++------------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/rm-config/src/keymap/mod.rs b/rm-config/src/keymap/mod.rs index d7d6658..ee20840 100644 --- a/rm-config/src/keymap/mod.rs +++ b/rm-config/src/keymap/mod.rs @@ -260,7 +260,7 @@ impl Default for KeyModifier { impl KeymapConfig { pub const FILENAME: &'static str = "keymap.toml"; - const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); + pub const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); pub fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-config/src/main_config.rs b/rm-config/src/main_config.rs index a0571ac..29f5551 100644 --- a/rm-config/src/main_config.rs +++ b/rm-config/src/main_config.rs @@ -80,7 +80,7 @@ impl Default for TorrentsTab { impl MainConfig { pub(crate) const FILENAME: &'static str = "config.toml"; - const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); + pub const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); pub(crate) fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-main/src/cli.rs b/rm-main/src/cli.rs index e3ca301..3d2103e 100644 --- a/rm-main/src/cli.rs +++ b/rm-main/src/cli.rs @@ -28,31 +28,16 @@ pub enum ConfigType { pub enum Commands { AddTorrent { torrent: String }, FetchRss { url: String, filter: Option }, - PrintDefaultConfig { }, - PrintDefaultKeyMap { } + PrintDefaultConfig, + PrintDefaultKeymap } pub async fn handle_command(config: &Config, command: Commands) -> Result<()> { match command { Commands::AddTorrent { torrent } => add_torrent(config, torrent).await?, Commands::FetchRss { url, filter } => fetch_rss(config, &url, filter.as_deref()).await?, - Commands::PrintDefaultConfig { } => print_config(ConfigType::DefaultConfig).await?, - Commands::PrintDefaultKeyMap { } => print_config(ConfigType::DefaultKeymap).await?, - } - Ok(()) -} - -async fn print_config(config_type: ConfigType) -> Result<()> { - let conf_path: &str; - match config_type { - ConfigType::DefaultConfig => conf_path = "./rm-config/defaults/config.toml", - ConfigType::DefaultKeymap => conf_path = "./rm-config/defaults/keymap.toml", - } - let mut file = File::open(conf_path)?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - for line in contents.lines() { - println!("{}", line) + Commands::PrintDefaultConfig { } => println!("{}", rm_config::main_config::MainConfig::DEFAULT_CONFIG), + Commands::PrintDefaultKeymap { } => println!("{}", rm_config::keymap::KeymapConfig::DEFAULT_CONFIG), } Ok(()) } From bb0d09d28e93da8972d59694eb08f8e2827a77b7 Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Thu, 15 Aug 2024 17:00:51 +0200 Subject: [PATCH 3/4] feat: default config printing in CLI --- rm-config/src/keymap/mod.rs | 2 +- rm-config/src/main_config.rs | 2 +- rm-main/src/cli.rs | 119 +++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 rm-main/src/cli.rs diff --git a/rm-config/src/keymap/mod.rs b/rm-config/src/keymap/mod.rs index 74c34cf..3985e70 100644 --- a/rm-config/src/keymap/mod.rs +++ b/rm-config/src/keymap/mod.rs @@ -277,7 +277,7 @@ impl Default for KeyModifier { impl KeymapConfig { pub const FILENAME: &'static str = "keymap.toml"; - const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); + pub const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); pub fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-config/src/main_config.rs b/rm-config/src/main_config.rs index 3103966..b1d6960 100644 --- a/rm-config/src/main_config.rs +++ b/rm-config/src/main_config.rs @@ -100,7 +100,7 @@ impl Default for SearchTab { impl MainConfig { pub(crate) const FILENAME: &'static str = "config.toml"; - const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); + pub const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); pub(crate) fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-main/src/cli.rs b/rm-main/src/cli.rs new file mode 100644 index 0000000..3d2103e --- /dev/null +++ b/rm-main/src/cli.rs @@ -0,0 +1,119 @@ +use std::{fs::File, io::Read}; + +use anyhow::{bail, Result}; +use base64::Engine; +use clap::{Parser, Subcommand}; +use regex::Regex; +use rm_config::Config; +use transmission_rpc::types::TorrentAddArgs; + +use crate::transmission; + +#[derive(Parser)] +#[command(version, about)] +#[derive(Debug)] +pub struct Args { + #[command(subcommand)] + pub command: Option, +} + +#[derive(Debug)] +pub enum ConfigType { + DefaultConfig, + DefaultKeymap +} + +#[derive(Subcommand)] +#[derive(Debug)] +pub enum Commands { + AddTorrent { torrent: String }, + FetchRss { url: String, filter: Option }, + PrintDefaultConfig, + PrintDefaultKeymap +} + +pub async fn handle_command(config: &Config, command: Commands) -> Result<()> { + match command { + Commands::AddTorrent { torrent } => add_torrent(config, torrent).await?, + Commands::FetchRss { url, filter } => fetch_rss(config, &url, filter.as_deref()).await?, + Commands::PrintDefaultConfig { } => println!("{}", rm_config::main_config::MainConfig::DEFAULT_CONFIG), + Commands::PrintDefaultKeymap { } => println!("{}", rm_config::keymap::KeymapConfig::DEFAULT_CONFIG), + } + Ok(()) +} + +async fn add_torrent(config: &Config, torrent: String) -> Result<()> { + let mut transclient = transmission::utils::client_from_config(config); + let args = { + if torrent.starts_with("magnet:") + || torrent.starts_with("http:") + || torrent.starts_with("https:") + { + TorrentAddArgs { + filename: Some(torrent), + ..Default::default() + } + } else if torrent.starts_with("www") { + TorrentAddArgs { + filename: Some(format!("https://{torrent}")), + ..Default::default() + } + } else { + let mut torrent_file = File::open(torrent)?; + let mut buf = vec![]; + torrent_file.read_to_end(&mut buf).unwrap(); + let metainfo = base64::engine::general_purpose::STANDARD.encode(buf); + TorrentAddArgs { + metainfo: Some(metainfo), + ..Default::default() + } + } + }; + + if let Err(e) = transclient.torrent_add(args).await { + eprintln!("error while adding a torrent: {e}"); + if e.to_string().contains("expected value at line") { + eprintln!("Check whether your arguments are valid."); + } + + std::process::exit(1); + }; + Ok(()) +} + +async fn fetch_rss(config: &Config, url: &str, filter: Option<&str>) -> Result<()> { + let mut transclient = transmission::utils::client_from_config(config); + let content = reqwest::get(url).await?.bytes().await?; + let channel = rss::Channel::read_from(&content[..])?; + let re: Option = { + if let Some(filter_str) = filter { + let res = Regex::new(filter_str)?; + Some(res) + } else { + None + } + }; + let items = channel.items().iter().filter_map(|item| { + if let (Some(title), Some(url)) = (item.title(), item.link()) { + if let Some(re) = &re { + if re.is_match(title) { + return Some((title, url)); + } + } else { + return Some((title, url)); + } + } + None + }); + for (title, url) in items { + println!("downloading {title}"); + let args = TorrentAddArgs { + filename: Some(url.to_string()), + ..Default::default() + }; + if let Err(e) = transclient.torrent_add(args).await { + bail!("error while adding a torrent: {e}") + } + } + Ok(()) +} From b521addf62496010cd3ea7adf75ba66e5478184f Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Thu, 15 Aug 2024 17:03:56 +0200 Subject: [PATCH 4/4] Revert "feat: default config printing in CLI" This reverts commit bb0d09d28e93da8972d59694eb08f8e2827a77b7. --- rm-config/src/keymap/mod.rs | 2 +- rm-config/src/main_config.rs | 2 +- rm-main/src/cli.rs | 119 ----------------------------------- 3 files changed, 2 insertions(+), 121 deletions(-) delete mode 100644 rm-main/src/cli.rs diff --git a/rm-config/src/keymap/mod.rs b/rm-config/src/keymap/mod.rs index 3985e70..74c34cf 100644 --- a/rm-config/src/keymap/mod.rs +++ b/rm-config/src/keymap/mod.rs @@ -277,7 +277,7 @@ impl Default for KeyModifier { impl KeymapConfig { pub const FILENAME: &'static str = "keymap.toml"; - pub const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); + const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml"); pub fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-config/src/main_config.rs b/rm-config/src/main_config.rs index b1d6960..3103966 100644 --- a/rm-config/src/main_config.rs +++ b/rm-config/src/main_config.rs @@ -100,7 +100,7 @@ impl Default for SearchTab { impl MainConfig { pub(crate) const FILENAME: &'static str = "config.toml"; - pub const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); + const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); pub(crate) fn init() -> Result { match utils::fetch_config::(Self::FILENAME) { diff --git a/rm-main/src/cli.rs b/rm-main/src/cli.rs deleted file mode 100644 index 3d2103e..0000000 --- a/rm-main/src/cli.rs +++ /dev/null @@ -1,119 +0,0 @@ -use std::{fs::File, io::Read}; - -use anyhow::{bail, Result}; -use base64::Engine; -use clap::{Parser, Subcommand}; -use regex::Regex; -use rm_config::Config; -use transmission_rpc::types::TorrentAddArgs; - -use crate::transmission; - -#[derive(Parser)] -#[command(version, about)] -#[derive(Debug)] -pub struct Args { - #[command(subcommand)] - pub command: Option, -} - -#[derive(Debug)] -pub enum ConfigType { - DefaultConfig, - DefaultKeymap -} - -#[derive(Subcommand)] -#[derive(Debug)] -pub enum Commands { - AddTorrent { torrent: String }, - FetchRss { url: String, filter: Option }, - PrintDefaultConfig, - PrintDefaultKeymap -} - -pub async fn handle_command(config: &Config, command: Commands) -> Result<()> { - match command { - Commands::AddTorrent { torrent } => add_torrent(config, torrent).await?, - Commands::FetchRss { url, filter } => fetch_rss(config, &url, filter.as_deref()).await?, - Commands::PrintDefaultConfig { } => println!("{}", rm_config::main_config::MainConfig::DEFAULT_CONFIG), - Commands::PrintDefaultKeymap { } => println!("{}", rm_config::keymap::KeymapConfig::DEFAULT_CONFIG), - } - Ok(()) -} - -async fn add_torrent(config: &Config, torrent: String) -> Result<()> { - let mut transclient = transmission::utils::client_from_config(config); - let args = { - if torrent.starts_with("magnet:") - || torrent.starts_with("http:") - || torrent.starts_with("https:") - { - TorrentAddArgs { - filename: Some(torrent), - ..Default::default() - } - } else if torrent.starts_with("www") { - TorrentAddArgs { - filename: Some(format!("https://{torrent}")), - ..Default::default() - } - } else { - let mut torrent_file = File::open(torrent)?; - let mut buf = vec![]; - torrent_file.read_to_end(&mut buf).unwrap(); - let metainfo = base64::engine::general_purpose::STANDARD.encode(buf); - TorrentAddArgs { - metainfo: Some(metainfo), - ..Default::default() - } - } - }; - - if let Err(e) = transclient.torrent_add(args).await { - eprintln!("error while adding a torrent: {e}"); - if e.to_string().contains("expected value at line") { - eprintln!("Check whether your arguments are valid."); - } - - std::process::exit(1); - }; - Ok(()) -} - -async fn fetch_rss(config: &Config, url: &str, filter: Option<&str>) -> Result<()> { - let mut transclient = transmission::utils::client_from_config(config); - let content = reqwest::get(url).await?.bytes().await?; - let channel = rss::Channel::read_from(&content[..])?; - let re: Option = { - if let Some(filter_str) = filter { - let res = Regex::new(filter_str)?; - Some(res) - } else { - None - } - }; - let items = channel.items().iter().filter_map(|item| { - if let (Some(title), Some(url)) = (item.title(), item.link()) { - if let Some(re) = &re { - if re.is_match(title) { - return Some((title, url)); - } - } else { - return Some((title, url)); - } - } - None - }); - for (title, url) in items { - println!("downloading {title}"); - let args = TorrentAddArgs { - filename: Some(url.to_string()), - ..Default::default() - }; - if let Err(e) = transclient.torrent_add(args).await { - bail!("error while adding a torrent: {e}") - } - } - Ok(()) -}