Skip to content

Commit

Permalink
Fix generated directories naming (#16)
Browse files Browse the repository at this point in the history
* Fix generated directories naming

Signed-off-by: Atanas Dinov <[email protected]>

* Test extracting hostname

Signed-off-by: Atanas Dinov <[email protected]>

---------

Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov authored Nov 21, 2023
1 parent 0dd378c commit e6cb78d
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/generate_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub(crate) fn generate(config_dir: &str, output_dir: &str) -> Result<(), anyhow:

info!("Generating config from {path:?}...");

let hostname = path
.file_stem()
let hostname = extract_hostname(&path)
.and_then(OsStr::to_str)
.ok_or_else(|| anyhow!("Invalid file path"))?
.to_string();
Expand All @@ -43,6 +42,17 @@ pub(crate) fn generate(config_dir: &str, output_dir: &str) -> Result<(), anyhow:
Ok(())
}

fn extract_hostname(path: &Path) -> Option<&OsStr> {
if path
.extension()
.is_some_and(|ext| ext == "yml" || ext == "yaml")
{
path.file_stem()
} else {
path.file_name()
}
}

fn generate_config(data: String) -> Result<(Vec<Interface>, NetworkConfig), anyhow::Error> {
let network_state = NetworkState::new_from_yaml(&data)?;

Expand Down Expand Up @@ -103,7 +113,7 @@ mod tests {
use std::fs;
use std::path::Path;

use crate::generate_conf::{extract_interfaces, generate, generate_config};
use crate::generate_conf::{extract_hostname, extract_interfaces, generate, generate_config};
use crate::types::{Host, Interface};
use crate::HOST_MAPPING_FILE;

Expand Down Expand Up @@ -204,4 +214,26 @@ mod tests {

Ok(())
}

#[test]
fn extract_host_name() {
assert_eq!(extract_hostname("".as_ref()), None);
assert_eq!(extract_hostname("node1".as_ref()), Some("node1".as_ref()));
assert_eq!(
extract_hostname("node1.example".as_ref()),
Some("node1.example".as_ref())
);
assert_eq!(
extract_hostname("node1.example.com".as_ref()),
Some("node1.example.com".as_ref())
);
assert_eq!(
extract_hostname("node1.example.com.yml".as_ref()),
Some("node1.example.com".as_ref())
);
assert_eq!(
extract_hostname("node1.example.com.yaml".as_ref()),
Some("node1.example.com".as_ref())
);
}
}

0 comments on commit e6cb78d

Please sign in to comment.