Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
</goals>
<configuration>
<annotationProcessors>
<arg>fi.helsinki.cs.tmc.cli.command.core.CommandAnnotationProcessor</arg>
<arg>fi.helsinki.cs.tmc.cli.core.CommandAnnotationProcessor</arg>
</annotationProcessors>
<fork>true</fork> <!-- bug; non-forked compiler has basedir set to user.dir or sth -->
<excludes>
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/fi/helsinki/cs/tmc/cli/Application.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package fi.helsinki.cs.tmc.cli;

import fi.helsinki.cs.tmc.cli.command.core.AbstractCommand;
import fi.helsinki.cs.tmc.cli.command.core.CommandFactory;
import fi.helsinki.cs.tmc.cli.core.AbstractCommand;
import fi.helsinki.cs.tmc.cli.core.CliContext;
import fi.helsinki.cs.tmc.cli.core.CommandFactory;
import fi.helsinki.cs.tmc.cli.io.Color;
import fi.helsinki.cs.tmc.cli.io.ColorUtil;
import fi.helsinki.cs.tmc.cli.io.EnvironmentUtil;
import fi.helsinki.cs.tmc.cli.io.HelpGenerator;
import fi.helsinki.cs.tmc.cli.io.Io;
import fi.helsinki.cs.tmc.cli.io.ShutdownHandler;
import fi.helsinki.cs.tmc.cli.updater.TmcCliUpdater;
import fi.helsinki.cs.tmc.cli.updater.AutoUpdater;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
Expand Down Expand Up @@ -58,13 +60,13 @@ public Application(CliContext context) {
}

private boolean runCommand(String name, String[] args) {
AbstractCommand command = CommandFactory.createCommand(this.context, name);
AbstractCommand command = CommandFactory.createCommand(name);
if (command == null) {
io.println("Command " + name + " doesn't exist.");
return false;
}

command.execute(args, io);
command.execute(context, args);
return true;
}

