cmd_lib_core 0.4.0

Common rust commandline macros and utils, to write shell script like tasks easily
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::process::{Child, Command, ExitStatus, Stdio};
use std::io::{Error, ErrorKind};
use std::fs::{File, OpenOptions};
use std::os::unix::io::{FromRawFd, AsRawFd};
use std::collections::HashSet;
use crate::proc_env::Env;
use crate::proc_env::ENV_VARS;
use crate::{CmdResult, FunResult};

pub struct GroupCmds {
     cmds: Vec<(Cmds, Option<Cmds>)>,  // (cmd, orCmd) pairs
     cmds_env: Env,
}

impl GroupCmds {
    pub fn new() -> Self {
        Self {
            cmds: vec![],
            cmds_env: Env::new(),
        }
    }

    pub fn add(&mut self, cmds: Cmds, or_cmds: Option<Cmds>) -> &mut Self {
        self.cmds.push((cmds, or_cmds));
        self
    }

    pub fn run_cmd(&mut self) -> CmdResult {
        for cmd in self.cmds.iter_mut() {
            if let Err(err) = cmd.0.run_cmd(&mut self.cmds_env) {
                if let Some(or_cmds) = &mut cmd.1 {
                    or_cmds.run_cmd(&mut self.cmds_env)?;
                } else {
                    return Err(err);
                }
            }
        }
        Ok(())
    }

    pub fn run_fun(&mut self) -> FunResult {
        let mut ret = String::new();
        for cmd in self.cmds.iter_mut() {
            let ret0 = cmd.0.run_fun(&mut self.cmds_env);
            match ret0 {
                Err(e) => {
                    if let Some(or_cmds) = &mut cmd.1 {
                        ret = or_cmds.run_fun(&mut self.cmds_env)?;
                    } else {
                        return Err(e);
                    }
                },
                Ok(r) => ret = r,
            };
        }
        Ok(ret)
    }
}

pub struct BuiltinCmds {
    cmds: Vec<String>,
}

impl BuiltinCmds {
    pub fn from_vec(cmds: &Vec<String>) -> Self {
        Self {
            cmds: cmds.to_vec(),
        }
    }

    pub fn is_builtin(cmd: &str) -> bool {
        let mut builtins = HashSet::new();
        builtins.insert("cd");
        builtins.insert("true");
        builtins.contains(cmd)
    }

    pub fn run_cmd(&mut self, cmds_env: &mut Env) -> CmdResult {
        match self.cmds[0].as_str() {
            "true" => self.run_true_cmd(cmds_env),
            "cd" => self.run_cd_cmd(cmds_env),
            _ => panic!("invalid builtin cmd: {}", self.cmds[0]),
        }
    }

    fn run_true_cmd(&mut self, _cmds_env: &mut Env) -> CmdResult {
        if self.cmds.len() != 1 {
            let err = Error::new(
                ErrorKind::Other,
                format!("true: too many arguments: {}", self.cmds.join(" ")),
            );
            return Err(err);
        }
        Ok(())
    }

    fn run_cd_cmd(&mut self, cmds_env: &mut Env) -> CmdResult {
        let mut dir = self.cmds[1].clone();
        if self.cmds.len() != 2 {
            let err = Error::new(
                ErrorKind::Other,
                format!("cd: too many arguments: {}", self.cmds.join(" ")),
            );
            return Err(err);
        }
        // if it is relative path, always convert it to absolute one
        if !dir.starts_with("/") {
            ENV_VARS.with(|vars| {
                if let Some(cmd_lib_pwd) = vars.borrow().get("PWD") {
                    dir = format!("{}/{}", cmd_lib_pwd, dir);
                } else {
                    dir = format!("{}/{}", std::env::current_dir().unwrap().to_str().unwrap(), dir);
                }
            });
        }
        if !std::path::Path::new(&dir).exists() {
            let err_msg = format!("cd: {}: No such file or directory", dir);
            eprintln!("{}", err_msg);
            let err = Error::new(
                ErrorKind::Other,
                err_msg,
            );
            return Err(err);
        }
        cmds_env.set_var("PWD".to_string(), dir);
        Ok(())
    }
}


pub struct Cmds {
    pipes: Vec<Command>,
    children: Vec<Child>,

    cmd_args: Vec<Cmd>,
    full_cmd: String,
}

