FWDHYYDCRS27NN2LVAP3VBCAFJAQ32X6WF4KRRD5ZBXAKZPX6TFQC
fn parse_karbonfile(file_path: &PathBuf) -> Karbonfile {
let file = fs::File::open(file_path)
//
// Return any karbon.{yml, yaml} file if a directory
// is provided. Else return the path as is.
// /!\ Success doesn't mean that the file is valid /!\
//
fn find_resource_file(path: &PathBuf) -> PathBuf {
let mut path = path.canonicalize()
.expect("Failed to canonicalize the path provided.");
// If path is a directory try to find a path/karbon.yml file.
if path.is_dir() {
path.push("karbon.yml");
}
// If path/karbon.yml is not a file, try .yaml extension.
if !path.is_file() {
path.set_extension("yaml");
}
// At that point give up and panic.
if !path.is_file() {
panic!("Not a valid file path or no karbonfile found.");
}
return path;
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ResourceType {
api_version: String,
kind: String,
}
fn get_resource_type(path: &PathBuf) -> ResourceType {
let file = fs::File::open(path)
fn karbonfile(path: &PathBuf) -> (PathBuf, Karbonfile) {
let mut karbonfile_path: PathBuf = path
.canonicalize()
.expect("Failed to canonicalize the path provided.");
fn parse_karbonfile(path: &PathBuf) -> Karbonfile {
let file = fs::File::open(path)
.expect("Failed to open the file.");
if !karbonfile_path.is_file() {
karbonfile_path = karbonfile_path.with_extension("yaml");
};
fn merge_relative_path(first: &PathBuf, second: &PathBuf) -> PathBuf {
match (first.is_file(), first.parent(), second.is_relative()) {
// First is a file path and as a parent directory.
// Second is a relative path.
// Append Second to First and canonicalize the result.
(true, Some(path), true) => {
return path.join(second)
.canonicalize()
.expect("Failed to canonitize the merged paths");
}
// First is a file and it's doesn't have a parent (Not even "/").
// This shouldn't happen.
(true, None, _) => {
panic!("Failed to merge paths. The first path doesn't have any parent");
}
// First is a diretory and second is relative.
(false, _, true) => {
return first.join(second)
.canonicalize()
.expect("Failed to canonitize the merged paths");
}
// Second is not a relative path.
// In this case just return the Second path.
(_, _, false) => {
return second.clone();
}
}
}
fn build_karbonfile_list(path: &PathBuf, accumulator: &mut Vec<PathBuf>) {
let (karbonfile_path, content) = karbonfile(&path);
accumulator.push(karbonfile_path);
if resources.is_empty() {
// leaf since it's a karbonfile but there
// is no resources in it.
result.push(branch);
} else {
// not a leaf here since it's a karbonfile
// and there is resources to go through.
// So start a new branch an keep walking.
for r in resources {
let next_path = merge_relative_path(&path, &r);
for resource in content.resources.iter() {
let full_path = &path.join(resource);
match &full_path.file_name().unwrap().to_string_lossy() {
"karbon.yml" | "karbon.yaml" =>
build_karbonfile_list(&full_path, accumulator),
build_tree(next_path, branch.clone(), result);
}