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

Commit e5d52f0

Browse files
author
Irene
committed
Make small refactorings
1 parent 378af8d commit e5d52f0

10 files changed

Lines changed: 17 additions & 30 deletions

File tree

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,6 @@ public void run(String[] args) {
180180

181181

182182
public static void main(String[] args) {
183-
while (true) {
184-
Scanner reader = new Scanner(System.in);
185-
System.out.println("Enter command (stop to exit)");
186-
String command = reader.nextLine();
187-
if (command.equals("stop")) {
188-
break;
189-
} else {
190-
String[] argh = command.split((" "));
191-
realMain(argh);
192-
}
193-
}
194-
}
195-
196-
public static void realMain(String[] args) {
197183
Application app = new Application(new CliContext(null));
198184
app.run(args);
199185
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void createNewCourse(Course course, Account account, Path parentDi
6363
CourseInfoIo.save(info, configFile);
6464
}
6565

66-
public static void abortCreatingCourse(Course course, Path parentDir) {
66+
public static void deleteConfigDirectory(Course course, Path parentDir) {
6767
Path configFile = parentDir.resolve(course.getName()).resolve(CourseInfoIo.COURSE_CONFIG);
6868
delete(configFile);
6969
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void setWorkDir(WorkDir workDir) {
4848

4949
@Override
5050
public String getServerAddress() {
51-
return account.getServerAddress();
51+
return account.getServerAddress();
5252
}
5353

5454
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,15 @@ public static boolean delete() {
7373
public static void saveCurrentSettingsToAccountList(Settings settings) {
7474
AccountList list = loadAccountList();
7575
Set<Account> deletables = new HashSet<>();
76-
for (Iterator<Account> i = list.iterator(); i.hasNext(); ) {
77-
Account account = i.next();
76+
list.forEach(account -> {
7877
if (account.getUsername().equals(settings.getAccount().getUsername())) {
7978
if (!account.getUsername().isPresent()) {
8079
logger.error("Savable account doesn't exist");
8180
return;
8281
}
8382
deletables.add(account);
8483
}
85-
}
84+
});
8685
deletables.stream().forEach(d -> list.deleteAccount(d.getUsername().get(), d.getServerAddress()));
8786
list.addAccount(settings.getAccount());
8887
saveAccountList(list);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void configureAllowedKeys() {
4343
ALLOWED_KEYS.put("send-diagnostics", (key, value) -> {
4444
String newVal = (String) value;
4545
if (!newVal.trim().toLowerCase().equals("true") && !newVal.trim().toLowerCase().equals("false")) {
46-
throw new BadValueTypeException("Not a boolean value");
46+
throw new BadValueTypeException("Please write either true or false");
4747
}
4848
boolean send = Boolean.parseBoolean(newVal);
4949
context.getSettings().setSendDiagnostics(send);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void run(CliContext context, CommandLine args) {
8888
List<Exercise> exercises = TmcUtil.downloadExercises(ctx, filtered, progobs);
8989
if (exercises == null) {
9090
io.errorln("Failed to download exercises");
91-
CourseInfoIo.abortCreatingCourse(course, workDir.getWorkingDirectory());
91+
CourseInfoIo.deleteConfigDirectory(course, workDir.getWorkingDirectory());
9292
return;
9393
}
9494

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ public Optional<Organization> chooseOrganization(CliContext ctx, CommandLine arg
102102
String slug = getOrganizationFromUser(organizations, args, printOptions, oneLine);
103103
printOptions = false;
104104
organization = organizations.stream().filter(o -> o.getSlug().equals(slug.trim().toLowerCase())).findFirst();
105-
if (!organization.isPresent()) {
105+
if (organization.isPresent()) {
106+
break;
107+
} else {
106108
io.errorln("Slug doesn't match any organization.");
107109
if (oneLine) {
108110
oneLine = false;
109111
printOptions = true;
110112
}
111-
} else {
112-
break;
113113
}
114114
}
115115
if (organization.isPresent()) {

src/main/java/fi/helsinki/cs/tmc/cli/shared/CourseFinder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ private boolean handleMultipleMatchingCourses(Map<Account, Course> matches) {
8484
Account entryAccount = entrySet.getKey();
8585
Course entryCourse = entrySet.getValue();
8686

87+
if (!entryAccount.getUsername().isPresent()) {
88+
continue;
89+
}
90+
8791
if (io.readConfirmation(
8892
"Download course from "
8993
+ entryAccount.getServerAddress()

src/test/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIoTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fi.helsinki.cs.tmc.cli.backend;
22

3-
import fi.helsinki.cs.tmc.cli.io.WorkDir;
43
import fi.helsinki.cs.tmc.core.domain.Course;
54

65
import junit.framework.Assert;
@@ -9,7 +8,6 @@
98
import org.junit.Before;
109
import org.junit.Test;
1110

12-
import java.io.File;
1311
import java.io.IOException;
1412
import java.nio.file.Files;
1513
import java.nio.file.Path;
@@ -64,7 +62,7 @@ public void abortingCourseCreationWorks() {
6462
CourseInfo loadedInfo = CourseInfoIo.load(this.courseFile);
6563
Assert.assertNotNull(loadedInfo);
6664

67-
CourseInfoIo.abortCreatingCourse(new Course("test-course"), Paths.get(tempDir));
65+
CourseInfoIo.deleteConfigDirectory(new Course("test-course"), Paths.get(tempDir));
6866
Path courseJson = Paths.get(tempDir).resolve(".tmc.json");
6967
assertTrue(!Files.exists(courseJson));
7068
}

src/test/java/fi/helsinki/cs/tmc/cli/command/ConfigCommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public void httpsIsRequiredInBeginningInValidation() {
242242
public void sendDiagnosticsIsValidated() {
243243
io.addConfirmationPrompt(true);
244244
app.run(new String[] {"config", "send-diagnostics=lol"});
245-
io.assertContains("Not a boolean value");
245+
io.assertContains("Please write either true or false");
246246
}
247247

248248
@Test
@@ -270,14 +270,14 @@ public void ifSeveralPairsAndSomeInvalidConfiguresValidOnes() {
270270
io.addConfirmationPrompt(true);
271271
io.addConfirmationPrompt(true);
272272
app.run(new String[] {"config", "send-diagnostics=asdf", "server-address=https://mooc.fi"});
273-
io.assertContains("Not a boolean value");
273+
io.assertContains("Please write either true or false");
274274
assertEquals("https://mooc.fi", settings.getServerAddress());
275275
}
276276

277277
@Test
278278
public void ifSeveralPairsAndSomeInvalidValuesConfiguresValidOnesWithQuiet() {
279279
app.run(new String[] {"config", "-q", "send-diagnostics=asdf", "server-address=https://mooc.fi"});
280-
io.assertContains("Not a boolean value");
280+
io.assertContains("Please write either true or false");
281281
assertEquals("https://mooc.fi", settings.getServerAddress());
282282
}
283283

0 commit comments

Comments
 (0)