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

Commit 0853bb2

Browse files
author
Irene
committed
Add oauth support, allow aborting download
1 parent 3e90d52 commit 0853bb2

19 files changed

Lines changed: 250 additions & 75 deletions

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10-
<maven.compiler.source>1.7</maven.compiler.source>
11-
<maven.compiler.target>1.7</maven.compiler.target>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
1212
<powermock.version>1.5.1</powermock.version>
1313
</properties>
1414

@@ -36,7 +36,7 @@
3636
<dependency>
3737
<groupId>fi.helsinki.cs.tmc</groupId>
3838
<artifactId>core</artifactId>
39-
<version>0.9.1-SNAPSHOT</version>
39+
<version>0.10.0-SNAPSHOT</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>commons-cli</groupId>

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

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

23-
import java.util.ArrayList;
24-
import java.util.Arrays;
25-
import java.util.Date;
26-
import java.util.List;
27-
import java.util.Map;
28-
import java.util.Set;
23+
import java.util.*;
2924

3025
/**
3126
* The application class for the program.
@@ -80,7 +75,8 @@ public Application(CliContext context) {
8075
}
8176

8277
private boolean runCommand(String name, String[] args) {
83-
AbstractCommand command = CommandFactory.createCommand(name);
78+
String[] commandName = name.split(" ");
79+
AbstractCommand command = CommandFactory.createCommand(commandName[0]);
8480
if (command == null) {
8581
io.errorln("Command " + name + " doesn't exist.");
8682
return false;
@@ -176,6 +172,20 @@ public void run(String[] args) {
176172
}
177173

178174
public static void main(String[] args) {
175+
while(true) {
176+
Scanner reader = new Scanner(System.in);
177+
System.out.println("Enter command (stop to exit)");
178+
String command = reader.nextLine();
179+
if (command.equals("stop")) {
180+
break;
181+
} else {
182+
String[] argh = command.split((" "));
183+
realMain(argh);
184+
}
185+
}
186+
}
187+
188+
public static void realMain(String[] args) {
179189
Application app = new Application(new CliContext(null));
180190
app.run(args);
181191
}

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

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

3+
import com.google.common.base.Optional;
4+
import fi.helsinki.cs.tmc.core.communication.oauth2.Oauth;
5+
import fi.helsinki.cs.tmc.core.domain.Course;
6+
import fi.helsinki.cs.tmc.core.domain.OauthCredentials;
7+
import fi.helsinki.cs.tmc.core.domain.Organization;
8+
39
/**
410
* This object stores all login info.
511
*/
@@ -10,6 +16,10 @@ public class Account {
1016
private String serverAddress;
1117
private String username;
1218
private String password;
19+
private OauthCredentials oauthCredentials;
20+
private Course currentCourse;
21+
private String token;
22+
private Organization organization;
1323

1424
// for gson
1525
public Account() {}
@@ -18,18 +28,19 @@ public Account(String serverAddress, String username, String password) {
1828
this.serverAddress = serverAddress;
1929
this.username = username;
2030
this.password = password;
31+
this.organization = new Organization("Default", "lolled", "default", "lolled", false);
2132
}
2233

23-
public String getServerAddress() {
24-
return serverAddress;
34+
public Optional<String> getServerAddress() {
35+
return Optional.fromNullable(serverAddress);
2536
}
2637

27-
public String getPassword() {
28-
return password;
38+
public Optional<String> getPassword() {
39+
return Optional.fromNullable(password);
2940
}
3041

31-
public String getUsername() {
32-
return username;
42+
public Optional<String> getUsername() {
43+
return Optional.fromNullable(username);
3344
}
3445

3546
private static boolean stringEquals(String str1, String str2) {
@@ -46,7 +57,79 @@ public boolean equals(Object obj) {
4657
}
4758
Account another = (Account) obj;
4859
return stringEquals(this.serverAddress, another.serverAddress)
49-
&& stringEquals(this.username, another.username)
50-
&& stringEquals(this.password, another.password);
60+
&& stringEquals(this.username, another.username);
61+
}
62+
63+
public void setServerAddress(String serverAddress) {
64+
this.serverAddress = serverAddress;
65+
}
66+
67+
public Optional<OauthCredentials> getOauthCredentials() {
68+
return Optional.fromNullable(oauthCredentials);
69+
}
70+
71+
public void setOauthCredentials(Optional<OauthCredentials> oauthCredentials) {
72+
if (oauthCredentials.isPresent()) {
73+
this.oauthCredentials = oauthCredentials.get();
74+
} else {
75+
this.oauthCredentials = null;
76+
}
77+
}
78+
79+
public void setPassword(Optional<String> password) {
80+
if (password.isPresent()) {
81+
this.password = password.get();
82+
} else {
83+
this.password = null;
84+
}
85+
}
86+
87+
public Optional<String> getoAuthToken() {
88+
return Optional.of(this.token);
89+
}
90+
91+
public void setoAuthToken(Optional<String> token) {
92+
if (token.isPresent()) {
93+
this.token = token.get();
94+
} else {
95+
this.token = null;
96+
}
97+
}
98+
99+
public Optional<Course> getCurrentCourse() {
100+
return Optional.fromNullable(currentCourse);
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return "Account{" +
106+
"serverAddress='" + serverAddress + '\'' +
107+
", username='" + username + '\'' +
108+
", password='" + password + '\'' +
109+
", oauthCredentials='" + oauthCredentials + '\'' +
110+
", token='" + token + '\'' +
111+
", currentCourse='" + currentCourse + '\'' +
112+
'}';
113+
}
114+
115+
public void setCurrentCourse(Optional<Course> currentCourse) {
116+
if (currentCourse.isPresent()) {
117+
this.currentCourse = currentCourse.get();
118+
} else {
119+
this.currentCourse = null;
120+
}
121+
122+
}
123+
124+
public Optional<Organization> getOrganization() {
125+
return Optional.fromNullable(this.organization);
126+
}
127+
128+
public void setOrganization(Optional<Organization> organization) {
129+
if (organization.isPresent()) {
130+
this.organization = organization.get();
131+
} else {
132+
this.organization = null;
133+
}
51134
}
52135
}

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class CourseInfo {
1919
private HashMap<String, String> properties;
2020

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,17 @@ public static void createNewCourse(Course course, Account account, Path parentDi
6262
info.setExercises(course.getExercises());
6363
CourseInfoIo.save(info, configFile);
6464
}
65+
66+
public static void abortCreatingCourse(Course course, Path parentDir) {
67+
Path configFile = parentDir.resolve(course.getName()).resolve(CourseInfoIo.COURSE_CONFIG);
68+
delete(configFile);
69+
}
70+
71+
private static void delete(Path courseInfoFile) {
72+
try {
73+
Files.deleteIfExists(courseInfoFile);
74+
} catch (IOException e) {
75+
logger.error("Could not delete course file", e);
76+
}
77+
}
6578
}

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

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import fi.helsinki.cs.tmc.core.domain.Course;
88

99
import com.google.common.base.Optional;
10+
import fi.helsinki.cs.tmc.core.domain.OauthCredentials;
11+
import fi.helsinki.cs.tmc.core.domain.Organization;
1012
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
1113

1214
import java.nio.file.Path;
@@ -49,16 +51,29 @@ public void setWorkDir(WorkDir workDir) {
4951

5052
@Override
5153
public String getServerAddress() {
52-
return account.getServerAddress();
54+
if (account.getServerAddress().isPresent()) {
55+
return account.getServerAddress().get();
56+
}
57+
return "https://tmc.mooc.fi";
58+
}
59+
60+
@Override
61+
public void setServerAddress(String address) {
62+
account.setServerAddress(address);
5363
}
5464

5565
@Override
56-
public String getPassword() {
66+
public Optional<String> getPassword() {
5767
return account.getPassword();
5868
}
5969

6070
@Override
61-
public String getUsername() {
71+
public void setPassword(Optional<String> password) {
72+
account.setPassword(password);
73+
}
74+
75+
@Override
76+
public Optional<String> getUsername() {
6277
return account.getUsername();
6378
}
6479

@@ -69,12 +84,7 @@ public boolean userDataExists() {
6984

7085
@Override
7186
public Optional<Course> getCurrentCourse() {
72-
return Optional.absent();
73-
}
74-
75-
@Override
76-
public String apiVersion() {
77-
return "7";
87+
return account.getCurrentCourse();
7888
}
7989

8090
@Override
@@ -116,8 +126,58 @@ public Path getConfigRoot() {
116126
}
117127

118128
@Override
119-
public void setCourse(Course course) {}
129+
public String hostProgramName() {
130+
// which command line is used
131+
return null;
132+
}
133+
134+
@Override
135+
public String hostProgramVersion() {
136+
// which command line is used
137+
return null;
138+
}
120139

121140
@Override
122-
public void setConfigRoot(Path path) {}
141+
public boolean getSendDiagnostics() {
142+
return false;
143+
}
144+
145+
@Override
146+
public Optional<OauthCredentials> getOauthCredentials() {
147+
return account.getOauthCredentials();
148+
}
149+
150+
@Override
151+
public void setOauthCredentials(Optional<OauthCredentials> credentials) {
152+
account.setOauthCredentials(credentials);
153+
}
154+
155+
@Override
156+
public void setToken(Optional<String> token) {
157+
account.setoAuthToken(token);
158+
}
159+
160+
@Override
161+
public Optional<String> getToken() {
162+
return account.getoAuthToken();
163+
}
164+
165+
@Override
166+
public Optional<Organization> getOrganization() {
167+
return account.getOrganization();
168+
}
169+
170+
@Override
171+
public void setOrganization(Optional<Organization> organization) {
172+
account.setOrganization(organization);
173+
}
174+
175+
@Override
176+
public void setCourse(Course course) {
177+
account.setCurrentCourse(Optional.of(course));
178+
}
179+
180+
@Override
181+
public void setConfigRoot(Path path) {
182+
}
123183
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class SettingsIo {
3232

3333
// PROPERTIES_CONFIG is the _global_ configuration file containing
3434
// tmc-cli's configuration and data, such as when to update when the client
35-
// was last updated. Is located under CONFIG_DIR
35+
// was last updated. Is located under CONFIG_DIR
3636
public static final String PROPERTIES_CONFIG = "properties.json";
3737

3838
public static AccountList loadAccountList() {
@@ -142,7 +142,6 @@ private static boolean requireConfigDirectory(Path configRoot) {
142142
try {
143143
Files.createDirectories(configRoot);
144144
} catch (Exception e) {
145-
//TODO print error to user
146145
logger.error("Could not create config directory", e);
147146
return false;
148147
}
@@ -156,7 +155,6 @@ private static AccountList getHolderFromJson(Path file) {
156155
try {
157156
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
158157
} catch (IOException e) {
159-
//TODO print error to user
160158
logger.error("Accounts file located, but failed to read from it", e);
161159
return null;
162160
}
@@ -169,7 +167,6 @@ private static boolean saveHolderToJson(AccountList holder, Path file) {
169167
try {
170168
Files.write(file, json);
171169
} catch (IOException e) {
172-
//TODO print error to user
173170
logger.error("Could not write account to accounts file", e);
174171
return false;
175172
}

0 commit comments

Comments
 (0)