Skip to content

Commit f1311f5

Browse files
authored
Merge pull request #15 from rage/refactoring
Refactoring
2 parents 910eb46 + b347600 commit f1311f5

22 files changed

Lines changed: 414 additions & 460 deletions

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: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
#[cfg(target_os = "windows")]
2-
use crate::updater;
3-
use command_util::{get_organization, Client, ClientProduction};
4-
use courses_command::list_courses;
5-
use download_command::download_or_update;
6-
use exercises_command::list_exercises;
7-
use login_command::login;
8-
use logout_command::logout;
9-
use organization_command::organization;
10-
use update_command::update;
11-
pub mod command_util;
12-
mod courses_command;
13-
mod download_command;
14-
mod exercises_command;
15-
mod login_command;
16-
mod logout_command;
17-
mod organization_command;
18-
mod paste_command;
19-
mod submit_command;
20-
mod test_command;
21-
mod update_command;
1+
mod courses;
2+
mod download;
3+
mod exercises;
4+
mod login;
5+
mod logout;
6+
mod organization;
7+
mod paste;
8+
mod submit;
9+
mod test;
10+
mod update;
11+
mod util;
2212

23-
use crate::io_module::{Io, PrintColor};
13+
use crate::io::{Io, PrintColor};
14+
use util::{Client, ClientProduction};
2415

2516
pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
2617
let mut client = ClientProduction::new(matches.is_present("testmode"));
@@ -50,7 +41,7 @@ pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
5041

5142
// Check that organization is set
5243
if let Some(("download" | "courses", _)) = matches.subcommand() {
53-
if get_organization().is_none() {
44+
if util::get_organization().is_none() {
5445
io.println(
5546
"No organization found. Run 'tmc organization' first.",
5647
PrintColor::Failed,
@@ -62,52 +53,52 @@ pub fn handle(matches: &clap::ArgMatches, io: &mut dyn Io) {
6253
match matches.subcommand() {
6354
Some(("login", args)) => {
6455
let interactive_mode = !args.is_present("non-interactive");
65-
login(io, &mut client, interactive_mode)
56+
login::login(io, &mut client, interactive_mode)
6657
}
67-
Some(("download", args)) => download_or_update(
58+
Some(("download", args)) => download::download_or_update(
6859
io,
6960
&mut client,
7061
args.value_of("course"),
7162
args.is_present("currentdir"),
7263
),
7364
Some(("update", args)) => {
74-
update(io, &mut client, args.is_present("currentdir"));
65+
update::update(io, &mut client, args.is_present("currentdir"));
7566
}
7667
Some(("organization", args)) => {
7768
let interactive_mode = !args.is_present("non-interactive");
78-
organization(io, &mut client, interactive_mode)
69+
organization::organization(io, &mut client, interactive_mode)
7970
}
80-
Some(("courses", _)) => list_courses(io, &mut client),
71+
Some(("courses", _)) => courses::list_courses(io, &mut client),
8172
Some(("submit", args)) => {
82-
submit_command::submit(io, &mut client, args.value_of("exercise"));
73+
submit::submit(io, &mut client, args.value_of("exercise"));
8374
}
8475
Some(("exercises", args)) => {
8576
if let Some(c) = args.value_of("course") {
86-
list_exercises(io, &mut client, String::from(c));
77+
exercises::list_exercises(io, &mut client, c);
8778
} else {
8879
io.println("argument for course not found", PrintColor::Normal);
8980
}
9081
}
9182
Some(("test", args)) => {
92-
test_command::test(io, args.value_of("exercise"));
83+
test::test(io, args.value_of("exercise"));
9384
}
9485
Some(("paste", args)) => {
95-
paste_command::paste(io, &mut client, args.value_of("exercise"));
86+
paste::paste(io, &mut client, args.value_of("exercise"));
9687
}
97-
Some(("logout", _)) => logout(io, &mut client),
88+
Some(("logout", _)) => logout::logout(io, &mut client),
9889
Some(("fetchupdate", _)) => {
9990
#[cfg(target_os = "windows")]
100-
updater::process_update();
91+
crate::updater::process_update();
10192
}
10293
Some(("cleartemp", _)) => {
10394
#[cfg(target_os = "windows")]
104-
updater::cleartemp().unwrap();
95+
crate::updater::cleartemp().unwrap();
10596
}
10697
Some(("elevateddownload", _)) => {
107-
download_command::elevated_download(io, &mut client);
98+
download::elevated_download(io, &mut client);
10899
}
109100
Some(("elevatedupdate", _)) => {
110-
update_command::elevated_update(io, &mut client);
101+
update::elevated_update(io, &mut client);
111102
}
112103
_ => (), // Unknown subcommand or no subcommand was given
113104
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use super::command_util::Client;
2-
use crate::io_module::{Io, PrintColor};
1+
use super::util::Client;
2+
use crate::io::{Io, PrintColor};
33
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
}

0 commit comments

Comments
 (0)