hawk_cli/
utils.rs

1use crate::models::files::File;
2use crate::models::workflow::Workflow;
3use std::fs;
4use std::path::Path;
5
6pub fn copy_file(source: &Path, target_dir: &str, scope: &str) -> std::io::Result<()> {
7    let filename = target_filename(source, target_dir, scope);
8    to_void_result(fs::copy(source, filename))
9}
10
11pub fn remove_file(source: &Path, target: &str, scope: &str) -> std::io::Result<()> {
12    let filename = target_filename(source, target, scope);
13
14    if Path::new(&filename).exists() {
15        fs::remove_file(filename)?
16    }
17
18    Ok(())
19}
20
21pub fn target_filename(source: &Path, target: &str, scope: &str) -> String {
22    let name = source.file_name().unwrap().to_str().unwrap();
23
24    format!("{}/{}--{}", target, scope, name)
25}
26
27pub fn to_void_result<T>(r: std::io::Result<T>) -> std::io::Result<()> {
28    match r {
29        Ok(_) => Ok(()),
30        Err(e) => Err(e),
31    }
32}
33
34#[deprecated(since = "0.1.4", note = "Use is_workflow_file instead")]
35pub fn is_yaml(path: &str) -> bool {
36    path.ends_with("yml") || path.ends_with("yaml")
37}
38
39// checks if the given filepath is a valid workflow file
40// path must be the entire filepath
41pub fn is_workflow_file(filepath: &Path) -> bool {
42    let is_yaml = match filepath.extension() {
43        None => false,
44        Some(ext) => ext.eq("yaml") || ext.eq("yml"),
45    };
46
47    if Workflow::load(filepath).is_err() {
48        return false;
49    }
50
51    is_yaml
52}