Expand Down Expand Up @@ -169,7 +171,7 @@ private boolean versionCheck() {
public boolean runAutoUpdate() {
Map<String, String> properties = context.getProperties();
Date now = new Date();
TmcCliUpdater update = TmcCliUpdater.createUpdater(io,
AutoUpdater update = AutoUpdater.createUpdater(io,
EnvironmentUtil.getVersion(), EnvironmentUtil.isWindows());
boolean updated = update.run();

Expand All @@ -181,16 +183,16 @@ public boolean runAutoUpdate() {
}

//TODO rename this as getColorProperty and move it somewhere else
public Color.AnsiColor getColor(String propertyName) {
public Color getColor(String propertyName) {
String propertyValue = context.getProperties().get(propertyName);
Color.AnsiColor color = Color.getColor(propertyValue);
Color color = ColorUtil.getColor(propertyValue);
if (color == null) {
switch (propertyName) {
case "progressbar-left": return Color.AnsiColor.ANSI_CYAN;
case "progressbar-right": return Color.AnsiColor.ANSI_CYAN;
case "testresults-left": return Color.AnsiColor.ANSI_GREEN;
case "testresults-right": return Color.AnsiColor.ANSI_RED;
default: return Color.AnsiColor.ANSI_NONE;
case "progressbar-left": return Color.CYAN;
case "progressbar-right": return Color.CYAN;
case "testresults-left": return Color.GREEN;
case "testresults-right": return Color.RED;
default: return Color.NONE;
}
}
return color;
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/fi/helsinki/cs/tmc/cli/backend/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fi.helsinki.cs.tmc.cli.backend;

/**
* This object stores all login info.
*/
public class Account {

static Account NULL_ACCOUNT = new Account(null, null, null);

private String serverAddress;
private String username;
private String password;

// for gson
public Account() { }

public Account(String serverAddress, String username, String password) {
this.serverAddress = serverAddress;
this.username = username;
this.password = password;
}

public String getServerAddress() {
return serverAddress;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

private static boolean stringEquals(String str1, String str2) {
return (str1 == null ? str2 == null : str1.equals(str2));
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Account)) {
return false;
}
Account another = (Account) obj;
return stringEquals(this.serverAddress, another.serverAddress)
&& stringEquals(this.username, another.username)
&& stringEquals(this.password, another.password);
}
}
86 changes: 86 additions & 0 deletions src/main/java/fi/helsinki/cs/tmc/cli/backend/AccountList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package fi.helsinki.cs.tmc.cli.backend;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
* This is a class for storing all different accounts as a single array.
*/
public class AccountList implements Iterable<Account> {

private List<Account> accountArray;

public AccountList() {
this.accountArray = new ArrayList<>();
}

//TODO This should not be used
public Account getAccount() {
if (this.accountArray.size() > 0) {
// Get last used account by default
return this.accountArray.get(0);
}
return null;
}

public Account getAccount(String username, String server) {
if (username == null || server == null) {
return getAccount();
}
for (Account account : this.accountArray) {
if (account.getUsername().equals(username)
&& account.getServerAddress().equals(server)) {
// Move account to index 0 so we can always use the last used account by default
this.accountArray.remove(account);
this.accountArray.add(0, account);
return account;
}
}
return null;
}

public void addAccount(Account newSettings) {
for (Account account : this.accountArray) {
if (account.getUsername().equals(newSettings.getUsername())
&& account.getServerAddress().equals(newSettings.getServerAddress())) {
// Replace old account if username and server match
this.accountArray.remove(account);
break;
}
}
this.accountArray.add(0, newSettings);
}

public void deleteAccount(String username, String server) {
Account remove = null;
for (Account account : this.accountArray) {
if (account.getUsername().equals(username)
&& account.getServerAddress().equals(server)) {
remove = account;
break;
}
}
if (remove != null) {
this.accountArray.remove(remove);
}
}

public void deleteAllAccounts() {
this.accountArray = new ArrayList<>();
}

public int getAccountCount() {
return this.accountArray.size();
}

public List<Account> getAccountList() {
return Collections.unmodifiableList(this.accountArray);
}

@Override
public Iterator<Account> iterator() {
return getAccountList().iterator();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fi.helsinki.cs.tmc.cli.tmcstuff;
package fi.helsinki.cs.tmc.cli.backend;

import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.Exercise;

Expand All @@ -19,12 +18,12 @@ public class CourseInfo {
private List<String> localCompletedExercises;
private HashMap<String, String> properties;

public CourseInfo(TmcSettings settings, Course course) {
this.username = settings.getUsername();
this.serverAddress = settings.getServerAddress();
public CourseInfo(Account account, Course course) {
this.username = account.getUsername();
this.serverAddress = account.getServerAddress();
this.course = course;
this.properties = new HashMap<>();
this.localCompletedExercises = new ArrayList<String>();
this.localCompletedExercises = new ArrayList<>();
}

public String getUsername() {
Expand All @@ -47,7 +46,7 @@ public List<String> getLocalCompletedExercises() {
// Check for null pointer in case of old .tmc.json files
// Remove this when we are sure nobody's using 0.5.1 anymore
if (this.localCompletedExercises == null) {
this.localCompletedExercises = new ArrayList<String>();
this.localCompletedExercises = new ArrayList<>();
}
return this.localCompletedExercises;
}
Expand Down Expand Up @@ -78,6 +77,7 @@ public List<String> getExerciseNames() {
return names;
}

//TODO This is exactly same method as TmcUtil.findExercise(course, name)
public Exercise getExercise(String name) {
for (Exercise exercise : this.course.getExercises()) {
if (exercise.getName().equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package fi.helsinki.cs.tmc.cli.tmcstuff;
package fi.helsinki.cs.tmc.cli.backend;

import fi.helsinki.cs.tmc.core.domain.Course;

import com.google.gson.Gson;
import org.slf4j.Logger;
Expand Down Expand Up @@ -50,4 +52,14 @@ public static CourseInfo load(Path courseInfoFile) {
}
return gson.fromJson(reader, CourseInfo.class);
}

public static void createNewCourse(Course course, Account account, Path parentDir) {
Path configFile = parentDir
.resolve(course.getName())
.resolve(CourseInfoIo.COURSE_CONFIG);

CourseInfo info = new CourseInfo(account, course);
info.setExercises(course.getExercises());
CourseInfoIo.save(info, configFile);
}
}
Loading