Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 7e944da

Browse files
author
jclc
committed
Merge branch 'master' into cache-test-results-jclc
2 parents 7e7714b + a821354 commit 7e944da

13 files changed

Lines changed: 110 additions & 92 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ Once installation is complete, you can log in using `tmc login`. This saves your
4848
~ $ tmc login
4949
username: my-username
5050
password:
51+
server address:
5152
Login successful.
5253
```
53-
By default, tmc-cli connects to University of Helsinki MOOC server. To log in to another server, specify the server with the `-s` or `--server` switch:
54-
```
55-
~ $ tmc login -s [SERVER_ADDRESS]
56-
```
5754

5855
##Listing courses
5956

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>fi.helsinki.cs.tmc.cli</groupId>
55
<artifactId>tmc-cli</artifactId>
6-
<version>0.5.1</version>
6+
<version>0.5.2</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.io.IOException;
3030
import java.nio.file.Path;
3131
import java.nio.file.Paths;
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
3234
import java.util.Date;
3335
import java.util.HashMap;
3436
import java.util.List;
@@ -105,7 +107,7 @@ private String[] parseArgs(String[] args) {
105107
return null;
106108
}
107109

108-
List<String> subArgs = line.getArgList();
110+
List<String> subArgs = new ArrayList<>(Arrays.asList(line.getArgs()));
109111
if (subArgs.size() > 0) {
110112
commandName = subArgs.remove(0);
111113
} else {

src/main/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommand.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,25 @@
1111
import org.apache.commons.cli.CommandLine;
1212
import org.apache.commons.cli.Options;
1313

14-
import org.slf4j.Logger;
15-
import org.slf4j.LoggerFactory;
16-
1714
import java.util.List;
1815

1916
/**
2017
* Command for listing all available courses to user.
2118
*/
2219
@Command(name = "courses", desc = "List the available courses")
2320
public class ListCoursesCommand extends AbstractCommand {
24-
private static final Logger logger = LoggerFactory.getLogger(ListCoursesCommand.class);
2521

2622
@Override
2723
public void getOptions(Options options) {
2824
}
2925

3026
@Override
3127
public void run(CommandLine args, Io io) {
32-
List<Course> courses;
33-
TmcCore core;
34-
35-
core = getApp().getTmcCore();
28+
TmcCore core = getApp().getTmcCore();
3629
if (core == null) {
3730
return;
3831
}
39-
courses = TmcUtil.listCourses(core);
32+
List<Course> courses = TmcUtil.listCourses(core);
4033
if (courses.isEmpty()) {
4134
io.println("No courses found on this server.");
4235
return;

src/main/java/fi/helsinki/cs/tmc/cli/command/ListExercisesCommand.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,34 @@ public void getOptions(Options options) {
3333
@Override
3434
public void run(CommandLine args, Io io) {
3535
this.io = io;
36-
String courseName;
37-
38-
// Get course name
39-
String[] stringArgs = args.getArgs();
40-
if (stringArgs.length == 0) {
41-
courseName = getCourseNameFromCurrentDirectory();
42-
if (courseName == null) {
43-
return;
44-
}
45-
} else {
46-
courseName = stringArgs[0];
36+
37+
String courseName = getCourseName(args);
38+
if (courseName == null) {
39+
return;
4740
}
4841

49-
// Get course exercises
50-
List<Exercise> exercises;
51-
if (args.hasOption("i")) {
52-
exercises = getExercisesFromServer(courseName);
53-
} else {
54-
exercises = getLocalExercises(courseName);
55-
}
42+
List<Exercise> exercises = getExercises(args, courseName);
5643
if (exercises == null) {
5744
return;
5845
}
5946

6047
printExercises(courseName, exercises, !args.hasOption("n"));
6148
}
49+
50+
private String getCourseName(CommandLine args) {
51+
String[] stringArgs = args.getArgs();
52+
if (stringArgs.length != 0) {
53+
return stringArgs[0];
54+
}
55+
return getCourseNameFromCurrentDirectory();
56+
}
57+
58+
private List<Exercise> getExercises(CommandLine args, String courseName) {
59+
if (args.hasOption("i")) {
60+
return getExercisesFromServer(courseName);
61+
}
62+
return getLocalExercises(courseName);
63+
}
6264

6365
private String getCourseNameFromCurrentDirectory() {
6466
CourseInfo info = getCourseInfoFromCurrentDirectory();
@@ -92,7 +94,7 @@ private List<Exercise> getExercisesFromServer(String courseName) {
9294
}
9395

9496
List<Exercise> exercises = course.getExercises();
95-
if (exercises.isEmpty()) {
97+
if (exercises == null || exercises.isEmpty()) {
9698
this.io.println("Course '" + courseName + "' doesn't have any exercises.");
9799
return null;
98100
}
@@ -116,6 +118,15 @@ private List<Exercise> getLocalExercises(String courseName) {
116118
}
117119

118120
private void printExercises(String courseName, List<Exercise> exercises, Boolean pager) {
121+
String str = getExercisesAsString(courseName, exercises);
122+
if (pager) {
123+
ExternalsUtil.showStringInPager(str, "exercise-list");
124+
} else {
125+
io.print(str);
126+
}
127+
}
128+
129+
private String getExercisesAsString(String courseName, List<Exercise> exercises) {
119130
StringBuilder sb = new StringBuilder("Course name: " + courseName);
120131
String prevDeadline = "";
121132

@@ -127,12 +138,7 @@ private void printExercises(String courseName, List<Exercise> exercises, Boolean
127138
}
128139
sb.append(getExerciseStatus(exercise));
129140
}
130-
131-
if (pager) {
132-
ExternalsUtil.showStringInPager(sb.toString(), "exercise-list");
133-
} else {
134-
io.print(sb.toString());
135-
}
141+
return sb.toString();
136142
}
137143

138144
private String getDeadline(Exercise exercise) {

src/main/java/fi/helsinki/cs/tmc/cli/command/LoginCommand.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,24 @@ public void getOptions(Options options) {
3636
@Override
3737
public void run(CommandLine args, Io io) {
3838
this.io = io;
39-
String username = getUsername(args);
40-
String password = getPassword(args);
41-
String serverAddress = getServerAddress(args);
39+
String username = getLoginInfo(args, "u", "username: ");
40+
String password = getLoginInfo(args, "p", "password: ");
41+
String serverAddress = getLoginInfo(args, "s", "server address: ");
4242

4343
Settings settings = new Settings(serverAddress, username, password);
4444
if (loginPossible(settings) && saveLoginSettings(settings)) {
4545
io.println("Login successful.");
4646
}
4747
}
4848

49-
private String getUsername(CommandLine line) {
50-
String username = line.getOptionValue("u");
51-
if (username == null) {
52-
username = io.readLine("username: ");
49+
private String getLoginInfo(CommandLine line, String option, String prompt) {
50+
String info = line.getOptionValue(option);
51+
if (info == null && option.equals("p")) {
52+
info = io.readPassword(prompt);
53+
} else if (info == null) {
54+
info = io.readLine(prompt);
5355
}
54-
return username;
55-
}
56-
57-
private String getPassword(CommandLine line) {
58-
String password = line.getOptionValue("p");
59-
if (password == null) {
60-
password = io.readPassword("password: ");
61-
}
62-
return password;
63-
}
64-
65-
private String getServerAddress(CommandLine line) {
66-
String serverAddress = line.getOptionValue("s");
67-
if (serverAddress == null) {
68-
// todo: don't hardcode the default value, get it from somewhere
69-
serverAddress = "https://tmc.mooc.fi/mooc";
70-
}
71-
return serverAddress;
56+
return info;
7257
}
7358

7459
private boolean saveLoginSettings(Settings settings) {
@@ -104,7 +89,7 @@ private boolean loginPossible(Settings settings) {
10489
return false;
10590
}
10691
}
107-
92+
10893
logger.error("Unable to connect to server", e);
10994
io.println("Unable to connect to server "
11095
+ settings.getServerAddress());

src/main/java/fi/helsinki/cs/tmc/cli/command/PasteCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void run(CommandLine args, Io io) {
4444
return;
4545
}
4646
WorkDir workdir = app.getWorkDir();
47-
List<String> argsList = args.getArgList();
47+
String[] stringArgs = args.getArgs();
4848

4949
// if (argsList.size() > 1) {
5050
// io.println(
@@ -53,13 +53,13 @@ public void run(CommandLine args, Io io) {
5353
// return;
5454
// }
5555
Boolean valid;
56-
if (argsList.isEmpty()) {
56+
if (stringArgs.length == 0) {
5757
valid = workdir.addPath();
58-
} else if (argsList.size() == 1) {
59-
valid = workdir.addPath(argsList.get(0));
58+
} else if (stringArgs.length == 1) {
59+
valid = workdir.addPath(stringArgs[0]);
6060
} else {
6161
io.println(
62-
"Error: Too many arguments. Expected 1, got " + argsList.size());
62+
"Error: Too many arguments. Expected 1, got " + stringArgs.length);
6363
return;
6464
}
6565
if (!valid) {

src/main/java/fi/helsinki/cs/tmc/cli/command/PropertiesCommand.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ public void getOptions(Options options) {
3131
public void run(CommandLine args, Io io) {
3232
this.io = io;
3333
Boolean unset = args.hasOption("u");
34-
List<String> arguments = args.getArgList();
34+
String[] arguments = args.getArgs();
3535
HashMap<String, String> props = getApp().getProperties();
36-
if (arguments.isEmpty()) {
36+
if (arguments.length == 0) {
3737
printAllProps(props);
3838
return;
3939
}
4040

41-
if (arguments.size() % 2 == 1 && !unset) {
41+
if (arguments.length % 2 == 1 && !unset) {
4242
io.println("Invalid argument count. Usage: tmc prop KEY VALUE ...");
4343
return;
4444
}
4545

4646
if (unset) {
47-
if (arguments.size() > 1) {
47+
if (arguments.length > 1) {
4848
io.print("Unsetting property keys:");
4949
for (String arg : arguments) {
5050
io.print(" " + arg);
@@ -58,25 +58,23 @@ public void run(CommandLine args, Io io) {
5858
io.println("Unset key " + key + ", was " + props.remove(key));
5959
}
6060
getApp().saveProperties();
61-
return;
6261
} else {
63-
if (arguments.size() > 2) {
62+
if (arguments.length > 2) {
6463
io.print("Setting property keys:");
65-
for (int i = 0; i < arguments.size(); i = i + 2) {
66-
io.print(" " + arguments.get(i) + "=>" + arguments.get(i + 1));
64+
for (int i = 0; i < arguments.length; i = i + 2) {
65+
io.print(" " + arguments[i] + "=>" + arguments[i + 1]);
6766
}
6867
io.println("");
6968
if (!io.readConfirmation("Are you sure?", true)) {
7069
return;
7170
}
7271
}
73-
for (int i = 0; i < arguments.size(); i = i + 2) {
74-
String last = props.put(arguments.get(i), arguments.get(i + 1));
75-
io.println("Set " + arguments.get(i) + "=>" + arguments.get(i + 1)
72+
for (int i = 0; i < arguments.length; i = i + 2) {
73+
String last = props.put(arguments[i], arguments[i + 1]);
74+
io.println("Set " + arguments[i] + "=>" + arguments[i + 1]
7675
+ ", was " + last);
7776
}
7877
getApp().saveProperties();
79-
return;
8078
}
8179
}
8280

src/main/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void getOptions(Options options) {
4545
public void run(CommandLine args, Io io) {
4646
// this.io = io;
4747

48-
List<String> exercisesFromArgs = parseArgs(args);
48+
String[] exercisesFromArgs = parseArgs(args);
4949
if (exercisesFromArgs == null) {
5050
return;
5151
}
@@ -124,9 +124,9 @@ private String getCourseName(WorkDir dirUtil) {
124124
return null;
125125
}
126126

127-
private List<String> parseArgs(CommandLine args) {
127+
private String[] parseArgs(CommandLine args) {
128128
this.showPassed = args.hasOption("a");
129129
this.showDetails = args.hasOption("d");
130-
return args.getArgList();
130+
return args.getArgs();
131131
}
132132
}

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void getOptions(Options options) {
4242
public void run(CommandLine args, Io io) {
4343
this.io = io;
4444

45-
List<String> exercisesFromArgs = parseArgs(args);
45+
String[] exercisesFromArgs = parseArgs(args);
4646
if (exercisesFromArgs == null) {
4747
return;
4848
}
@@ -132,9 +132,9 @@ public void checkForExerciseUpdates(TmcCore core, Course course) {
132132
io.println(Color.colorString(msg, Color.AnsiColor.ANSI_YELLOW));
133133
}
134134

135-
private List<String> parseArgs(CommandLine args) {
135+
private String[] parseArgs(CommandLine args) {
136136
this.showAll = args.hasOption("a");
137137
this.showDetails = args.hasOption("d");
138-
return args.getArgList();
138+
return args.getArgs();
139139
}
140140
}

0 commit comments

Comments
 (0)