0.6.0: change alias.json path logic

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-12-23 07:04:36 +03:00
parent 82ddc180cb
commit 9cfa1cf950
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
5 changed files with 42 additions and 14 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target /target
.idea/
*.DS_Store

View file

@ -1,6 +1,6 @@
[package] [package]
name = "urouter" name = "urouter"
version = "0.5.3" version = "0.6.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/urouter" repository = "https://github.com/ivabus/urouter"
@ -16,3 +16,4 @@ smurf = "0.3.0"
clap = { version = "4.4.11", features = ["derive"] } clap = { version = "4.4.11", features = ["derive"] }
regex = "1.10.2" regex = "1.10.2"
ureq = { version = "2.9.1", features = ["brotli", "native-certs"] } ureq = { version = "2.9.1", features = ["brotli", "native-certs"] }
users = "0.11.0"

View file

@ -85,6 +85,14 @@ Each set contains 2 necessary elements and 1 optional.
Agent matching made for `curl https://url | sh` like scripts. Agent matching made for `curl https://url | sh` like scripts.
## `alias.json` location
- Passed with `--alias_file`, will look up to this path, if file doesn't exist (or no access to it) will panic
- If urouter started with privileges (EUID = 0), file would be `/etc/urouter/alias.json`
- Otherwise if `XDG_CONFIG_HOME` is set, file would be `$XDG_CONFIG_HOME/urouter/alias.json`
- Otherwise if `$HOME` is set, file would be `$HOME/.config/urouter/alias.json`
- If not matched any, will panic and exit
## License ## License
The project is licensed under the terms of the [MIT license](./LICENSE). The project is licensed under the terms of the [MIT license](./LICENSE).

View file

@ -7,24 +7,23 @@ Global comments:
*/ */
mod structs; mod structs;
use structs::*; use structs::*;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::http::{ContentType, Status}; use rocket::http::{ContentType, Status};
use std::cell::OnceCell; use std::{
use std::collections::HashMap; cell::OnceCell, collections::HashMap, hint::unreachable_unchecked, path::PathBuf, time::Instant,
use std::hint::unreachable_unchecked; };
use std::path::PathBuf;
use std::time::Instant;
use rocket::response::content::RawText; use rocket::{
use rocket::response::Redirect; figment::Figment,
response::{content::RawText, Redirect},
};
use clap::Parser; use clap::Parser;
use regex::Regex; use regex::Regex;
use rocket::figment::Figment;
static mut ALIAS: OnceCell<Vec<Alias>> = OnceCell::new(); static mut ALIAS: OnceCell<Vec<Alias>> = OnceCell::new();
static mut COMPILED_REGEXES: OnceCell<HashMap<String, Regex>> = OnceCell::new(); static mut COMPILED_REGEXES: OnceCell<HashMap<String, Regex>> = OnceCell::new();
@ -98,8 +97,26 @@ async fn index(user_agent: UserAgent) -> Response {
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), rocket::Error> { async fn main() -> Result<(), rocket::Error> {
let args = Args::parse(); let mut args = Args::parse();
let file = std::fs::File::open(args.alias_file).unwrap(); args.alias_file = match args.alias_file {
Some(f) => Some(f),
None => Some(
match users::get_effective_uid() {
0 => "/etc/urouter/alias.json".to_string(),
_ => match std::env::var("XDG_CONFIG_HOME") {
Ok(config_home) => format!("{}/urouter/alias.json", config_home),
Err(_) => match std::env::var("HOME") {
Ok(home) => format!("{}/.config/urouter/alias.json", home),
Err(_) => {
panic!("Could not get config location, see README")
}
},
},
}
.into(),
),
};
let file = std::fs::File::open(args.alias_file.unwrap()).unwrap();
let alias: Vec<Alias> = if args.alias_file_is_set_not_a_list { let alias: Vec<Alias> = if args.alias_file_is_set_not_a_list {
serde_json::from_reader::<std::fs::File, NixJson>(file).unwrap().alias serde_json::from_reader::<std::fs::File, NixJson>(file).unwrap().alias
} else { } else {

View file

@ -14,8 +14,8 @@ use std::path::PathBuf;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(about, author)] #[command(about, author)]
pub struct Args { pub struct Args {
#[arg(long, default_value = "./alias.json")] #[arg(long)]
pub alias_file: PathBuf, pub alias_file: Option<PathBuf>,
/// For internal usage /// For internal usage
#[arg(long, default_value = "false")] #[arg(long, default_value = "false")]