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
use crate::{Command, Result};
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]) -> Result<()> {
for (_i, c) in command_strs.iter().enumerate() {
let command = Command::new(c).exec_in(std::env::temp_dir())?;
self.history.push(command.clone());
}
Ok(())
}
pub fn exec_in<P: AsRef<Path>>(&mut self, command_str: &str, path: P) -> Result<Command> {
let command = Command::new(command_str).exec_in(&path)?;
self.history.push(command.clone());
Ok(command)
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let json = serde_json::to_string_pretty(&self)?;
std::fs::write(path.as_ref(), &json)?;
Ok(())
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<Commands> {
if path.as_ref().exists() {
let json = std::fs::read_to_string(path.as_ref())?;
let data = serde_json::from_str::<Commands>(&json)?;
Ok(data)
} else {
Ok(Commands::new())
}
}
pub fn find(&self, pattern: &str) -> Vec<Command> {
let mut results = Vec::new();
for c in &self.history {
if c.matches(pattern) {
results.push(c.clone());
}
}
results
}
}
impl Display for Commands {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
for (_i, c) in self.history.iter().enumerate() {
write!(f, "\n{}", c)?;
}
write!(f, "")
}
}
impl juniper::Context for Commands {}
#[cfg(test)]
#[test]
fn usage() {
let mut commands = Commands::new();
commands.exec(&["git version", "cargo version"]).unwrap();
}