feat: add list_versions
This commit is contained in:
@@ -1,10 +1,59 @@
|
|||||||
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn _get_script_base_path(base: &PathBuf, file_name: &str) -> PathBuf {
|
pub fn _get_script_base_path(base: &PathBuf, file_name: &str) -> PathBuf {
|
||||||
let mut file_name_chars = file_name.chars();
|
let mut fn_chars = file_name.chars().take_while(|c| *c != '.');
|
||||||
let path = base.join(file_name_chars.next().unwrap_or('_').to_string())
|
let path = base.join(fn_chars.next().unwrap_or('_').to_string())
|
||||||
.join(file_name_chars.next().unwrap_or('_').to_string())
|
.join(fn_chars.next().unwrap_or('_').to_string())
|
||||||
.join(file_name_chars.next().unwrap_or('_').to_string())
|
.join(fn_chars.next().unwrap_or('_').to_string())
|
||||||
.join(file_name);
|
.join(file_name);
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn _list_versions(script_base_path: &PathBuf) -> Vec<(PathBuf, String)> {
|
||||||
|
let mut ret = vec![];
|
||||||
|
match fs::read_dir(script_base_path) {
|
||||||
|
Err(e) => error!("Read dir: {:?}, error: {}", script_base_path, e),
|
||||||
|
Ok(paths) => for path in paths {
|
||||||
|
match path {
|
||||||
|
Err(e) => error!("List dir: {:?}, error: {}", script_base_path, e),
|
||||||
|
Ok(path) => if path.path().is_dir() {
|
||||||
|
trace!("Found dir: {:?}", path);
|
||||||
|
match path.file_name().to_str() {
|
||||||
|
None => error!("Get path file name: {:?}, is none", path),
|
||||||
|
Some(file_name) => if file_name.starts_with("v") {
|
||||||
|
let ver = get_dir_ver(&file_name);
|
||||||
|
ret.push((path.path(), ver));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_dir_ver(dir_name: &str) -> String {
|
||||||
|
dir_name.chars().skip_while(|c| *c != '-').skip(1).collect::<String>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_dir_ver() {
|
||||||
|
assert_eq!("".to_owned(), get_dir_ver(""));
|
||||||
|
assert_eq!("".to_owned(), get_dir_ver("v0001"));
|
||||||
|
assert_eq!("".to_owned(), get_dir_ver("v00011.0"));
|
||||||
|
assert_eq!("0.0.1".to_owned(), get_dir_ver("v0001-0.0.1"));
|
||||||
|
assert_eq!("1.0.1".to_owned(), get_dir_ver("v0001-1.0.1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_script_base_path() {
|
||||||
|
use std::str::FromStr;
|
||||||
|
let base = PathBuf::from_str("/sample").unwrap();
|
||||||
|
let script_base_path = _get_script_base_path(&base, "s.crs");
|
||||||
|
assert_eq!(PathBuf::from_str("/sample/s/_/_/s.crs").unwrap(), script_base_path);
|
||||||
|
let script_base_path = _get_script_base_path(&base, "sa.crs");
|
||||||
|
assert_eq!(PathBuf::from_str("/sample/s/a/_/sa.crs").unwrap(), script_base_path);
|
||||||
|
let script_base_path = _get_script_base_path(&base, "sample.crs");
|
||||||
|
assert_eq!(PathBuf::from_str("/sample/s/a/m/sample.crs").unwrap(), script_base_path);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user