diff --git a/src/main.rs b/src/main.rs index 74d9910..1f44ae7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,16 +75,58 @@ fn read_known_hosts() -> Vec { result } -fn expand_expression(expr: &str) -> Vec { - let mut result: Vec = Vec::new(); +fn expand_string(string: String) -> Vec { + let mut result: Vec = Vec::new(); + let mut _result: Vec = Vec::new(); + let mut hosts: Vec = Vec::new(); - for hostname in brace_expand(expr) { - result.push(Host { + if let Some(open_bracket_index) = string.find('[') { + if let Some(close_bracket_index) = string.find(']') { + let prefix = &string[..open_bracket_index]; + let range = &string[open_bracket_index + 1..close_bracket_index]; + let postfix = &string[close_bracket_index + 1..]; + + let parts: Vec<&str> = range.split(':').collect(); + + if parts.len() == 2 { + if let Ok(start) = parts[0].parse::() { + if let Ok(end) = parts[1].parse::() { + for num in start..=end { + _result.push(format!("{}{}{}", prefix, num, postfix)); + } + } + } + } + } + } else { + _result.push(String::from(string)); + } + + for string in _result { + if let Some(open_brace_index) = string.find('{') { + if let Some(close_brace_index) = string.find('}') { + let prefix = &string[..open_brace_index]; + let list = &string[open_brace_index + 1..close_brace_index]; + let postfix = &string[close_brace_index + 1..]; + + let items: Vec<&str> = list.split(',').collect(); + + for item in items { + result.push(format!("{}{}{}", prefix, item, postfix)); + } + } + } else { + result.push(String::from(string)); + } + } + + for hostname in result { + hosts.push(Host { name: hostname.to_string(), ip: None, }) } - result + hosts } fn main() { @@ -112,7 +154,7 @@ fn main() { .collect() } else { info!("Using string expansion to build server list."); - expand_expression(&args.expression) + expand_string(args.expression) }; // Dedup hosts from known_hosts file