ggen_cli_lib/cmds/hook/
run.rs1use clap::Args;
23use ggen_utils::error::Result;
24
25#[derive(Args, Debug)]
26pub struct RunArgs {
27 pub name: String,
29
30 #[arg(short = 'v', long = "var")]
32 pub vars: Vec<String>,
33
34 #[arg(long)]
36 pub dry_run: bool,
37
38 #[arg(long)]
40 pub verbose: bool,
41}
42
43pub async fn run(args: &RunArgs) -> Result<()> {
45 if args.name.trim().is_empty() {
47 return Err(ggen_utils::error::Error::new("Hook name cannot be empty"));
48 }
49
50 println!("š Running hook '{}'...", args.name);
51
52 if args.dry_run {
53 println!(" Dry run enabled - no side effects will occur");
54 }
55
56 if args.verbose {
57 println!("\nHook details:");
58 println!(" Name: {}", args.name);
59 println!(" Dry run: {}", args.dry_run);
60 if !args.vars.is_empty() {
61 println!(" Variables:");
62 for var in &args.vars {
63 println!(" {}", var);
64 }
65 }
66 }
67
68 println!("\n Checking hook configuration...");
77 println!(" Loading template...");
78 println!(" Executing generation...");
79
80 if args.dry_run {
81 println!("\nā
Dry run completed successfully!");
82 println!(" No changes were made to the filesystem.");
83 } else {
84 println!("\nā
Hook '{}' executed successfully!", args.name);
85 println!(" Graph regeneration completed.");
86 }
87
88 Ok(())
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[tokio::test]
96 async fn test_run_hook_basic() {
97 let args = RunArgs {
98 name: "test-hook".to_string(),
99 vars: vec![],
100 dry_run: false,
101 verbose: false,
102 };
103 let result = run(&args).await;
104 assert!(result.is_ok());
105 }
106
107 #[tokio::test]
108 async fn test_run_hook_empty_name() {
109 let args = RunArgs {
110 name: "".to_string(),
111 vars: vec![],
112 dry_run: false,
113 verbose: false,
114 };
115 let result = run(&args).await;
116 assert!(result.is_err());
117 }
118
119 #[tokio::test]
120 async fn test_run_hook_dry_run() {
121 let args = RunArgs {
122 name: "test-hook".to_string(),
123 vars: vec![],
124 dry_run: true,
125 verbose: false,
126 };
127 let result = run(&args).await;
128 assert!(result.is_ok());
129 }
130
131 #[tokio::test]
132 async fn test_run_hook_with_vars() {
133 let args = RunArgs {
134 name: "test-hook".to_string(),
135 vars: vec!["file=main.rs".to_string(), "env=dev".to_string()],
136 dry_run: true,
137 verbose: true,
138 };
139 let result = run(&args).await;
140 assert!(result.is_ok());
141 }
142}