Skip to content

Commit f76f70f

Browse files
committed
made output-path in several commands optional
1 parent cdf9993 commit f76f70f

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

tmc-langs-cli/src/app.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ pub fn create_app() -> App<'static, 'static> {
1515
.long("exercise-path")
1616
.required(true)
1717
.takes_value(true))
18-
.arg(Arg::with_name("output-path")
19-
.help("Path to the file where the check results will be written. Overwritten if it already exists.")
20-
.long("output-path")
21-
.required(true)
22-
.takes_value(true))
2318
.arg(Arg::with_name("locale")
2419
.help("Language as a three letter ISO 639-3 code, e.g. 'eng' or 'fin'.")
2520
.long("locale")
2621
.required(true)
22+
.takes_value(true))
23+
.arg(Arg::with_name("output-path")
24+
.help("If defined, the check results will be written to this path. Overwritten if it already exists.")
25+
.long("output-path")
2726
.takes_value(true)))
2827

2928
.subcommand(SubCommand::with_name("compress-project")
@@ -119,11 +118,6 @@ pub fn create_app() -> App<'static, 'static> {
119118
.long("exercise-path")
120119
.required(true)
121120
.takes_value(true))
122-
.arg(Arg::with_name("output-path")
123-
.help("Path to the file where the test results will be written. Overwritten if it already exists.")
124-
.long("output-path")
125-
.required(true)
126-
.takes_value(true))
127121
.arg(Arg::with_name("checkstyle-output-path")
128122
.help("Runs checkstyle if given. Path to the file where the style results will be written.")
129123
.long("checkstyle-output-path")
@@ -132,6 +126,10 @@ pub fn create_app() -> App<'static, 'static> {
132126
.arg(Arg::with_name("locale")
133127
.help("Language as a three letter ISO 639-3 code, e.g. 'eng' or 'fin'. Required if checkstyle-output-path is given.")
134128
.long("locale")
129+
.takes_value(true))
130+
.arg(Arg::with_name("output-path")
131+
.help("If defined, the test results will be written to this path. Overwritten if it already exists.")
132+
.long("output-path")
135133
.takes_value(true)))
136134

137135
.subcommand(SubCommand::with_name("scan-exercise")
@@ -142,9 +140,8 @@ pub fn create_app() -> App<'static, 'static> {
142140
.required(true)
143141
.takes_value(true))
144142
.arg(Arg::with_name("output-path")
145-
.help("Path to the file where the scan results will be written. Overwritten if it already exists.")
143+
.help("If given, the scan results will be written to this path. Overwritten if it already exists.")
146144
.long("output-path")
147-
.required(true)
148145
.takes_value(true)))
149146

150147
.subcommand(SubCommand::with_name("find-exercises")
@@ -155,9 +152,8 @@ pub fn create_app() -> App<'static, 'static> {
155152
.required(true)
156153
.takes_value(true))
157154
.arg(Arg::with_name("output-path")
158-
.help("Path to the file where the search results will be written. Overwritten if it already exists.")
155+
.help("If given, the search results will be written to this path. Overwritten if it already exists.")
159156
.long("output-path")
160-
.required(true)
161157
.takes_value(true)))
162158

163159
.subcommand(SubCommand::with_name("get-exercise-packaging-configuration")
@@ -168,9 +164,8 @@ pub fn create_app() -> App<'static, 'static> {
168164
.required(true)
169165
.takes_value(true))
170166
.arg(Arg::with_name("output-path")
171-
.help("Path to the file where the configuration will be written. Overwritten if it already exists.")
167+
.help("If given, the configuration will be written to this path. Overwritten if it already exists.")
172168
.long("output-path")
173-
.required(true)
174169
.takes_value(true)))
175170

176171
.subcommand(SubCommand::with_name("clean")

tmc-langs-cli/src/main.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use tmc_langs_core::oauth2::{
1919
basic::BasicTokenType, AccessToken, EmptyExtraTokenFields, Scope, StandardTokenResponse,
2020
};
2121
use tmc_langs_core::{FeedbackAnswer, TmcCore, Token};
22-
use tmc_langs_framework::io::submission_processing;
22+
use tmc_langs_framework::{domain::ValidationResult, io::submission_processing};
2323
use tmc_langs_util::{
2424
task_executor::{self, TmcParams},
2525
Language,
@@ -70,20 +70,20 @@ fn run() -> Result<()> {
7070
let exercise_path = matches.value_of("exercise-path").unwrap();
7171
let exercise_path = Path::new(exercise_path);
7272

73-
let output_path = matches.value_of("output-path").unwrap();
74-
let output_path = Path::new(output_path);
73+
let output_path = matches.value_of("output-path");
74+
let output_path = output_path.map(Path::new);
7575

7676
let locale = matches.value_of("locale").unwrap();
7777
let locale = into_locale(locale)?;
7878

79-
run_checkstyle(exercise_path, output_path, locale)?;
79+
let check_result = run_checkstyle_write_results(exercise_path, output_path, locale)?;
8080

81-
let output = Output::<()> {
81+
let output = Output {
8282
status: Status::Successful,
8383
message: Some("ran checkstyle".to_string()),
8484
result: OutputResult::ExecutedCommand,
8585
percent_done: 1.0,
86-
data: None,
86+
data: check_result,
8787
};
8888
print_output(&output)?
8989
} else if let Some(matches) = matches.subcommand_matches("compress-project") {
@@ -269,9 +269,10 @@ fn run() -> Result<()> {
269269
let exercise_path = matches.value_of("exercise-path").unwrap();
270270
let exercise_path = Path::new(exercise_path);
271271

272-
let output_path = matches.value_of("output-path").unwrap();
273-
let output_path = Path::new(output_path);
272+
let output_path = matches.value_of("output-path");
273+
let output_path = output_path.map(Path::new);
274274

275+
// todo: checkstyle results in stdout?
275276
let checkstyle_output_path = matches.value_of("checkstyle-output-path");
276277
let checkstyle_output_path: Option<&Path> = checkstyle_output_path.map(Path::new);
277278

@@ -282,13 +283,15 @@ fn run() -> Result<()> {
282283
)
283284
})?;
284285

285-
write_result_to_file_as_json(&test_result, output_path)?;
286+
if let Some(output_path) = output_path {
287+
write_result_to_file_as_json(&test_result, output_path)?;
288+
}
286289

287290
if let Some(checkstyle_output_path) = checkstyle_output_path {
288291
let locale = matches.value_of("locale").unwrap();
289292
let locale = into_locale(locale)?;
290293

291-
run_checkstyle(exercise_path, checkstyle_output_path, locale)?;
294+
run_checkstyle_write_results(exercise_path, Some(checkstyle_output_path), locale)?;
292295
}
293296

294297
let output = Output {
@@ -303,8 +306,8 @@ fn run() -> Result<()> {
303306
let exercise_path = matches.value_of("exercise-path").unwrap();
304307
let exercise_path = Path::new(exercise_path);
305308

306-
let output_path = matches.value_of("output-path").unwrap();
307-
let output_path = Path::new(output_path);
309+
let output_path = matches.value_of("output-path");
310+
let output_path = output_path.map(Path::new);
308311

309312
let exercise_name = exercise_path.file_name().with_context(|| {
310313
format!(
@@ -323,7 +326,9 @@ fn run() -> Result<()> {
323326
let scan_result = task_executor::scan_exercise(exercise_path, exercise_name.to_string())
324327
.with_context(|| format!("Failed to scan exercise at {}", exercise_path.display()))?;
325328

326-
write_result_to_file_as_json(&scan_result, output_path)?;
329+
if let Some(output_path) = output_path {
330+
write_result_to_file_as_json(&scan_result, output_path)?;
331+
}
327332

328333
let output = Output {
329334
status: Status::Successful,
@@ -337,8 +342,8 @@ fn run() -> Result<()> {
337342
let exercise_path = matches.value_of("exercise-path").unwrap();
338343
let exercise_path = Path::new(exercise_path);
339344

340-
let output_path = matches.value_of("output-path").unwrap();
341-
let output_path = Path::new(output_path);
345+
let output_path = matches.value_of("output-path");
346+
let output_path = output_path.map(Path::new);
342347

343348
let mut exercises = vec![];
344349
// silently skips errors
@@ -356,7 +361,9 @@ fn run() -> Result<()> {
356361
}
357362
}
358363

359-
write_result_to_file_as_json(&exercises, output_path)?;
364+
if let Some(output_path) = output_path {
365+
write_result_to_file_as_json(&exercises, output_path)?;
366+
}
360367

361368
let output = Output {
362369
status: Status::Successful,
@@ -371,8 +378,8 @@ fn run() -> Result<()> {
371378
let exercise_path = matches.value_of("exercise-path").unwrap();
372379
let exercise_path = Path::new(exercise_path);
373380

374-
let output_path = matches.value_of("output-path").unwrap();
375-
let output_path = Path::new(output_path);
381+
let output_path = matches.value_of("output-path");
382+
let output_path = output_path.map(Path::new);
376383

377384
let config = task_executor::get_exercise_packaging_configuration(exercise_path)
378385
.with_context(|| {
@@ -382,7 +389,9 @@ fn run() -> Result<()> {
382389
)
383390
})?;
384391

385-
write_result_to_file_as_json(&config, output_path)?;
392+
if let Some(output_path) = output_path {
393+
write_result_to_file_as_json(&config, output_path)?;
394+
}
386395

387396
let output = Output {
388397
status: Status::Successful,
@@ -1067,15 +1076,20 @@ fn find_exercise_directories(exercise_path: &Path) -> Vec<PathBuf> {
10671076
paths
10681077
}
10691078

1070-
fn run_checkstyle(exercise_path: &Path, output_path: &Path, locale: Language) -> Result<()> {
1079+
// if output_path is Some, the checkstyle results are written to that path
1080+
fn run_checkstyle_write_results(
1081+
exercise_path: &Path,
1082+
output_path: Option<&Path>,
1083+
locale: Language,
1084+
) -> Result<Option<ValidationResult>> {
10711085
let check_result =
10721086
task_executor::run_check_code_style(exercise_path, locale).with_context(|| {
10731087
format!(
10741088
"Failed to check code style for project at {}",
10751089
exercise_path.display()
10761090
)
10771091
})?;
1078-
if let Some(check_result) = check_result {
1092+
if let Some(output_path) = output_path {
10791093
let output_file = File::create(output_path).with_context(|| {
10801094
format!(
10811095
"Failed to create code style check results file at {}",
@@ -1089,5 +1103,5 @@ fn run_checkstyle(exercise_path: &Path, output_path: &Path, locale: Language) ->
10891103
)
10901104
})?;
10911105
}
1092-
Ok(())
1106+
Ok(check_result)
10931107
}

0 commit comments

Comments
 (0)