mirror of
https://codeberg.org/icewind/nextcloud-config-parser.git
synced 2026-06-03 08:34:13 +02:00
parse string dbport values
This commit is contained in:
parent
bb16d30aa2
commit
172f328713
1 changed files with 79 additions and 4 deletions
83
src/nc.rs
83
src/nc.rs
|
|
@ -171,12 +171,12 @@ fn parse_db_options(parsed: &Value) -> Result<Database> {
|
|||
unreachable!()
|
||||
}
|
||||
};
|
||||
if let Some(port) = parsed["dbport"].clone().into_int() {
|
||||
if let Some(port) = parse_port(&parsed["dbport"]) {
|
||||
if let DbConnect::Tcp {
|
||||
port: connect_port, ..
|
||||
} = &mut connect
|
||||
{
|
||||
*connect_port = port as u16;
|
||||
*connect_port = port;
|
||||
}
|
||||
}
|
||||
let database = parsed["dbname"].as_str().unwrap_or("owncloud");
|
||||
|
|
@ -258,12 +258,12 @@ fn parse_db_options(parsed: &Value) -> Result<Database> {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(port) = parsed["dbport"].clone().into_int() {
|
||||
if let Some(port) = parse_port(&parsed["dbport"]) {
|
||||
if let DbConnect::Tcp {
|
||||
port: connect_port, ..
|
||||
} = &mut connect
|
||||
{
|
||||
*connect_port = port as u16;
|
||||
*connect_port = port;
|
||||
}
|
||||
}
|
||||
if disable_ssl {
|
||||
|
|
@ -374,6 +374,12 @@ fn parse_redis_options(parsed: &Value, key: &str) -> RedisConfig {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_port(port: &Value) -> Option<u16> {
|
||||
port.as_str()
|
||||
.and_then(|port| port.parse().ok())
|
||||
.or_else(|| port.as_int().map(|port| port as u16))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_redis_empty_password_none() {
|
||||
let config =
|
||||
|
|
@ -389,6 +395,75 @@ fn test_redis_empty_password_none() {
|
|||
assert_eq!(redis.passwd(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_postgres_port() {
|
||||
use indexmap::indexmap;
|
||||
|
||||
let config = php_literal_parser::from_str(
|
||||
r#"[
|
||||
'dbtype' => 'pgsql',
|
||||
'dbhost' => '127.0.0.1:6432',
|
||||
'dbport' => '',
|
||||
'dbuser' => 'nextcloud',
|
||||
'dbpassword' => 'nextcloud',
|
||||
'dbname' => 'nextcloud',
|
||||
]"#,
|
||||
)
|
||||
.unwrap();
|
||||
let db = parse_db_options(&config).unwrap();
|
||||
assert_eq!(
|
||||
db,
|
||||
Database::Postgres {
|
||||
database: "nextcloud".to_string(),
|
||||
username: "nextcloud".to_string(),
|
||||
password: "nextcloud".to_string(),
|
||||
connect: DbConnect::Tcp {
|
||||
host: "127.0.0.1".into(),
|
||||
port: 6432,
|
||||
},
|
||||
options: indexmap! {
|
||||
"sslmode".into() => "disable".into(),
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
db.url(),
|
||||
"postgresql://nextcloud:nextcloud@127.0.0.1:6432/nextcloud?sslmode=disable"
|
||||
);
|
||||
|
||||
let config = php_literal_parser::from_str(
|
||||
r#"[
|
||||
'dbtype' => 'pgsql',
|
||||
'dbhost' => '127.0.0.1',
|
||||
'dbport' => '6432',
|
||||
'dbuser' => 'nextcloud',
|
||||
'dbpassword' => 'nextcloud',
|
||||
'dbname' => 'nextcloud',
|
||||
]"#,
|
||||
)
|
||||
.unwrap();
|
||||
let db = parse_db_options(&config).unwrap();
|
||||
assert_eq!(
|
||||
db,
|
||||
Database::Postgres {
|
||||
database: "nextcloud".to_string(),
|
||||
username: "nextcloud".to_string(),
|
||||
password: "nextcloud".to_string(),
|
||||
connect: DbConnect::Tcp {
|
||||
host: "127.0.0.1".into(),
|
||||
port: 6432,
|
||||
},
|
||||
options: indexmap! {
|
||||
"sslmode".into() => "disable".into(),
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
db.url(),
|
||||
"postgresql://nextcloud:nextcloud@127.0.0.1:6432/nextcloud?sslmode=disable"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_postgres_options() {
|
||||
use indexmap::indexmap;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue