Skip to content

Commit

Permalink
feat: password_file for clients and hot-reload configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed Jun 22, 2024
1 parent 92e6b2d commit a40c3ca
Show file tree
Hide file tree
Showing 27 changed files with 444 additions and 187 deletions.
10 changes: 5 additions & 5 deletions docs/clients/qBittorrent.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ download_client = "qBittorrent"
# ...

[client.qBittorrent]
base_url = "http://localhost:8080" # required
username = "admin" # required
password = "adminadmin" # required
base_url = "http://localhost:8080" # required
username = "admin" # optional
password = "adminadmin" # optional
password_file = "/path/to/password.txt" # optional
use_magnet = true # optional, will be true by default
savepath = "~/Downloads/" # all optional with no default here and below...
category = "Category Name"
Expand All @@ -40,5 +41,4 @@ sequential_download = true
prioritize_first_last_pieces = true
```

For more information on what each of the values represent, check qBittorrents [WebUI-API documentation](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)). For most users, you will only need the three required parts at the top to get downloads working.

For more information on what each of the values represent, check qBittorrents [WebUI-API documentation](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)). For most users, you will only need the base URL, username, and password to get downloads working. The password can either be defined in `password_file` (a raw text file containing only the password) or hardcoded in `password`.
3 changes: 3 additions & 0 deletions docs/clients/transmission.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ download_client = "Transmission"
base_url = "http://localhost:9091/transmission/rpc" # required
username = "user" # all optional here and below
password = "pass"
password_file = "/path/to/password.txt"
use_magnet = true
labels = [ # must not contain commas in any of the labels
"label1",
Expand All @@ -33,5 +34,7 @@ download_dir = "~/Downloads/"
bandwidth_priority = "Low"
```

The password can either be defined in `password_file` (a raw text file containing only the password) or hardcoded in `password`.

### Bandwidth Priority
This value can be one of the following: `Low`, `Normal`, `High`
21 changes: 15 additions & 6 deletions modules/clients/qBittorrent.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@
'';
};
username = lib.mkOption {
type = lib.types.str;
default = "admin";
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The username to login to qBittorrent
The username to login to qBittorrent (optional)
'';
};
password = lib.mkOption {
type = lib.types.str;
default = "adminadmin";
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The password to login to qBittorrent (optional)
Has higher priority than `password_file`
'';
};
password_file = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The password to login to qBittorrent
The path to a file containing the password to login to qBittorrent (optional)
Has lower priority than `password`
'';
};
use_magnet = lib.mkOption {
Expand Down
9 changes: 9 additions & 0 deletions modules/clients/transmission.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
default = null;
description = ''
The password to login to Transmission (optional)
Has higher priority than `password_file`
'';
};
password_file = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The path to a file containing the password to login to Transmission (optional)
Has lower priority than `password`
'';
};
use_magnet = lib.mkOption {
Expand Down
8 changes: 8 additions & 0 deletions modules/home-manager.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ in {
'';
};

hot_reload_config = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether to automatically reload config/user-themes once modified
'';
};

notifications = {
position = lib.mkOption {
type = lib.types.nullOr lib.types.str;
Expand Down
52 changes: 49 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
source::{
nyaa_html::NyaaHtmlSource, request_client, Item, Source, SourceInfo, SourceResults, Sources,
},
sync::{EventSync, SearchQuery},
sync::{EventSync, ReloadType, SearchQuery},
theme::{self, Theme},
util::conv::key_to_string,
widget::{
Expand Down Expand Up @@ -246,13 +246,15 @@ impl App {
mpsc::channel::<Result<SourceResults, Box<dyn Error + Send + Sync>>>(32);
let (tx_evt, mut rx_evt) = mpsc::channel::<Event>(100);
let (tx_dl, mut rx_dl) = mpsc::channel::<DownloadResult>(100);
let (tx_cfg, mut rx_cfg) = mpsc::channel::<ReloadType>(1);

tokio::task::spawn(sync.clone().read_event_loop(tx_evt));
tokio::task::spawn(sync.clone().watch_config_loop(tx_cfg));

match config_manager.load() {
Ok(config) => {
ctx.failed_config_load = false;
if let Err(e) = config.apply::<C>(&config_manager, ctx, &mut self.widgets) {
if let Err(e) = config.full_apply(config_manager.path(), ctx, &mut self.widgets) {
ctx.show_error(e);
} else if let Err(e) = ctx.save_config() {
ctx.show_error(e);
Expand All @@ -263,7 +265,7 @@ impl App {
if let Err(e) =
ctx.config
.clone()
.apply::<C>(&config_manager, ctx, &mut self.widgets)
.full_apply(config_manager.path(), ctx, &mut self.widgets)
{
ctx.show_error(e);
}
Expand Down Expand Up @@ -453,6 +455,50 @@ impl App {
}
break;
}
Some(notif) = rx_cfg.recv() => {
match notif {
ReloadType::Config => {
match config_manager.load() {
Ok(config) => {
match config.partial_apply(ctx, &mut self.widgets) {
Ok(()) => ctx.notify("Reloaded config".to_owned()),
Err(e) => ctx.show_error(e),
}
}
Err(e) => ctx.show_error(e),
}
},
ReloadType::Theme(t) => match theme::load_user_themes(ctx, config_manager.path()) {
Ok(()) => ctx.notify(format!("Reloaded theme \"{t}\"")),
Err(e) => ctx.show_error(e)
},
}
//match config_manager.load() {
// Ok(config) => {
// ctx.failed_config_load = false;
// if let Err(e) = config.apply::<C>(&config_manager, ctx, &mut self.widgets) {
// ctx.show_error(e);
// } else {
// ctx.notify(match notif {
// ReloadType::Config => "Reloaded config".to_owned(),
// ReloadType::Theme(theme) => format!("Reloaded theme \"{theme}\""),
// });
// }
// }
// Err(e) => {
// ctx.show_error(format!("Failed to load config:\n{}", e));
// if let Err(e) =
// ctx.config
// .clone()
// .apply::<C>(&config_manager, ctx, &mut self.widgets)
// {
// ctx.show_error(e);
// }
// }
//}

break;
},
// _ = async{}, if matches!(terminal.size().map(|s| self.widgets.notification.update(last_time.map(|l| (Instant::now() - l).as_secs_f64()).unwrap_or(0.), s)), Ok(true)) => {
else => {
return Err("All channels closed".into());
Expand Down
17 changes: 8 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use strum::{Display, VariantArray};
use tokio::task::JoinSet;

use crate::{app::Context, client::cmd::CmdClient, source::Item};
use crate::{client::cmd::CmdClient, source::Item};

use self::{
cmd::CmdConfig,
Expand Down Expand Up @@ -256,15 +256,14 @@ impl Client {
// ctx.batch.retain(|i| !success_ids.contains(&i.id)); // Remove successes from batch
}

pub fn load_config(self, ctx: &mut Context) {
pub fn load_config(self, cfg: &mut ClientConfig) {
match self {
Self::Cmd => cmd::load_config(ctx),
Self::Qbit => qbit::load_config(ctx),
Self::Transmission => transmission::load_config(ctx),
Self::Rqbit => rqbit::load_config(ctx),
Self::DefaultApp => default_app::load_config(ctx),
Self::Download => download::load_config(ctx),
Self::Cmd => cmd::load_config(cfg),
Self::Qbit => qbit::load_config(cfg),
Self::Transmission => transmission::load_config(cfg),
Self::Rqbit => rqbit::load_config(cfg),
Self::DefaultApp => default_app::load_config(cfg),
Self::Download => download::load_config(cfg),
};
ctx.config.download_client = self;
}
}
8 changes: 4 additions & 4 deletions src/client/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{app::Context, source::Item, util::cmd::CommandBuilder};
use crate::{source::Item, util::cmd::CommandBuilder};

use super::{multidownload, ClientConfig, DownloadClient, DownloadError, DownloadResult};

Expand All @@ -26,9 +26,9 @@ impl Default for CmdConfig {
}
}

pub fn load_config(app: &mut Context) {
if app.config.client.cmd.is_none() {
app.config.client.cmd = Some(CmdConfig::default());
pub fn load_config(cfg: &mut ClientConfig) {
if cfg.cmd.is_none() {
cfg.cmd = Some(CmdConfig::default());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/client/default_app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{app::Context, source::Item};
use crate::source::Item;

use super::{multidownload, ClientConfig, DownloadClient, DownloadError, DownloadResult};

Expand All @@ -12,10 +12,10 @@ pub struct DefaultAppConfig {

pub struct DefaultAppClient;

pub fn load_config(app: &mut Context) {
if app.config.client.default_app.is_none() {
pub fn load_config(cfg: &mut ClientConfig) {
if cfg.default_app.is_none() {
let def = DefaultAppConfig::default();
app.config.client.default_app = Some(def);
cfg.default_app = Some(def);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/client/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{error::Error, fs, path::PathBuf};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

use crate::{app::Context, source::Item, util::conv::get_hash};
use crate::{source::Item, util::conv::get_hash};

use super::{multidownload, ClientConfig, DownloadClient, DownloadError, DownloadResult};

Expand Down Expand Up @@ -36,10 +36,9 @@ impl Default for DownloadConfig {
}
}

pub fn load_config(app: &mut Context) {
if app.config.client.download.is_none() {
let def = DownloadConfig::default();
app.config.client.download = Some(def);
pub fn load_config(cfg: &mut ClientConfig) {
if cfg.download.is_none() {
cfg.download = Some(DownloadConfig::default());
}
}

Expand Down
Loading

0 comments on commit a40c3ca

Please sign in to comment.