Skip to content

Commit b347600

Browse files
committed
Minor refactoring
1 parent b2fcf8a commit b347600

10 files changed

Lines changed: 111 additions & 139 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ authors = ["HoolaBoola <jaime.heikkiladias@helsinki.fi>",
77
"Nooblue <joni.sikio@helsinki.fi>"]
88
edition = "2021"
99
rust-version = "1.58.1"
10-
description = "Command line interface for TMC, written in Rust."
10+
description = "Client for downloading, testing and submitting exercises through the Test My Code system."
1111
license = "Apache-2.0"
12-
build = "build.rs"
1312

1413
[dependencies]
1514
anyhow = "1.0.56"

src/cli.rs

Lines changed: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,77 @@
11
use clap::{AppSettings, Arg, Command};
22

3-
const PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
4-
53
pub fn build_cli() -> Command<'static> {
6-
Command::new("Test My Code client written in Rust")
4+
Command::new(env!("CARGO_PKG_NAME"))
5+
.version(env!("CARGO_PKG_VERSION"))
6+
.about(env!("CARGO_PKG_DESCRIPTION"))
77
.arg_required_else_help(true)
8-
.version(PKG_VERSION.unwrap())
9-
.about("Client for downloading, testing and submitting exercises through the Test My Code system")
108
.subcommand(Command::new("courses").about("List the available courses"))
119
.subcommand(
1210
Command::new("download")
13-
.about("Downloads course exercises")
14-
.arg(
15-
Arg::new("course")
16-
.short('c')
17-
.long("course")
18-
.value_name("course name")
19-
.required(false),
11+
.about("Downloads course exercises")
12+
.arg(
13+
Arg::new("course")
14+
.short('c')
15+
.long("course")
16+
.value_name("course name")
17+
.required(false),
2018
)
21-
.arg(
22-
Arg::new("currentdir")
23-
.short('d')
24-
.long("currentdir")
25-
.required(false),
19+
.arg(
20+
Arg::new("currentdir")
21+
.short('d')
22+
.long("currentdir")
23+
.required(false),
2624
),
27-
)
25+
)
2826
.subcommand(
2927
Command::new("exercises")
30-
.about("List the exercises for a specific course")
31-
.arg(Arg::new("course").value_name("course").required(true)),
32-
)
28+
.about("List the exercises for a specific course")
29+
.arg(Arg::new("course").value_name("course").required(true)),
30+
)
3331
.subcommand(
34-
Command::new("login")
35-
.about("Login to TMC server")
36-
.arg(
32+
Command::new("login").about("Login to TMC server").arg(
3733
Arg::new("non-interactive")
38-
.short('n')
39-
.help("Initiates the non-interactive mode.")
40-
.long("non-interactive"),
41-
),
42-
)
34+
.short('n')
35+
.help("Initiates the non-interactive mode.")
36+
.long("non-interactive"),
37+
),
38+
)
4339
.subcommand(Command::new("logout").about("Logout from TMC server"))
4440
.subcommand(
4541
Command::new("organization")
46-
.about("Change organization")
47-
.arg(
48-
Arg::new("non-interactive")
49-
.short('n')
50-
.help("Initiates the non-interactive mode.")
51-
.long("non-interactive"),
42+
.about("Change organization")
43+
.arg(
44+
Arg::new("non-interactive")
45+
.short('n')
46+
.help("Initiates the non-interactive mode.")
47+
.long("non-interactive"),
5248
),
53-
)
49+
)
5450
.subcommand(
5551
Command::new("paste")
56-
.about("Submit exercise to TMC pastebin")
57-
.arg(
58-
Arg::new("exercise")
59-
.value_name("exercise")
60-
.required(false),
61-
),
62-
)
52+
.about("Submit exercise to TMC pastebin")
53+
.arg(Arg::new("exercise").value_name("exercise").required(false)),
54+
)
6355
.subcommand(
6456
Command::new("submit")
65-
.about("Submit exercises to TMC server")
66-
.arg(
67-
Arg::new("exercise")
68-
.value_name("exercise")
69-
.required(false),
70-
),
71-
)
57+
.about("Submit exercises to TMC server")
58+
.arg(Arg::new("exercise").value_name("exercise").required(false)),
59+
)
7260
.subcommand(
7361
Command::new("test")
74-
.about("Run local exercise tests")
75-
.arg(
76-
Arg::new("exercise")
77-
.value_name("exercise")
78-
.required(false),
79-
),
80-
)
62+
.about("Run local exercise tests")
63+
.arg(Arg::new("exercise").value_name("exercise").required(false)),
64+
)
8165
.subcommand(
8266
Command::new("fetchupdate")
83-
.hide(true)
84-
.about("Finishes the autoupdater. Administator rights needed."),
85-
)
67+
.hide(true)
68+
.about("Finishes the autoupdater. Administator rights needed."),
69+
)
8670
.subcommand(
8771
Command::new("cleartemp")
88-
.hide(true)
89-
.about("Removes tempfiles. Administator rights needed."),
90-
)
72+
.hide(true)
73+
.about("Removes tempfiles. Administator rights needed."),
74+
)
9175
.subcommand(
9276
Command::new("elevateddownload")
9377
.hide(true)
@@ -111,33 +95,26 @@ pub fn build_cli() -> Command<'static> {
11195
.subcommand(Command::new("update").about("Update exercises"))
11296
.arg(
11397
Arg::new("no-update")
114-
.short('d')
115-
.long("no-update")
116-
.help("Disable auto update temporarily"),
117-
)
98+
.short('d')
99+
.long("no-update")
100+
.help("Disable auto update temporarily"),
101+
)
118102
.arg(
119103
Arg::new("testmode")
120-
.long("testmode")
121-
.help("Only for internal testing, disables server connection"),
122-
)
123-
.subcommand(Command::new("generate-completions")
124-
.override_usage("tmc generate_completions --[your shell] > /path/to/your/completions/folder")
125-
.about("Generate completion scripts for command line usage.")
126-
.disable_version_flag(true)
127-
.hide(true)
128-
.setting(AppSettings::DeriveDisplayOrder)
129-
.arg(
130-
Arg::new("bash")
131-
.short('b')
132-
.long("bash"))
133-
.arg(
134-
Arg::new("zsh")
135-
.short('z')
136-
.long("zsh")
137-
)
138-
.arg(
139-
Arg::new("powershell")
140-
.short('p')
141-
.long("powershell"))
142-
)
104+
.long("testmode")
105+
.help("Only for internal testing, disables server connection"),
106+
)
107+
.subcommand(
108+
Command::new("generate-completions")
109+
.override_usage(
110+
"tmc generate_completions --[your shell] > /path/to/your/completions/folder",
111+
)
112+
.about("Generate completion scripts for command line usage.")
113+
.disable_version_flag(true)
114+
.hide(true)
115+
.setting(AppSettings::DeriveDisplayOrder)
116+
.arg(Arg::new("bash").short('b').long("bash"))
117+
.arg(Arg::new("zsh").short('z').long("zsh"))
118+
.arg(Arg::new("powershell").short('p').long("powershell")),
119+
)
143120
}

