diff --git a/examples/config.rs b/examples/config.rs index 4ee5b7e..c156f32 100644 --- a/examples/config.rs +++ b/examples/config.rs @@ -1,8 +1,10 @@ +use std::env::args; + use miette::Result; use nextcloud_config_parser::parse; fn main() -> Result<()> { - let config = parse("tests/configs/basic.php")?; + let config = parse(args().nth(1).expect("no config file provided"))?; dbg!(config); Ok(()) } diff --git a/src/nc.rs b/src/nc.rs index e51b7b7..e2a88e3 100644 --- a/src/nc.rs +++ b/src/nc.rs @@ -53,7 +53,7 @@ fn parse_php(path: impl AsRef) -> Result { } } - let php = match content.find("$CONFIG") { + let php = match content.rfind("$CONFIG") { Some(pos) => content[pos + "$CONFIG".len()..] .trim() .trim_start_matches('='), diff --git a/tests/configs.rs b/tests/configs.rs index f3ce479..04ea2c8 100644 --- a/tests/configs.rs +++ b/tests/configs.rs @@ -81,6 +81,36 @@ fn test_parse_config_basic() { ); } +#[test] +fn test_parse_config_example_comment() { + let config = config_from_file("tests/configs/comment_example.php"); + assert_eq!("https://cloud.example.com", config.nextcloud_url); + assert_eq!("oc_", config.database_prefix); + assert_debug_equal( + &Database::MySql { + database: "nextcloud".to_string(), + username: "nextcloud".to_string(), + password: "secret".to_string(), + connect: DbConnect::Tcp { + host: "127.0.0.1".to_string(), + port: 3306, + }, + ssl_options: SslOptions::Disabled, + }, + &config.database, + ); + + assert_eq!( + config.database.url(), + "mysql://nextcloud:secret@127.0.0.1/nextcloud?ssl-mode=disabled" + ); + + assert_debug_equal( + RedisConfig::Single(parse_redis("redis://127.0.0.1")), + config.redis, + ); +} + #[test] fn test_parse_implicit_prefix() { let config = config_from_file("tests/configs/implicit_prefix.php"); diff --git a/tests/configs/comment_example.php b/tests/configs/comment_example.php new file mode 100644 index 0000000..9e063fd --- /dev/null +++ b/tests/configs/comment_example.php @@ -0,0 +1,31 @@ +.config.php' file which will be included and rendered into this file. + * + * Example: + * "https://cloud.example.com", + "dbtype" => "mysql", + "dbname" => "nextcloud", + "dbhost" => "127.0.0.1", + "dbport" => "", + "dbtableprefix" => "oc_", + "dbuser" => "nextcloud", + "dbpassword" => "secret", + "redis" => [ + "host" => "localhost", + ], +];