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
use dev_prefix::*;
use types::*;
use logging;
use user;
use utils;
use security;
use clap::{ArgMatches, ErrorKind as ClEk};
use ansi_term::Colour::Green;
mod export;
mod types;
mod matches;
mod ls;
mod check;
mod display;
mod fmt;
mod init;
mod tutorial;
#[cfg(feature="server")]
mod server;
#[cfg(test)]
mod tests;
pub fn get_loglevel(matches: &ArgMatches) -> Option<(u8, bool)> {
let verbosity = match matches.occurrences_of("verbose") {
v @ 0...3 => v,
_ => {
println!("ERROR: verbosity cannot be higher than 3");
return None;
}
} as u8;
let quiet = matches.is_present("quiet");
Some((verbosity, quiet))
}
#[cfg(feature="server")]
fn run_server(project: &Project, matches: &ArgMatches) -> Result<u8> {
if let Some(mat) = matches.subcommand_matches("server") {
let addr = server::get_cmd(mat);
server::run_cmd(project.clone(), &addr);
Ok(0)
} else {
Err(ErrorKind::NothingDone.into())
}
}
#[cfg(not(feature="server"))]
fn run_server(_: &Project, _: &ArgMatches) -> Result<u8> {
Err(ErrorKind::NothingDone.into())
}
pub fn cmd<W, I, T>(w: &mut W, args: I) -> Result<u8>
where I: IntoIterator<Item = T>,
T: Into<OsString> + clone::Clone,
W: io::Write
{
let matches = match matches::get_matches(args) {
Ok(m) => m,
Err(e) => {
match e.kind {
ClEk::HelpDisplayed | ClEk::VersionDisplayed => {
print!("{}", e);
return Ok(0);
}
_ => return Err(ErrorKind::CmdError(e.to_string()).into()),
}
}
};
match get_loglevel(&matches) {
Some((v, q)) => logging::init_logger(q, v, true).unwrap(),
None => panic!("could not find loglevel"),
};
let cwd = env::current_dir().unwrap();
let work_tree = match matches.value_of("work-tree") {
Some(w) => PathBuf::from(w),
None => cwd.to_path_buf(),
};
if !work_tree.is_dir() {
let msg = format!("ERROR: work-tree {} is not a directory",
work_tree.display());
return Err(ErrorKind::CmdError(msg).into());
}
if matches.subcommand_matches("init").is_some() {
info!("Calling the init command");
init::run_cmd(&work_tree)?;
return Ok(0);
}
if let Some(t) = matches.subcommand_matches("tutorial") {
info!("Calling the tutorial command");
let c = tutorial::get_cmd(t)?;
tutorial::run_cmd(&work_tree, c).unwrap();
return Ok(0);
}
let repo = match utils::find_repo(&work_tree) {
Some(r) => r,
None => {
let msg = "Could not find .art folder. Try running `art init`";
return Err(ErrorKind::CmdError(msg.to_string()).into());
}
};
debug!("using repo dir {:?}", repo);
let project = user::load_repo(&repo)?;
security::validate(&repo, &project)?;
debug!("settings={:?}", project.settings);
if let Some(ls) = matches.subcommand_matches("ls") {
info!("Calling the ls command");
let cmd = ls::get_cmd(ls).unwrap();
ls::run_cmd(w, &work_tree, &cmd, &project)
} else if matches.subcommand_matches("check").is_some() {
info!("Calling the check command");
let cmd = check::Cmd { color: types::COLOR_IF_POSSIBLE };
check::run_cmd(w, &work_tree, &project, &cmd)
} else if let Some(mat) = matches.subcommand_matches("fmt") {
info!("Calling the fmt command");
let c = fmt::get_cmd(mat)?;
fmt::run_cmd(w, &repo, &project, &c)
} else if let Some(mat) = matches.subcommand_matches("export") {
info!("Calling the export command");
let c = export::get_cmd(mat)?;
export::run_cmd(&cwd, &project, &c)
} else if match run_server(&project, &matches) {
Ok(r) => return Ok(r),
Err(err) => {
match *err.kind() {
ErrorKind::NothingDone => false,
_ => return Err(err),
}
}
} {
unreachable!();
} else {
write!(w,
"{} {}: use -h to show help",
Green.bold().paint("artifact"),
Green.paint(VERSION))
.unwrap();
return Ok(0);
}
}