mirror of
https://codeberg.org/icewind/nextcloud-config-parser.git
synced 2026-06-03 08:34:13 +02:00
remove redis-connect feature
This commit is contained in:
parent
10b7d8eb56
commit
88db1d1d86
5 changed files with 151 additions and 104 deletions
55
src/lib.rs
55
src/lib.rs
|
|
@ -2,9 +2,6 @@ mod nc;
|
|||
|
||||
use form_urlencoded::Serializer;
|
||||
use miette::Diagnostic;
|
||||
#[cfg(feature = "redis-connect")]
|
||||
use redis::{ConnectionAddr, ConnectionInfo};
|
||||
#[cfg(feature = "redis-connect")]
|
||||
use std::iter::once;
|
||||
use std::path::PathBuf;
|
||||
use thiserror::Error;
|
||||
|
|
@ -15,22 +12,44 @@ pub use nc::{parse, parse_glob};
|
|||
pub struct Config {
|
||||
pub database: Database,
|
||||
pub database_prefix: String,
|
||||
#[cfg(feature = "redis-connect")]
|
||||
pub redis: RedisConfig,
|
||||
pub nextcloud_url: String,
|
||||
}
|
||||
|
||||
#[cfg(feature = "redis-connect")]
|
||||
#[derive(Debug)]
|
||||
pub enum RedisConfig {
|
||||
Single(ConnectionInfo),
|
||||
Cluster(Vec<ConnectionInfo>),
|
||||
Single(RedisConnectionInfo),
|
||||
Cluster(Vec<RedisConnectionInfo>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RedisConnectionAddr {
|
||||
Tcp {
|
||||
host: String,
|
||||
port: u16,
|
||||
},
|
||||
TcpTls {
|
||||
host: String,
|
||||
port: u16,
|
||||
insecure: bool,
|
||||
tls_params: Option<String>,
|
||||
},
|
||||
Unix {
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RedisConnectionInfo {
|
||||
pub addr: RedisConnectionAddr,
|
||||
pub db: i64,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "redis-connect")]
|
||||
impl RedisConfig {
|
||||
pub fn addr(&self) -> impl Iterator<Item = &ConnectionAddr> {
|
||||
let boxed: Box<dyn Iterator<Item = &ConnectionAddr>> = match self {
|
||||
pub fn addr(&self) -> impl Iterator<Item = &RedisConnectionAddr> {
|
||||
let boxed: Box<dyn Iterator<Item = &RedisConnectionAddr>> = match self {
|
||||
RedisConfig::Single(conn) => Box::new(once(&conn.addr)),
|
||||
RedisConfig::Cluster(conns) => Box::new(conns.iter().map(|conn| &conn.addr)),
|
||||
};
|
||||
|
|
@ -39,34 +58,32 @@ impl RedisConfig {
|
|||
|
||||
pub fn db(&self) -> i64 {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => conn.redis.db,
|
||||
RedisConfig::Cluster(conns) => {
|
||||
conns.first().map(|conn| conn.redis.db).unwrap_or_default()
|
||||
}
|
||||
RedisConfig::Single(conn) => conn.db,
|
||||
RedisConfig::Cluster(conns) => conns.first().map(|conn| conn.db).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn username(&self) -> Option<&str> {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => conn.redis.username.as_deref(),
|
||||
RedisConfig::Single(conn) => conn.username.as_deref(),
|
||||
RedisConfig::Cluster(conns) => conns
|
||||
.first()
|
||||
.map(|conn| conn.redis.username.as_deref())
|
||||
.map(|conn| conn.username.as_deref())
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn passwd(&self) -> Option<&str> {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => conn.redis.password.as_deref(),
|
||||
RedisConfig::Single(conn) => conn.password.as_deref(),
|
||||
RedisConfig::Cluster(conns) => conns
|
||||
.first()
|
||||
.map(|conn| conn.redis.password.as_deref())
|
||||
.map(|conn| conn.password.as_deref())
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_vec(self) -> Vec<ConnectionInfo> {
|
||||
pub fn into_vec(self) -> Vec<RedisConnectionInfo> {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => vec![conn],
|
||||
RedisConfig::Cluster(vec) => vec,
|
||||
|
|
|
|||
55
src/nc.rs
55
src/nc.rs
|
|
@ -1,11 +1,9 @@
|
|||
#[cfg(feature = "redis-connect")]
|
||||
use crate::RedisConfig;
|
||||
use crate::{
|
||||
Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError, Result, SslOptions,
|
||||
Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError,
|
||||
RedisConnectionInfo, Result, SslOptions,
|
||||
};
|
||||
use crate::{RedisConfig, RedisConnectionAddr};
|
||||
use php_literal_parser::Value;
|
||||
#[cfg(feature = "redis-connect")]
|
||||
use redis::{ConnectionAddr, ConnectionInfo, RedisConnectionInfo};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::DirEntry;
|
||||
use std::iter::once;
|
||||
|
|
@ -111,14 +109,12 @@ fn parse_files(files: impl IntoIterator<Item = PathBuf>) -> Result<Config> {
|
|||
.clone()
|
||||
.into_string()
|
||||
.ok_or(Error::NoUrl)?;
|
||||
#[cfg(feature = "redis-connect")]
|
||||
let redis = parse_redis_options(&parsed);
|
||||
|
||||
Ok(Config {
|
||||
database,
|
||||
database_prefix,
|
||||
nextcloud_url,
|
||||
#[cfg(feature = "redis-connect")]
|
||||
redis,
|
||||
})
|
||||
}
|
||||
|
|
@ -299,30 +295,31 @@ fn split_host(host: &str) -> (&str, Option<u16>, Option<&str>) {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "redis-connect")]
|
||||
enum RedisAddress {
|
||||
Single(ConnectionAddr),
|
||||
Cluster(Vec<ConnectionAddr>),
|
||||
Single(RedisConnectionAddr),
|
||||
Cluster(Vec<RedisConnectionAddr>),
|
||||
}
|
||||
|
||||
#[cfg(feature = "redis-connect")]
|
||||
fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
||||
use redis::ProtocolVersion;
|
||||
|
||||
let (redis_options, address) = if parsed["redis.cluster"].is_array() {
|
||||
let redis_options = &parsed["redis.cluster"];
|
||||
let seeds = redis_options["seeds"].values();
|
||||
let addresses = seeds
|
||||
.filter_map(|seed| seed.as_str())
|
||||
.map(split_host)
|
||||
.filter_map(|(host, port, _)| Some(ConnectionAddr::Tcp(host.into(), port?)))
|
||||
.filter_map(|(host, port, _)| {
|
||||
Some(RedisConnectionAddr::Tcp {
|
||||
host: host.into(),
|
||||
port: port?,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(redis_options, RedisAddress::Cluster(addresses))
|
||||
} else {
|
||||
let redis_options = &parsed["redis"];
|
||||
let mut host = redis_options["host"].as_str().unwrap_or("127.0.0.1");
|
||||
let address = if host.starts_with('/') {
|
||||
RedisAddress::Single(ConnectionAddr::Unix(host.into()))
|
||||
RedisAddress::Single(RedisConnectionAddr::Unix { path: host.into() })
|
||||
} else {
|
||||
if host == "localhost" {
|
||||
host = "127.0.0.1";
|
||||
|
|
@ -332,7 +329,10 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
|||
} else {
|
||||
split_host(host)
|
||||
};
|
||||
RedisAddress::Single(ConnectionAddr::Tcp(host.into(), port.unwrap_or(6379)))
|
||||
RedisAddress::Single(RedisConnectionAddr::Tcp {
|
||||
host: host.into(),
|
||||
port: port.unwrap_or(6379),
|
||||
})
|
||||
};
|
||||
(redis_options, address)
|
||||
};
|
||||
|
|
@ -348,26 +348,20 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
|||
.map(String::from);
|
||||
|
||||
match address {
|
||||
RedisAddress::Single(addr) => RedisConfig::Single(ConnectionInfo {
|
||||
RedisAddress::Single(addr) => RedisConfig::Single(RedisConnectionInfo {
|
||||
addr,
|
||||
redis: RedisConnectionInfo {
|
||||
db,
|
||||
username,
|
||||
password,
|
||||
protocol: ProtocolVersion::default(),
|
||||
},
|
||||
db,
|
||||
username,
|
||||
password,
|
||||
}),
|
||||
RedisAddress::Cluster(addresses) => RedisConfig::Cluster(
|
||||
addresses
|
||||
.into_iter()
|
||||
.map(|addr| ConnectionInfo {
|
||||
.map(|addr| RedisConnectionInfo {
|
||||
addr,
|
||||
redis: RedisConnectionInfo {
|
||||
db,
|
||||
username: username.clone(),
|
||||
password: password.clone(),
|
||||
protocol: ProtocolVersion::default(),
|
||||
},
|
||||
db,
|
||||
username: username.clone(),
|
||||
password: password.clone(),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
|
@ -375,7 +369,6 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "redis-connect")]
|
||||
fn test_redis_empty_password_none() {
|
||||
let config =
|
||||
php_literal_parser::from_str(r#"["redis" => ["host" => "redis", "password" => "pass"]]"#)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue