gcd_cli/
scriptfile.rs

1use std::io;
2
3pub struct ScriptFile{
4    script_file_name: String,
5}
6
7impl ScriptFile {
8    pub fn new(script_file: String) -> Self {
9        ScriptFile{
10            script_file_name : script_file,
11        }
12    }
13
14    pub fn write_cd_only(&self, project: String) -> io::Result<()>{
15        script::write_cd_only(self.script_file_name.clone(), project)
16    }
17    pub fn write_cd_and_exec(&self, project: String, command: Vec<String>) -> io::Result<()>{
18        script::write_cd_and_exec(self.script_file_name.clone(), project, command)
19    }
20}
21
22#[cfg(unix)]
23mod script {
24    use std::io;
25    use std::fs;
26
27    pub fn write_cd_only(script_file_name: String, project: String) -> io::Result<()>{
28        let data = format!("#/bin/bash\ncd {}\n", project);
29        fs::write(script_file_name, data)
30    }
31    pub fn write_cd_and_exec(script_file_name: String, project: String, command: Vec<String>) -> io::Result<()>{
32        let data = format!("#/bin/bash\ncd {}\n{}\n", project, command.join(" "));
33        fs::write(script_file_name, data)
34    }
35}
36
37#[cfg(windows)]
38mod script {
39    use std::io;
40
41    use std::fs;
42
43    pub fn write_cd_only(script_file_name: String, project: String) -> io::Result<()>{
44        let data = format!("cd /d {}\n", project);
45        fs::write(script_file_name, data)
46    }
47    pub fn write_cd_and_exec(script_file_name: String, project: String, command: Vec<String>) -> io::Result<()>{
48        let data = format!("cd /d {}\n{}\n", project, command.join(" "));
49        fs::write(script_file_name, data)
50    }
51}
52
53#[cfg(test)]
54mod test {
55    use super::*;
56
57    #[cfg(unix)]
58    #[test]
59    fn write_cd_only() {
60        let script_file = ScriptFile::new("target/tmp/test_cd_only".to_owned());
61
62        assert!(script_file.write_cd_only("sample".to_owned()).is_ok());
63        assert_eq!(std::fs::read_to_string("target/tmp/test_cd_only").unwrap(), "#/bin/bash\ncd sample\n");
64
65    }
66
67    #[cfg(unix)]
68    #[test]
69    fn write_cd_and_exec() {
70        let script_file = ScriptFile::new("target/tmp/test_cd_and_exec".to_owned());
71
72        assert!(script_file.write_cd_and_exec("sample".to_owned(), vec!["ls".to_owned()]).is_ok());
73        assert_eq!(std::fs::read_to_string("target/tmp/test_cd_and_exec").unwrap(), "#/bin/bash\ncd sample\nls\n");
74
75
76    }
77
78}