hawk_cli/
actions.rs

1use colored::*;
2use std::fs;
3use std::path::Path;
4
5use crate::cli::InitFlags;
6use crate::models::config::Config;
7use crate::models::environment_files::list_files;
8use crate::models::files;
9use crate::models::files::*;
10use crate::models::workflow::Workflow;
11use crate::models::workspace::Workspace;
12use crate::utils;
13
14pub fn list(workspace: &Workspace, target: &str) {
15    let t = Path::new(target);
16
17    list_files(t)
18        .iter()
19        .filter(|f| {
20            utils::is_workflow_file(f)
21                && f.file_name()
22                    .unwrap()
23                    .to_str()
24                    .unwrap_or("")
25                    .starts_with(&workspace.name)
26        })
27        .map(|f| {
28            (
29                Workflow::load(f).expect("invalid workflow file"),
30                f.file_name().unwrap().to_str().unwrap_or(""),
31            )
32        })
33        .for_each(|(w, p)| {
34            println!("{}: {}", w.name.bold().cyan(), p);
35        })
36}
37
38pub fn init(flags: &InitFlags) -> files::Result<Config> {
39    let mut config_path = Path::new("hawk-config.yaml").to_path_buf();
40
41    if flags.json {
42        config_path = config_path.with_extension("json");
43    }
44
45    if flags.read_env {
46        let wk_target = flags
47            .clone()
48            .workflows
49            .unwrap_or_else(|| ".github/workflows".into());
50        let config = Config::init(".github/workflows", &wk_target)?;
51        config.write(config_path.as_path())?;
52
53        return Ok(config);
54    }
55
56    let config = Config::new(".github/workflows");
57    config.write(config_path.as_path())?;
58
59    println!("Project setup completed!");
60
61    Ok(config)
62}
63
64pub fn clean(workspace: Workspace, target: &str) -> std::io::Result<()> {
65    if let Ok(files) = fs::read_dir(workspace.path) {
66        for file in files {
67            match file {
68                Ok(f) => {
69                    if utils::is_workflow_file(&f.path()) {
70                        utils::remove_file(&f.path(), target, &workspace.name)?;
71
72                        println!(
73                            "Removing {}",
74                            utils::target_filename(&f.path(), target, &workspace.name)
75                                .underline()
76                                .blue()
77                        );
78                    }
79                }
80                Err(err) => println!("Failed to delete file: {}", err),
81            }
82        }
83    }
84
85    Ok(())
86}
87
88pub fn copy(workspace: &Workspace, target: &str) -> notify::Result<()> {
89    let mut copied = 0;
90    let mut skipped = 0;
91
92    if let Ok(content) = fs::read_dir(&workspace.path) {
93        for f in content {
94            match f {
95                Ok(path) => {
96                    let is_workflow = utils::is_workflow_file(&path.path());
97
98                    if is_workflow {
99                        utils::copy_file(&path.path(), target, &workspace.name)?;
100                        copied += 1;
101                    } else {
102                        skipped += 1;
103                    }
104                }
105                Err(err) => println!("Failed to copy: {}", err),
106            }
107        }
108    }
109
110    println!("{} skipped", skipped.to_string().yellow());
111    println!("{} copied", copied.to_string().green());
112
113    Ok(())
114}