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

Commit ea97a4c

Browse files
authored
Merge pull request #338 from tmc-cli/cli-context-aleksi
Cli context bug fixes
2 parents 3aa5568 + a5c1638 commit ea97a4c

16 files changed

Lines changed: 84 additions & 85 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>fi.helsinki.cs.tmc.cli</groupId>
55
<artifactId>tmc-cli</artifactId>
6-
<version>0.6.1</version>
6+
<version>0.6.2</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ public Application(CliContext context) {
5757
}
5858
}
5959

60-
public Application(Io io, WorkDir workDir) {
61-
this(new CliContext(io, workDir));
62-
}
63-
6460
private boolean runCommand(String name, String[] args) {
6561
AbstractCommand command = CommandFactory.createCommand(this.context, name);
6662
if (command == null) {

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,22 @@ public class CliContext {
2222
private Application application;
2323
private TmcCore tmcCore;
2424
private Settings settings;
25+
26+
/* cached values */
2527
private boolean hasLogin;
2628
private CourseInfo courseInfo;
27-
2829
private HashMap<String, String> properties;
2930
private final boolean inTest;
3031

3132
/*TODO some of the constructors could be removed */
3233
public CliContext(Io io) {
33-
this(io, new WorkDir());
34+
this(io, new WorkDir(), null);
3435
}
3536

3637
public CliContext(Io io, TmcCore core) {
3738
this(io, new WorkDir(), core);
3839
}
3940

40-
@Deprecated
41-
public CliContext(Io io, WorkDir workDir) {
42-
this(io, workDir, null);
43-
}
44-
4541
public CliContext(Io io, WorkDir workDir, TmcCore core) {
4642
inTest = (io != null);
4743
if (!inTest) {
@@ -60,6 +56,11 @@ public CliContext(Io io, WorkDir workDir, TmcCore core) {
6056
this.courseInfo = null;
6157
}
6258

59+
/*TODO create reset method for removing all cached data that is called
60+
* when working directory is changed or by users demand also use it in
61+
* constructor.
62+
*/
63+
6364
protected void setApp(Application app) {
6465
this.application = app;
6566
}
@@ -85,16 +86,6 @@ public WorkDir getWorkDir() {
8586
return this.workDir;
8687
}
8788

88-
/**
89-
* Does some thing in old style.
90-
*
91-
* @deprecated use {@link fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir#setWorkdir(String)} instead.
92-
*/
93-
@Deprecated
94-
public void setWorkdir(WorkDir workDir) {
95-
this.workDir = workDir;
96-
}
97-
9889
public CourseInfo createCourseInfo(Course course) {
9990
return new CourseInfo(settings, course);
10091
}
@@ -109,6 +100,24 @@ public Boolean saveProperties() {
109100
return SettingsIo.saveProperties(properties);
110101
}
111102

103+
public CourseInfo getCourseInfo() {
104+
if (courseInfo != null) {
105+
return courseInfo;
106+
}
107+
if (workDir.getConfigFile() == null) {
108+
return null;
109+
}
110+
courseInfo = CourseInfoIo.load(workDir.getConfigFile());
111+
if (courseInfo == null) {
112+
io.println("Course configuration file "
113+
+ workDir.getConfigFile().toString()
114+
+ "is invalid.");
115+
//TODO add a way to rewrite the course config file.
116+
return null;
117+
}
118+
return this.courseInfo;
119+
}
120+
112121
// Method is used to help testing
113122
public void setTmcCore(TmcCore tmcCore) {
114123
this.tmcCore = tmcCore;
@@ -168,23 +177,18 @@ private void createTmcCore(Settings settings) {
168177
}
169178

170179
private boolean createTmcCore() {
171-
Settings settings;
180+
Settings cachedSettings = null;
172181

173182
if (workDir.getConfigFile() != null) {
174183
// If we're in a course directory, we load settings matching the course
175184
// Otherwise we just load the last used settings
176-
courseInfo = CourseInfoIo.load(workDir.getConfigFile());
177-
if (courseInfo == null) {
178-
io.println("Course configuration file "
179-
+ workDir.getConfigFile().toString()
180-
+ "is invalid.");
181-
//TODO add a way to rewrite the course config file.
182-
return false;
185+
courseInfo = getCourseInfo();
186+
if (courseInfo != null) {
187+
cachedSettings = SettingsIo.load(courseInfo.getUsername(),
188+
courseInfo.getServerAddress());
183189
}
184-
settings = SettingsIo.load(courseInfo.getUsername(),
185-
courseInfo.getServerAddress());
186190
} else {
187-
settings = SettingsIo.load();
191+
cachedSettings = SettingsIo.load();
188192
}
189193

190194
hasLogin = true;
@@ -193,7 +197,7 @@ private boolean createTmcCore() {
193197
settings = new Settings();
194198
}
195199

196-
createTmcCore(settings);
200+
createTmcCore(cachedSettings);
197201
return true;
198202
}
199203
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ public void run(CommandLine args, Io io) {
4545
this.ctx = getContext();
4646
workDir = ctx.getWorkDir();
4747

48-
if (!args.hasOption("i")) {
48+
boolean fetchFromInternet = args.hasOption("i");
49+
50+
if (! ctx.loadBackend()) {
51+
return;
52+
}
53+
54+
if (!fetchFromInternet) {
4955
printLocalCourseOrExercise(args);
5056
return;
5157
}
@@ -56,9 +62,6 @@ public void run(CommandLine args, Io io) {
5662
return;
5763
}
5864

59-
if (! ctx.loadBackend()) {
60-
return;
61-
}
6265
course = TmcUtil.findCourse(ctx, stringArgs[0]);
6366
if (course == null) {
6467
io.println("The course " + stringArgs[0] + " doesn't exist on this server.");
@@ -68,8 +71,8 @@ public void run(CommandLine args, Io io) {
6871
}
6972

7073
private void printLocalCourseOrExercise(CommandLine args) {
71-
if (workDir.getConfigFile() != null) {
72-
info = CourseInfoIo.load(workDir.getConfigFile());
74+
info = ctx.getCourseInfo();
75+
if (info != null) {
7376
course = info.getCourse();
7477
printCourseOrExercise(args);
7578
} else {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import fi.helsinki.cs.tmc.cli.io.Io;
66
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
77

8-
import fi.helsinki.cs.tmc.core.TmcCore;
98
import fi.helsinki.cs.tmc.core.domain.Course;
109

1110
import org.apache.commons.cli.CommandLine;

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void getOptions(Options options) {
3535
public void run(CommandLine args, Io io) {
3636
this.ctx = getContext();
3737
this.io = io;
38-
38+
3939
String courseName = getCourseName(args);
4040
if (courseName == null) {
4141
return;
@@ -77,10 +77,7 @@ private String getCourseNameFromCurrentDirectory() {
7777
private CourseInfo getCourseInfoFromCurrentDirectory() {
7878
WorkDir workDir = ctx.getWorkDir();
7979
workDir.addPath();
80-
if (workDir.getConfigFile() != null) {
81-
return CourseInfoIo.load(workDir.getConfigFile());
82-
}
83-
return null;
80+
return ctx.getCourseInfo();
8481
}
8582

8683
private List<Exercise> getExercisesFromServer(String courseName) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public void run(CommandLine args, Io io) {
8888
}
8989

9090
String exerciseName = exercisenames.get(0);
91-
CourseInfo courseinfo = CourseInfoIo.load(ctx.getWorkDir().getConfigFile());
92-
Exercise exercise = courseinfo.getExercise(exerciseName);
91+
CourseInfo info = ctx.getCourseInfo();
92+
Exercise exercise = info.getExercise(exerciseName);
9393
URI uri = TmcUtil.sendPaste(ctx, exercise, message);
9494
if (uri == null && exercise.hasDeadlinePassed()) {
9595
io.println("Unable to send the paste."

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void run(CommandLine args, Io io) {
6767
io.println("You have to be in a course directory to run tests");
6868
return;
6969
}
70-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
70+
CourseInfo info = ctx.getCourseInfo();
7171

7272
ResultPrinter resultPrinter
7373
= new ResultPrinter(io, this.showDetails, this.showPassed);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void run(CommandLine args, Io io) {
8888
return;
8989
}
9090

91-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
91+
CourseInfo info = ctx.getCourseInfo();
9292
Course currentCourse = info.getCourse();
9393
if (currentCourse == null) {
9494
return;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void run(CommandLine args, Io io) {
5353
return;
5454
}
5555

56-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
56+
CourseInfo info = ctx.getCourseInfo();
5757
updateExercises(info, workDir.getConfigFile());
5858
}
5959

0 commit comments

Comments
 (0)