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

Commit 6a2d639

Browse files
authored
Merge pull request #388 from testmycode/improvements-aleksi
Improvements aleksi
2 parents 3ac0533 + 7db978d commit 6a2d639

34 files changed

+545
-400
lines changed

scripts/autocompletion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SCRIPT_PATH will be filled by stub script
44
alias tmc="$SCRIPT_PATH/tmc"
55

6-
TMC_COMMANDS=( "\$(tmc shell-helper -c)" )
6+
TMC_COMMANDS=( "\$(tmc -d shell-helper -c)" )
77

88
_tmc_opts()
99
{

scripts/stub.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ EOM
103103
PROFILE_FILE=$(tmc_detect_profile)
104104

105105
# get the aliases
106+
#TODO remove this and use global variables
106107
set +euo pipefail
107108
source $PROFILE_FILE
108109
set -euo pipefail

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class Application {
4040
private final Options options;
4141
private final GnuParser parser;
4242
private String commandName;
43+
private boolean noAutoUpdate;
4344

4445
public Application(CliContext context) {
4546
this.parser = new GnuParser();
@@ -51,6 +52,7 @@ public Application(CliContext context) {
5152
options.addOption("h", "help", false, "Display help information about tmc-cli");
5253
options.addOption("v", "version", false, "Give the version of the tmc-cli");
5354
options.addOption("u", "force-update", false, "Force the auto-update");
55+
options.addOption("d", "no-update", false, "Disable temporarily the auto-update");
5456

5557
//TODO implement the inTests as context.property
5658
if (!context.inTests()) {
@@ -62,7 +64,7 @@ public Application(CliContext context) {
6264
private boolean runCommand(String name, String[] args) {
6365
AbstractCommand command = CommandFactory.createCommand(name);
6466
if (command == null) {
65-
io.println("Command " + name + " doesn't exist.");
67+
io.errorln("Command " + name + " doesn't exist.");
6668
return false;
6769
}
6870

@@ -87,13 +89,19 @@ private String[] parseArgs(String[] args) {
8789
}
8890

8991
if (commandName.startsWith("-")) {
90-
io.println("Unrecognized option: " + commandName);
92+
io.errorln("Unrecognized option: " + commandName);
9193
return null;
9294
}
9395

9496
boolean showHelp = line.hasOption("h");
9597
boolean showVersion = line.hasOption("v");
9698
boolean forceUpdate = line.hasOption("u");
99+
this.noAutoUpdate = line.hasOption("d");
100+
101+
if (forceUpdate && this.noAutoUpdate) {
102+
io.errorln("You can't use --force-update and --no-update at same time.");
103+
return null;
104+
}
97105

98106
if (showHelp) {
99107
// don't run the help sub-command with -h switch
@@ -127,7 +135,7 @@ public void run(String[] args) {
127135
return;
128136
}
129137

130-
if (!context.inTests() && versionCheck()) {
138+
if (!context.inTests() && !noAutoUpdate && versionCheck()) {
131139
return;
132140
}
133141

@@ -153,7 +161,7 @@ private boolean versionCheck() {
153161
try {
154162
time = Long.parseLong(previousTimestamp);
155163
} catch (NumberFormatException ex) {
156-
io.println("The previous update date isn't number.");
164+
io.errorln("The previous update date isn't number.");
157165
logger.warn("The previous update date isn't number.", ex);
158166
return false;
159167
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public Exercise getExercise(String name) {
8787
return null;
8888
}
8989

90+
public static String getExerciseDeadline(Exercise exercise) {
91+
if (exercise.getDeadline() == null) {
92+
return "none";
93+
}
94+
return exercise.getDeadlineDate().toString();
95+
}
96+
9097
public void setExercises(List<Exercise> exercises) {
9198
this.course.setExercises(exercises);
9299
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static Boolean save(CourseInfo course, Path courseInfoFile) {
3131
Files.createDirectories(courseInfoFile.getParent());
3232
Files.write(courseInfoFile, json);
3333
} catch (IOException e) {
34+
//TODO print to user
3435
logger.error("Could not create course file", e);
3536
return false;
3637
}
@@ -47,6 +48,7 @@ public static CourseInfo load(Path courseInfoFile) {
4748
try {
4849
reader = Files.newBufferedReader(courseInfoFile, Charset.forName("UTF-8"));
4950
} catch (IOException e) {
51+
//TODO print to user
5052
logger.error("Course file located, but failed to read from it", e);
5153
return null;
5254
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/SettingsIo.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,38 @@ static Path getConfigDirectory() {
116116
return configPath.resolve(CONFIG_DIR);
117117
}
118118

119-
//TODO handle exceptions
120119
private static Path getAccountsFile(Path configRoot) {
121120
Path file = configRoot.resolve(ACCOUNTS_CONFIG);
122-
if (!Files.exists(configRoot)) {
123-
try {
124-
Files.createDirectories(configRoot).getParent();
125-
} catch (IOException e) {
126-
}
127-
try {
128-
Files.createFile(configRoot);
129-
} catch (Exception e) {
130-
}
121+
if (!requireConfigDirectory(configRoot)) {
122+
return null;
131123
}
132124
return file;
133125
}
134126

135-
//TODO handle exceptions
136127
private static Path getPropertiesFile(Path configRoot) {
137128
Path file = configRoot.resolve(PROPERTIES_CONFIG);
129+
if (!requireConfigDirectory(configRoot)) {
130+
return null;
131+
}
132+
return file;
133+
}
134+
135+
/**
136+
* Create the config directory if it doesn't already exist.
137+
* @param configRoot config directory
138+
* @return false if directory can't be created.
139+
*/
140+
private static boolean requireConfigDirectory(Path configRoot) {
138141
if (!Files.exists(configRoot)) {
139142
try {
140-
Files.createDirectories(configRoot).getParent();
141-
} catch (Exception e) {
142-
}
143-
try {
144-
Files.createFile(configRoot);
143+
Files.createDirectories(configRoot);
145144
} catch (Exception e) {
145+
//TODO print error to user
146+
logger.error("Could not create config directory", e);
147+
return false;
146148
}
147149
}
148-
return file;
150+
return true;
149151
}
150152

151153
private static AccountList getHolderFromJson(Path file) {
@@ -154,6 +156,7 @@ private static AccountList getHolderFromJson(Path file) {
154156
try {
155157
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
156158
} catch (IOException e) {
159+
//TODO print error to user
157160
logger.error("Accounts file located, but failed to read from it", e);
158161
return null;
159162
}
@@ -166,6 +169,7 @@ private static boolean saveHolderToJson(AccountList holder, Path file) {
166169
try {
167170
Files.write(file, json);
168171
} catch (IOException e) {
172+
//TODO print error to user
169173
logger.error("Could not write account to accounts file", e);
170174
return false;
171175
}
@@ -178,6 +182,7 @@ private static HashMap<String, String> getPropertiesFromJson(Path file) {
178182
try {
179183
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
180184
} catch (IOException e) {
185+
//TODO print error to user
181186
logger.error("Properties file located, but failed to read from it", e);
182187
return null;
183188
}
@@ -192,6 +197,7 @@ private static boolean savePropertiesToJson(HashMap<String, String> properties,
192197
try {
193198
Files.write(file, json);
194199
} catch (IOException e) {
200+
//TODO print error to user
195201
logger.error("Could not write properties to file", e);
196202
return false;
197203
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/TmcUtil.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ public static URI sendPaste(CliContext ctx, Exercise exercise, String message) {
169169
} catch (Exception e) {
170170
TmcUtil.handleTmcExceptions(ctx, e);
171171
logger.error("Failed to send paste", e);
172-
ctx.getIo().println(e.toString());
173172
return null;
174173
}
175174
}
@@ -214,27 +213,28 @@ private static void handleTmcExceptions(CliContext ctx, Exception exception) {
214213
Throwable cause = exception.getCause();
215214

216215
if (isAuthenticationError(exception)) {
217-
io.println("Your username or password is not valid anymore.");
216+
io.errorln("Your username or password is not valid anymore.");
218217
return;
219218
}
220219
if (cause instanceof FailedHttpResponseException) {
221220
logger.error("Unable to connect to server", exception);
222-
io.println("Unable to connect to server.");
221+
io.errorln("Unable to connect to server.");
223222
return;
224223
}
225224
if (cause instanceof ObsoleteClientException) {
226225
logger.error("Outdated tmc client");
227-
io.println("Your tmc-cli is outdated. Please update it.");
226+
io.errorln("Your tmc-cli is outdated. Please update it.");
228227
ctx.getApp().runAutoUpdate();
229228
return;
230229
}
231230
if (cause != null && cause.getCause() instanceof UnknownHostException) {
232231
logger.error("No internet connection");
233-
io.println("You have no internet connection.");
232+
io.errorln("You have no internet connection.");
234233
return;
235234
}
236235
logger.error("Command failed in tmc-core", exception);
237-
io.println("Command failed in tmc-core, check tmc-cli.log file for more info");
236+
//TODO we seem to write twice error message; here and in the commands.
237+
io.errorln("Command failed in tmc-core, check tmc-cli.log file for more info");
238238
}
239239

240240
private static boolean isAuthenticationError(Exception exception) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ public void run(CliContext context, CommandLine args) {
9393
try {
9494
this.height = Integer.parseInt(args.getOptionValue('h'));
9595
} catch (NumberFormatException e) {
96-
io.println("Height must be integer");
96+
io.errorln("Height must be integer");
9797
return;
9898
}
9999
}
100100
if (args.hasOption('s')) {
101101
try {
102102
this.speed = Integer.parseInt(args.getOptionValue('s'));
103103
} catch (NumberFormatException e) {
104-
io.println("Speed must be integer");
104+
io.errorln("Speed must be integer");
105105
return;
106106
}
107107
}
108108
if (EnvironmentUtil.isWindows()) {
109-
io.println("Command document doesn't exist. ;)");
109+
io.errorln("Command document doesn't exist. ;)");
110110
return;
111111
}
112112

@@ -118,7 +118,7 @@ public void run(CliContext context, CommandLine args) {
118118
if (cursorY == height / 2) {
119119
io.println("Just kidding :)");
120120
}
121-
io.println("");
121+
io.println();
122122
wait(50);
123123
}
124124
fadeOut();
@@ -238,7 +238,7 @@ private String loadFile(String filename) {
238238
try {
239239
return IOUtils.toString(stream, StandardCharsets.UTF_8);
240240
} catch (IOException e) {
241-
io.println("Encoding failure... REALLY???");
241+
io.errorln("Encoding failure... REALLY???");
242242
return null;
243243
}
244244
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public class DownloadExercisesCommand extends AbstractCommand {
2727
private CliContext ctx;
2828
private boolean showAll;
2929

30+
@Override
31+
public String[] getUsages() {
32+
return new String[] {"[-a] COURSE"};
33+
}
34+
3035
@Override
3136
public void getOptions(Options options) {
3237
options.addOption(
@@ -46,8 +51,8 @@ public void run(CliContext context, CommandLine args) {
4651

4752
String[] stringArgs = args.getArgs();
4853
if (stringArgs.length == 0 || stringArgs.length > 1) {
49-
io.println("You must give a course name as an argument.");
50-
io.println("Usage: tmc download COURSE");
54+
io.errorln("You must give a course name as an argument.");
55+
printUsage(ctx);
5156
return;
5257
}
5358

@@ -60,7 +65,7 @@ public void run(CliContext context, CommandLine args) {
6065

6166
WorkDir workDir = ctx.getWorkDir();
6267
if (workDir.getConfigFile() != null) {
63-
io.println("Can't download a course inside a course directory.");
68+
io.errorln("Can't download a course inside a course directory.");
6469
return;
6570
}
6671

@@ -81,7 +86,7 @@ public void run(CliContext context, CommandLine args) {
8186
ctx.useAccount(finder.getAccount());
8287
List<Exercise> exercises = TmcUtil.downloadExercises(ctx, filtered, progobs);
8388
if (exercises == null) {
84-
io.println("Failed to download exercises");
89+
io.errorln("Failed to download exercises");
8590
return;
8691
}
8792

0 commit comments

Comments
 (0)