impl Cmds {
    pub fn new() -> Self {
        Self {
            pipes: vec![],
            children: vec![],
            cmd_args: vec![],
            full_cmd: String::new(),
        }
    }

    pub fn from_cmd(mut cmd: Cmd) -> Self {
        let cmd_args: Vec<String> = cmd.get_args().to_vec();
         Self {
            pipes: vec![cmd.gen_command()],
            children: vec![],
            full_cmd: cmd_args.join(" ").to_string(),
            cmd_args: vec![cmd],
        }
    }

    pub fn is_empty(&self) -> bool {
        self.pipes.is_empty()
    }

    pub fn pipe(&mut self, mut cmd: Cmd) -> &mut Self {
        if !self.pipes.is_empty() {
            let last_i = self.pipes.len() - 1;
            self.pipes[last_i].stdout(Stdio::piped());
        }

        let cmd_args: Vec<String> = cmd.get_args().to_vec();
        let pipe_cmd = cmd.gen_command();
        self.pipes.push(pipe_cmd);

        if !self.full_cmd.is_empty() {
            self.full_cmd += " | ";
        }
        self.full_cmd += &cmd_args.join(" ");
        self.cmd_args.push(cmd);
        self
    }

    fn spawn(&mut self) -> CmdResult {
        let mut pipe_error = false;

        ENV_VARS.with(|vars| {
            if let Some(dir) = vars.borrow().get("PWD") {
                self.full_cmd += &format!(" (cd: {})", dir);
                self.pipes[0].current_dir(dir);
            }
            let mut debug = String::from("0");
            if let Some(proc_debug) = vars.borrow().get("CMD_LIB_DEBUG") {
                debug = proc_debug.clone();
            } else if let Ok(global_debug) = std::env::var("CMD_LIB_DEBUG") {
                debug = global_debug.clone();
            }
            if debug == "1" {
                eprintln!("Running \"{}\" ...", self.full_cmd);
            }

            if let Some("1") = vars.borrow().get("CMD_LIB_PIPE_FAIL").map(|v| v as &str) {
                pipe_error = true;
            } else if let Ok("1") = std::env::var("CMD_LIB_PIPE_FAIL").as_ref().map(|v| v as &str) {
                pipe_error = true;
            }
        });

        for (i, cmd) in self.pipes.iter_mut().enumerate() {
            if i != 0 {
                cmd.stdin(self.children[i - 1].stdout.take().unwrap());
            }
            self.children.push(cmd.spawn()?);
            if i % 2 != 0 {
                self.children[i - 1].wait()?;
            }
            if pipe_error {
                let status = self.children[i].wait()?;
                if !status.success() {
                    return Err(Self::to_io_error(&format!("{:?}", cmd), status))
                }
            }
        }

        Ok(())
    }

    pub fn run_cmd(&mut self, cmds_env: &mut Env) -> CmdResult {
        // check builtin commands
        if BuiltinCmds::is_builtin(&self.cmd_args[0].get_args()[0]) {
            return BuiltinCmds::from_vec(&self.cmd_args[0].get_args()).run_cmd(cmds_env);
        }

        self.spawn()?;
        let status = self.children.pop().unwrap().wait()?;
        if !status.success() {
            Err(Self::to_io_error(&self.full_cmd, status))
        } else {
            Ok(())
        }
    }

    pub fn run_fun(&mut self, _cmds_env: &mut Env) -> FunResult {
        let last_i = self.pipes.len() - 1;
        self.pipes[last_i].stdout(Stdio::piped());

        self.spawn()?;
        let output = self.children.pop().unwrap().wait_with_output()?;
        if !output.status.success() {
            Err(Self::to_io_error(&self.full_cmd, output.status))
        } else {
            let mut ret = String::from_utf8_lossy(&output.stdout).to_string();
            if ret.ends_with('\n') {
                ret.pop();
            }
            Ok(ret)
        }
    }

    fn to_io_error(command: &str, status: ExitStatus) -> Error {
        if let Some(code) = status.code() {
            Error::new(ErrorKind::Other, format!("{} exit with {}", command, code))
        } else {
            Error::new(ErrorKind::Other, "Unknown error")
        }
   }
}

pub enum FdOrFile {
    Fd(i32, bool),          // fd, append?
    File(String, bool),     // file, append?
    OpenedFile(File, bool), // opened file, append?
}
impl FdOrFile {
    pub fn is_orig_stdout(&self) -> bool {
        if let FdOrFile::Fd(1, _) = self {
            true
        } else {
            false
        }
    }
}