src/commands.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ mod update;
1111
mod util;
1212

1313
use crate::io::{Io, PrintColor};
14-
#[cfg(target_os = "windows")]
15-
use crate::updater;
1614
use util::{Client, ClientProduction};
1715

1816
pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
@@ -76,7 +74,7 @@ pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
7674
}
7775
Some(("exercises", args)) => {
7876
if let Some(c) = args.value_of("course") {
79-
exercises::list_exercises(io, &mut client, String::from(c));
77+
exercises::list_exercises(io, &mut client, c);
8078
} else {
8179
io.println("argument for course not found", PrintColor::Normal);
8280
}
@@ -90,11 +88,11 @@ pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
9088
Some(("logout", _)) => logout::logout(io, &mut client),
9189
Some(("fetchupdate", _)) => {
9290
#[cfg(target_os = "windows")]
93-
updater::process_update();
91+
crate::updater::process_update();
9492
}
9593
Some(("cleartemp", _)) => {
9694
#[cfg(target_os = "windows")]
97-
updater::cleartemp().unwrap();
95+
crate::updater::cleartemp().unwrap();
9896
}
9997
Some(("elevateddownload", _)) => {
10098
download::elevated_download(io, &mut client);

src/commands/courses.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use tmc_langs::Course;
44

55
/// Lists available courses from clients organization
66
pub fn list_courses(io: &mut dyn Io, client: &mut dyn Client) {
7-
let courses_result = client.list_courses();
8-
9-
match courses_result {
7+
match client.list_courses() {
108
Ok(course_list) => print_courses(io, course_list),
119
Err(error) => io.println(&error, PrintColor::Failed),
1210
}

src/commands/download.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::interactive;
44
use crate::io::{Io, PrintColor};
55
use crate::progress_reporting;
66
use crate::progress_reporting::ProgressBarManager;
7-
use std::path::PathBuf;
7+
use std::path::Path;
88
use std::process::Command;
99
use tmc_langs::ClientUpdateData;
1010
use tmc_langs::Course;
@@ -49,21 +49,22 @@ pub fn download_or_update(
4949
});
5050

5151
let name_select = if let Some(course) = course_name {
52-
course.to_string()
52+
course
5353
} else {
5454
match get_course_name(
5555
courses
5656
.iter()
5757
.map(|course| course.course.title.clone())
5858
.collect(),
5959
) {
60-
Ok(course) => courses
61-
.iter()
62-
.find(|c| c.course.title == course)
63-
.unwrap()
64-
.course
65-
.name
66-
.clone(),
60+
Ok(course) => {
61+
&courses
62+
.iter()
63+
.find(|c| c.course.title == course)
64+
.unwrap()
65+
.course
66+
.name
67+
}
6768
Err(msg) => {
6869
io.println(&msg, PrintColor::Failed);
6970
return;
@@ -91,10 +92,10 @@ pub fn download_or_update(
9192
get_projects_dir()
9293
};
9394

94-
let tmp_course = course.name.clone();
95-
let tmp_path = pathbuf.clone();
95+
let tmp_course = &course.name;
96+
let tmp_path = &pathbuf;
9697
let tmp_path = tmp_path.to_str().unwrap();
97-
match download_exercises(pathbuf, client, course) {
98+
match download_exercises(&pathbuf, client, &course) {
9899
Ok(msg) => io.println(&format!("\n{}", msg), PrintColor::Success),
99100
Err(msg) => {
100101
let os = std::env::consts::OS;
@@ -142,9 +143,9 @@ pub fn get_course_name(courses: Vec<String>) -> Result<String, String> {
142143
}
143144

144145
pub fn download_exercises(
145-
pathbuf: PathBuf,
146+
path: &Path,
146147
client: &mut dyn Client,
147-
course: Course,
148+
course: &Course,
148149
) -> Result<String, String> {
149150
match client.get_course_exercises(course.id) {
150151
Ok(exercises) => {
@@ -169,7 +170,7 @@ pub fn download_exercises(
169170
);
170171
manager.start::<ClientUpdateData>();
171172

172-
let result = client.download_or_update_exercises(&exercise_ids, pathbuf.as_path());
173+
let result = client.download_or_update_exercises(&exercise_ids, path);
173174

174175
match result {
175176
Ok(download_result) => {
@@ -203,7 +204,7 @@ pub fn download_exercises(
203204
if !downloaded.is_empty() {
204205
res.push_str(&format!(
205206
"\n\nSuccessful downloads saved to {}\\",
206-
pathbuf.to_str().unwrap()
207+
path.to_str().unwrap()
207208
));
208209
}
209210

@@ -222,7 +223,7 @@ pub fn download_exercises(
222223

223224
Ok(format!(
224225
"Exercises downloaded successfully to {}\\",
225-
pathbuf.to_str().unwrap()
226+
path.to_str().unwrap()
226227
))
227228
}
228229
pub fn elevated_download(io: &mut dyn Io, client: &mut dyn Client) {
@@ -235,8 +236,8 @@ pub fn elevated_download(io: &mut dyn Io, client: &mut dyn Client) {
235236
std::fs::remove_file(temp_file_path).unwrap();
236237
let split = params.split(';');
237238
let vec = split.collect::<Vec<&str>>();
238-
let path = PathBuf::from(vec[0]);
239-
let name_select = String::from(vec[1]);
239+
let path = Path::new(vec[0]);
240+
let name_select = &vec[1];
240241

241242
// Get course by name
242243
let course_result = match util::get_course_by_name(client, name_select) {
@@ -253,7 +254,7 @@ pub fn elevated_download(io: &mut dyn Io, client: &mut dyn Client) {
253254
}
254255
let course = course_result.unwrap();
255256
io.println("", PrintColor::Normal);
256-
match download_exercises(path, client, course) {
257+
match download_exercises(path, client, &course) {
257258
Ok(msg) => io.println(&msg, PrintColor::Success),
258259
Err(msg) => io.println(&msg, PrintColor::Failed),
259260
}

0 commit comments

Comments
 (0)