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

Commit ec649e9

Browse files
author
Irene
committed
Remove server prompt and useless Settings methods
1 parent 2889b8a commit ec649e9

28 files changed

Lines changed: 207 additions & 151 deletions

pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@
182182
<artifactId>cobertura-maven-plugin</artifactId>
183183
<version>2.7</version>
184184
<configuration>
185-
<check>
186-
187-
</check>
188185
<formats>
189186
<format>xml</format>
190187
<format>html</format>

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
2222

23-
import java.util.*;
23+
import java.lang.management.ManagementFactory;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Set;
27+
import java.util.Scanner;
28+
import java.util.Date;
29+
import java.util.ArrayList;
30+
import java.util.Arrays;
2431

2532
/**
2633
* The application class for the program.
@@ -171,15 +178,16 @@ public void run(String[] args) {
171178
}
172179
}
173180

181+
174182
public static void main(String[] args) {
175-
while(true) {
183+
while (true) {
176184
Scanner reader = new Scanner(System.in);
177185
System.out.println("Enter command (stop to exit)");
178186
String command = reader.nextLine();
179187
if (command.equals("stop")) {
180188
break;
181189
} else {
182-
String[] argh = command.split((" "));
190+
String[] argh = command.split((" "));
183191
realMain(argh);
184192
}
185193
}

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,37 @@
1111
*/
1212
public class Account {
1313

14-
static Account NULL_ACCOUNT = new Account(null, null, null, null);
15-
1614
private String serverAddress;
1715
private String username;
1816
private String password;
1917
private OauthCredentials oauthCredentials;
2018
private Course currentCourse;
2119
private String token;
2220
private Organization organization;
21+
private boolean sendDiagnostics;
22+
private static final String DEFAULT_SERVER = "https://tmc.mooc.fi/staging";
2323

2424
// for gson
25-
public Account() {}
25+
public Account() {
26+
this.serverAddress = "";
27+
}
2628

27-
public Account(String serverAddress, String username, String password) {
28-
this.serverAddress = serverAddress == null ? null : serverAddress.trim();
29+
public Account(String username, String password) {
30+
this.serverAddress = DEFAULT_SERVER;
2931
this.username = username == null ? null : username.trim();
3032
this.password = password == null ? null : password.trim();
3133
}
3234

3335

34-
public Account(String serverAddress, String username, String password, Organization organization) {
35-
this.serverAddress = serverAddress == null ? null : serverAddress.trim();
36+
public Account(String username, String password, Organization organization) {
37+
this.serverAddress = DEFAULT_SERVER;
3638
this.username = username == null ? null : username.trim();
3739
this.password = password == null ? null : password.trim();
3840
this.organization = organization;
3941
}
4042

41-
public Optional<String> getServerAddress() {
42-
return Optional.fromNullable(serverAddress);
43+
public String getServerAddress() {
44+
return serverAddress;
4345
}
4446

4547
public Optional<String> getPassword() {
@@ -63,12 +65,12 @@ public boolean equals(Object obj) {
6365
return false;
6466
}
6567
Account another = (Account) obj;
66-
return stringEquals(this.serverAddress, another.serverAddress)
68+
return stringEquals(this.password, another.password)
6769
&& stringEquals(this.username, another.username);
6870
}
6971

7072
public void setServerAddress(String serverAddress) {
71-
this.serverAddress = serverAddress.trim();
73+
this.serverAddress = serverAddress;
7274
}
7375

7476
public Optional<OauthCredentials> getOauthCredentials() {
@@ -109,4 +111,12 @@ public Optional<Organization> getOrganization() {
109111
public void setOrganization(Optional<Organization> organization) {
110112
this.organization = organization.orNull();
111113
}
114+
115+
public void setSendDiagnostics(boolean value) {
116+
this.sendDiagnostics = value;
117+
}
118+
119+
public boolean getSendDiagnostics() {
120+
return this.sendDiagnostics;
121+
}
112122
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public Account getAccount(String username, String server) {
3030
return getAccount();
3131
}
3232
for (Account account : this.accountArray) {
33-
if (!account.getUsername().isPresent() || !account.getServerAddress().isPresent()) {
33+
if (!account.getUsername().isPresent()) {
3434
continue;
3535
}
3636
if (account.getUsername().get().equals(username)
37-
&& account.getServerAddress().get().equals(server)) {
37+
&& account.getServerAddress().equals(server)) {
3838
// Move account to index 0 so we can always use the last used account by default
3939
this.accountArray.remove(account);
4040
this.accountArray.add(0, account);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CourseInfo {
2020

2121
public CourseInfo(Account account, Course course) {
2222
this.username = account.getUsername().orNull();
23-
this.serverAddress = account.getServerAddress().orNull();
23+
this.serverAddress = account.getServerAddress();
2424
this.course = course;
2525
this.properties = new HashMap<>();
2626
this.localCompletedExercises = new ArrayList<>();

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class Settings implements TmcSettings {
2020
private Account account;
2121

2222
public Settings() {
23-
this.account = Account.NULL_ACCOUNT;
23+
this.account = new Account();
2424
}
2525

26-
public Settings(String serverAddress, String username, String password, Organization organization) {
27-
this.account = new Account(serverAddress, username, password, organization);
26+
public Settings(String username, String password, Organization organization) {
27+
this.account = new Account(username, password, organization);
2828
}
2929

3030
/**
@@ -33,10 +33,7 @@ public Settings(String serverAddress, String username, String password, Organiza
3333
*/
3434
public void setAccount(Account account) {
3535
if (account == null) {
36-
/* NULL_ACCOUNT is used so that the NullPointerException
37-
* is not thrown in the getters.
38-
*/
39-
account = Account.NULL_ACCOUNT;
36+
account = new Account();
4037
}
4138
this.account = account;
4239
}
@@ -51,10 +48,7 @@ public void setWorkDir(WorkDir workDir) {
5148

5249
@Override
5350
public String getServerAddress() {
54-
if (account.getServerAddress().isPresent()) {
55-
return account.getServerAddress().get();
56-
}
57-
return "https://tmc.mooc.fi";
51+
return account.getServerAddress();
5852
}
5953

6054
@Override
@@ -97,14 +91,6 @@ public String clientVersion() {
9791
return EnvironmentUtil.getVersion();
9892
}
9993

100-
@Override
101-
public String getFormattedUserData() {
102-
if (!userDataExists()) {
103-
return "";
104-
}
105-
return getUsername().get() + ":" + this.getPassword().get();
106-
}
107-
10894
@Override
10995
public Path getTmcProjectDirectory() {
11096
return workDir.getTmcDirectory();
@@ -128,18 +114,21 @@ public Path getConfigRoot() {
128114
@Override
129115
public String hostProgramName() {
130116
// which command line is used
131-
return null;
117+
return "unknown";
132118
}
133119

134120
@Override
135121
public String hostProgramVersion() {
136-
// which command line is used
137-
return null;
122+
return "unknown";
138123
}
139124

140125
@Override
141126
public boolean getSendDiagnostics() {
142-
return false;
127+
return account.getSendDiagnostics();
128+
}
129+
130+
public void setSendDiagnostics(boolean value) {
131+
account.setSendDiagnostics(value);
143132
}
144133

145134
@Override
@@ -176,8 +165,4 @@ public void setOrganization(Optional<Organization> organization) {
176165
public void setCourse(Course course) {
177166
account.setCurrentCourse(Optional.of(course));
178167
}
179-
180-
@Override
181-
public void setConfigRoot(Path path) {
182-
}
183168
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private static boolean requireConfigDirectory(Path configRoot) {
142142
try {
143143
Files.createDirectories(configRoot);
144144
} catch (Exception e) {
145+
//TODO print error to user
145146
logger.error("Could not create config directory", e);
146147
return false;
147148
}
@@ -155,6 +156,7 @@ private static AccountList getHolderFromJson(Path file) {
155156
try {
156157
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
157158
} catch (IOException e) {
159+
//TODO print error to user
158160
logger.error("Accounts file located, but failed to read from it", e);
159161
return null;
160162
}
@@ -167,6 +169,7 @@ private static boolean saveHolderToJson(AccountList holder, Path file) {
167169
try {
168170
Files.write(file, json);
169171
} catch (IOException e) {
172+
//TODO print error to user
170173
logger.error("Could not write account to accounts file", e);
171174
return false;
172175
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,6 @@ private static void handleTmcExceptions(CliContext ctx, Exception exception) {
270270
}
271271

272272
private static boolean isAuthenticationError(Exception exception) {
273-
Throwable cause = exception.getCause();
274-
if (cause instanceof OAuthProblemException) {
275-
return true;
276-
}
277-
return false;
273+
return exception.getCause() instanceof OAuthProblemException;
278274
}
279275
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public void run(CliContext context, CommandLine args) {
5757
if (!isFirst) {
5858
io.println();
5959
}
60-
if (accountsList.getAccountCount() > 1 && settings.getServerAddress().isPresent()) {
60+
if (accountsList.getAccountCount() > 1) {
6161
io.println(
6262
ColorUtil.colorString(
63-
"Server " + settings.getServerAddress().get(), Color.YELLOW));
63+
"Server " + settings.getServerAddress(), Color.YELLOW));
6464
}
6565

6666
printCourseList(settings);

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class LoginCommand extends AbstractCommand {
2525
private CliContext ctx;
2626
private Io io;
2727

28-
private String serverAddress;
2928
private String username;
3029
private String password;
3130

@@ -38,7 +37,6 @@ public String[] getUsages() {
3837
public void getOptions(Options options) {
3938
options.addOption("u", "user", true, "TMC username");
4039
options.addOption("p", "password", true, "Password for the user");
41-
options.addOption("s", "server", true, "Address for TMC server");
4240
options.addOption("o", "organization", true, "TMC organization");
4341
}
4442

@@ -65,16 +63,14 @@ public void run(CliContext context, CommandLine args) {
6563

6664
CourseInfo info = ctx.getCourseInfo();
6765
if (info != null) {
68-
serverAddress = info.getServerAddress();
6966
username = info.getUsername();
7067
}
7168

72-
serverAddress = getLoginInfo(args, serverAddress, "s", "server address: ");
7369
username = getLoginInfo(args, username, "u", "username: ");
7470
password = getLoginInfo(args, null, "p", "password: ");
7571

7672
OrganizationCommand organizationCommand = new OrganizationCommand();
77-
Account account = new Account(serverAddress, username, null);
73+
Account account = new Account(username, null);
7874
if (!TmcUtil.tryToLogin(ctx, account, password)) {
7975
return;
8076
}
@@ -83,9 +79,11 @@ public void run(CliContext context, CommandLine args) {
8379
if (!organization.isPresent()) {
8480
return;
8581
}
86-
Account currentAccount = this.ctx.getSettings().getAccount();
87-
currentAccount.setOrganization(organization);
88-
this.ctx.getSettings().setAccount(currentAccount);
82+
83+
this.ctx.getSettings().setOrganization(organization);
84+
85+
boolean sendDiagnostics = getSendDiagnosticsAnswer(username, account.getServerAddress());
86+
this.ctx.getSettings().setSendDiagnostics(sendDiagnostics);
8987

9088
AccountList list = SettingsIo.loadAccountList();
9189
list.addAccount(account);
@@ -116,4 +114,21 @@ private String getLoginInfo(CommandLine line, String oldValue, String option, St
116114
}
117115
return value;
118116
}
117+
118+
private boolean getSendDiagnosticsAnswer(String username, String server) {
119+
AccountList savedAccounts = SettingsIo.loadAccountList();
120+
if (savedAccounts.getAccount(username, server) != null) {
121+
// not the first time logging in, diagnostics not asked
122+
return this.ctx.getSettings().getSendDiagnostics();
123+
}
124+
while (true) {
125+
String sendDiagnostics = io.readLine("Do you want to send crash reports and diagnostics for client development? (y/n)");
126+
if (sendDiagnostics.trim().toLowerCase().startsWith("y")) {
127+
return true;
128+
} else if (sendDiagnostics.trim().toLowerCase().startsWith("n")) {
129+
return false;
130+
}
131+
io.println("Please answer y or n.");
132+
}
133+
}
119134
}

0 commit comments

Comments
 (0)