git_helpe_rs/
run_mode.rs

1use std::process::{Command, Stdio};
2
3use anyhow::{Error, Result};
4
5use crate::{cli::DryRunAndCopyFlag, git_config::GitConfig};
6
7pub enum RunMode {
8    Normal,
9    DryRun,
10    DryRunAndCopy,
11    Copy,
12}
13
14pub fn get_run_mode_from_options(flags: DryRunAndCopyFlag) -> RunMode {
15    if flags.copy {
16        if flags.dry_run {
17            RunMode::DryRunAndCopy
18        } else {
19            RunMode::Copy
20        }
21    } else {
22        if flags.dry_run {
23            RunMode::DryRun
24        } else {
25            RunMode::Normal
26        }
27    }
28}
29
30pub fn run_copy(config: &GitConfig, value_to_copy: String) -> Result<(), Error> {
31    let copy_command = config.data.clipboard_commands.copy.to_string();
32
33    let echo = Command::new("echo")
34        .arg(value_to_copy)
35        .stdout(Stdio::piped())
36        .spawn()
37        .unwrap();
38
39    Command::new(copy_command)
40        .stdin(Stdio::from(echo.stdout.unwrap()))
41        .output()
42        .unwrap();
43
44    Ok(())
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_get_run_mode_from_options() {
53        // Test case: Copy flag is true, DryRun flag is true
54        let flags = DryRunAndCopyFlag {
55            copy: true,
56            dry_run: true,
57        };
58        let run_mode = get_run_mode_from_options(flags);
59        match run_mode {
60            RunMode::DryRunAndCopy => assert!(true),
61            _ => assert!(false),
62        }
63
64        // Test case: Copy flag is true, DryRun flag is false
65        let flags = DryRunAndCopyFlag {
66            copy: true,
67            dry_run: false,
68        };
69        let run_mode = get_run_mode_from_options(flags);
70        match run_mode {
71            RunMode::Copy => assert!(true),
72            _ => assert!(false),
73        }
74
75        // Test case: Copy flag is false, DryRun flag is true
76        let flags = DryRunAndCopyFlag {
77            copy: false,
78            dry_run: true,
79        };
80        let run_mode = get_run_mode_from_options(flags);
81        match run_mode {
82            RunMode::DryRun => assert!(true),
83            _ => assert!(false),
84        }
85
86        // Test case: Copy flag is false, DryRun flag is false
87        let flags = DryRunAndCopyFlag {
88            copy: false,
89            dry_run: false,
90        };
91        let run_mode = get_run_mode_from_options(flags);
92        match run_mode {
93            RunMode::Normal => assert!(true),
94            _ => assert!(false),
95        }
96    }
97}