pub struct Cmd {
    args: Vec<String>,
    redirects: Vec<(i32, FdOrFile)>,
}

impl Cmd {
    pub fn new() -> Self {
        Self {
            args: vec![],
            redirects: vec![],
        }
    }

    pub fn from_args<I, S>(args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self {
            args: args.into_iter()
                .map(|s| s.as_ref().to_owned())
                .collect(),
            redirects: vec![],
        }
    }

    pub fn add_arg(&mut self, arg: String) -> &mut Self {
        self.args.push(arg);
        self
    }

    pub fn get_args(&mut self) -> &mut Vec<String> {
        &mut self.args
    }

    pub fn set_redirect(&mut self, fd: i32, target: FdOrFile) -> &mut Self {
        self.redirects.push((fd, target));
        self
    }

    pub fn is_empty(&self) -> bool {
        self.args.is_empty()
    }

    pub fn gen_command(&mut self) -> Command {
        let cmd_args: Vec<String> = self.get_args().to_vec();
        let mut cmd = Command::new(&cmd_args[0]);
        cmd.args(&cmd_args[1..]);

        for (fd_src, target) in self.redirects.iter_mut() {
            match &target {
                FdOrFile::Fd(fd, _append) => {
                    let out = unsafe {Stdio::from_raw_fd(*fd)};
                    match *fd_src {
                        1 => cmd.stdout(out),
                        2 => cmd.stderr(out),
                        _ => panic!("invalid fd: {}", *fd_src),
                    };
                },
                FdOrFile::File(file, append) => {
                    if file == "/dev/null" {
                        match *fd_src {
                            0 => cmd.stdin(Stdio::null()),
                            1 => cmd.stdout(Stdio::null()),
                            2 => cmd.stderr(Stdio::null()),
                            _ => panic!("invalid fd: {}", *fd_src),
                        };
                    } else {
                        let f = if *fd_src == 0 {
                            OpenOptions::new()
                                .read(true)
                                .open(file)
                                .unwrap()
                        } else {
                            OpenOptions::new()
                                .create(true)
                                .truncate(!append)
                                .write(true)
                                .append(*append)
                                .open(file)
                                .unwrap()
                        };
                        let fd = f.as_raw_fd();
                        let out = unsafe {Stdio::from_raw_fd(fd)};
                        match *fd_src {
                            0 => cmd.stdin(out),
                            1 => cmd.stdout(out),
                            2 => cmd.stderr(out),
                            _ => panic!("invalid fd: {}", *fd_src),
                        };
                        *target = FdOrFile::OpenedFile(f, *append);
                    }
                },
                _ => {
                    panic!("file is already opened");
                }
            };
        }

        cmd
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_run_piped_cmds() {
        assert!(Cmds::from_cmd(Cmd::from_args(vec!["echo", "rust"]))
                .pipe(Cmd::from_args(vec!["wc"]))
                .run_cmd(&mut Env::new())
                .is_ok());
    }

    #[test]
    fn test_run_piped_funs() {
        assert_eq!(Cmds::from_cmd(Cmd::from_args(vec!["echo", "rust"]))
                   .run_fun(&mut Env::new())
                   .unwrap(),
                   "rust");

        assert_eq!(Cmds::from_cmd(Cmd::from_args(vec!["echo", "rust"]))
                   .pipe(Cmd::from_args(vec!["wc", "-c"]))
                   .run_fun(&mut Env::new())
                   .unwrap()
                   .trim(), "5");
    }

    #[test]
    fn test_stdout_redirect() {
        let tmp_file = "/tmp/file_echo_rust";
        let mut write_cmd = Cmd::from_args(vec!["echo", "rust"]);
        write_cmd.set_redirect(1, FdOrFile::File(tmp_file.to_string(), false));
        assert!(Cmds::from_cmd(write_cmd)
                .run_cmd(&mut Env::new())
                .is_ok());

        let read_cmd = Cmd::from_args(vec!["cat", tmp_file]);
        assert_eq!(Cmds::from_cmd(read_cmd)
                   .run_fun(&mut Env::new())
                   .unwrap(),
                   "rust");

        let cleanup_cmd = Cmd::from_args(vec!["rm", tmp_file]);
        assert!(Cmds::from_cmd(cleanup_cmd)
                .run_cmd(&mut Env::new())
                .is_ok());
    }
}