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
pub mod designsync;
pub mod git;
use crate::Result;
use designsync::Designsync;
use git::Git;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct RevisionControl {
driver: Box<dyn RevisionControlAPI>,
}
#[derive(Clone, Default)]
pub struct Credentials {
pub username: Option<String>,
pub password: Option<String>,
}
impl std::fmt::Debug for Credentials {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Credentials")
.field("username", &self.username)
.field("password", &"<-- Plaintext Password Withheld -->")
.finish()
}
}
#[derive(Clone, Default, Debug)]
pub struct Status {
pub added: Vec<PathBuf>,
pub removed: Vec<PathBuf>,
pub changed: Vec<PathBuf>,
pub conflicted: Vec<PathBuf>,
pub revision: String,
}
impl Status {
pub fn is_modified(&self) -> bool {
!self.added.is_empty()
|| !self.removed.is_empty()
|| !self.changed.is_empty()
|| !self.conflicted.is_empty()
|| !self.added.is_empty()
}
}
pub enum SupportedSystems {
Git,
Designsync,
}
impl SupportedSystems {
pub fn from_str(system: &str) -> Result<Self> {
let s = system.to_lowercase();
match s.as_str() {
"git" => Ok(Self::Git),
"design_sync" | "designsync" => Ok(Self::Designsync),
_ => error!("Unsupported revision control system '{}'", system),
}
}
}
impl RevisionControl {
pub fn new(
local: &Path,
remotes: Vec<&str>,
credentials: Option<Credentials>,
) -> RevisionControl {
if remotes.iter().any(|r| r.ends_with(".git")) {
RevisionControl {
driver: Box::new(RevisionControl::git(local, remotes, credentials)),
}
} else {
RevisionControl {
driver: Box::new(RevisionControl::designsync(local, remotes, credentials)),
}
}
}
pub fn from_config(config: &HashMap<String, String>) -> Result<Self> {
let driver: Box<dyn RevisionControlAPI>;
if let Some(c) = config.get("system") {
let _c = c.to_lowercase();
match _c.as_str() {
"git" => driver = Box::new(Self::git_from_config(config)?),
"designsync" | "design_sync" => {
driver = Box::new(Self::designsync_from_config(config)?)
}
_ => return error!("Unknown RC system '{}'", _c),
}
} else {
if config.contains_key("vault") {
if config.contains_key("remote") {
return error!("Both 'vault' and 'remote' cannot be used without specifying the 'system' parameter");
} else {
driver = Box::new(Self::designsync_from_config(config)?);
}
} else if config.contains_key("remote") {
driver = Box::new(Self::git_from_config(config)?);
} else {
return error!("Could not discern revision control system. None of 'remote', 'vault', or 'system' were given");
}
}
Ok(Self { driver: driver })
}
pub fn git(local: &Path, remotes: Vec<&str>, credentials: Option<Credentials>) -> Git {
Git::new(local, remotes, credentials)
}
pub fn git_from_config(config: &HashMap<String, String>) -> Result<Git> {
Ok(Self::git(
&Path::new(config.get("local").unwrap()),
match config.get("remote") {
Some(r) => vec![r],
None => return error!("Git driver must be given a 'remote' parameter"),
},
None,
))
}
pub fn designsync(
local: &Path,
remotes: Vec<&str>,
credentials: Option<Credentials>,
) -> Designsync {
if remotes.len() > 1 {
log_warning!("Multiple remotes were given to the DesignSync driver, but only the first one is currently used");
}
Designsync::new(local, remotes[0], credentials)
}
pub fn designsync_from_config(config: &HashMap<String, String>) -> Result<Designsync> {
Ok(Self::designsync(
&Path::new(config.get("local").unwrap()),
match config.get("vault") {
Some(v) => vec![v],
None => return error!("DesignSync driver muust be given a 'vault' parameter"),
},
None,
))
}
}
pub trait RevisionControlAPI: std::fmt::Debug {
fn populate(&self, version: &str) -> Result<()>;
fn checkout(&self, force: bool, path: Option<&Path>, version: &str) -> Result<bool>;
fn revert(&self, path: Option<&Path>) -> Result<()>;
fn status(&self, path: Option<&Path>) -> Result<Status>;
fn tag(&self, tagname: &str, force: bool, message: Option<&str>) -> Result<()>;
fn init(&self) -> Result<bool>;
fn is_initialized(&self) -> Result<bool>;
fn checkin(&self, files_or_dirs: Option<Vec<&Path>>, msg: &str) -> Result<String>;
}
impl RevisionControlAPI for RevisionControl {
fn populate(&self, version: &str) -> Result<()> {
self.driver.populate(version)
}
fn checkout(&self, force: bool, path: Option<&Path>, version: &str) -> Result<bool> {
self.driver.checkout(force, path, version)
}
fn revert(&self, path: Option<&Path>) -> Result<()> {
self.driver.revert(path)
}
fn status(&self, path: Option<&Path>) -> Result<Status> {
self.driver.status(path)
}
fn tag(&self, tagname: &str, force: bool, message: Option<&str>) -> Result<()> {
self.driver.tag(tagname, force, message)
}
fn init(&self) -> Result<bool> {
self.driver.init()
}
fn is_initialized(&self) -> Result<bool> {
self.driver.is_initialized()
}
fn checkin(&self, files_or_dirs: Option<Vec<&Path>>, msg: &str) -> Result<String> {
self.driver.checkin(files_or_dirs, msg)
}
}