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
use crate::Command;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::path::Path;
use std::vec::Vec;
#[derive(
Serialize,
Deserialize,
Clone,
Debug,
Hash,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
juniper::GraphQLObject,
)]
pub struct Commands {
pub history: Vec<Command>,
}
impl Commands {
pub fn new() -> Self {
Commands {
history: Vec::new(),
}
}
pub fn exec(&mut self, command_strs: &[&str]) {
for (_i, c) in command_strs.iter().enumerate() {
let command = Command::new(c).exec_in(std::env::temp_dir());
self.history.push(command.clone());
}
}
pub fn exec_in<P: AsRef<Path>>(&mut self, command_str: &str, path: P) -> Command {
let command = Command::new(command_str).exec_in(&path);
self.history.push(command.clone());
command
}
}
impl Display for Commands {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let mut text = String::new();
for (_i, c) in self.history.iter().enumerate() {
if text.len() > 0 {
text = text + "\n";
}
text = text + &format!("{}", c);
}
write!(f, "{}", text)
}
}
impl juniper::Context for Commands {}
#[cfg(test)]
#[test]
fn usage() {
let mut commands = Commands::new();
commands.exec(&["git version", "cargo version"]);
}