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
.idea/
*.DS_Store

View file

@ -1,6 +1,6 @@
[package]
name = "urouter"
version = "0.5.3"
version = "0.6.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/ivabus/urouter"
@ -16,3 +16,4 @@ smurf = "0.3.0"
clap = { version = "4.4.11", features = ["derive"] }
regex = "1.10.2"
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.
## `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
The project is licensed under the terms of the [MIT license](./LICENSE).

View file

@ -7,24 +7,23 @@ Global comments:
*/
mod structs;
use structs::*;
#[macro_use]
extern crate rocket;
use rocket::http::{ContentType, Status};
use std::cell::OnceCell;
use std::collections::HashMap;
use std::hint::unreachable_unchecked;
use std::path::PathBuf;
use std::time::Instant;
use std::{
cell::OnceCell, collections::HashMap, hint::unreachable_unchecked, path::PathBuf, time::Instant,
};
use rocket::response::content::RawText;
use rocket::response::Redirect;
use rocket::{
figment::Figment,
response::{content::RawText, Redirect},
};
use clap::Parser;
use regex::Regex;
use rocket::figment::Figment;
static mut ALIAS: OnceCell<Vec<Alias>> = 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]
async fn main() -> Result<(), rocket::Error> {
let args = Args::parse();
let file = std::fs::File::open(args.alias_file).unwrap();
let mut args = Args::parse();
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 {
serde_json::from_reader::<std::fs::File, NixJson>(file).unwrap().alias
} else {

View file

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