1616
1717public class CliContext {
1818
19- private WorkDir workDir ;//TODO make this final
19+ private final WorkDir workDir ;
2020 private final Io io ;
2121
2222 private Application application ;
@@ -29,16 +29,15 @@ public class CliContext {
2929 private HashMap <String , String > properties ;
3030 private final boolean inTest ;
3131
32- /*TODO some of the constructors could be removed */
3332 public CliContext (Io io ) {
34- this (io , new WorkDir (), null );
33+ this (io , null );
3534 }
3635
3736 public CliContext (Io io , TmcCore core ) {
38- this (io , new WorkDir (), core );
37+ this (io , core , new WorkDir ());
3938 }
4039
41- public CliContext (Io io , WorkDir workDir , TmcCore core ) {
40+ public CliContext (Io io , TmcCore core , WorkDir workDir ) {
4241 inTest = (io != null );
4342 if (!inTest ) {
4443 io = new TerminalIo (System .in );
@@ -52,22 +51,27 @@ public CliContext(Io io, WorkDir workDir, TmcCore core) {
5251 this .workDir = workDir ;
5352 this .properties = SettingsIo .loadProperties ();
5453 this .tmcCore = core ;
55- this .hasLogin = false ;
54+ this .hasLogin = ( core != null ) ;
5655 this .courseInfo = null ;
5756 }
5857
5958 /*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
59+ * when working directory is changed or by user's demand, also use it in
6160 * constructor.
6261 */
6362
6463 protected void setApp (Application app ) {
6564 this .application = app ;
6665 }
6766
67+ /**
68+ * Get singleton Application object.
69+ *
70+ * @return application object
71+ */
6872 public Application getApp () {
6973 if (application == null ) {
70- throw new RuntimeException ("Application isn't usually set in tests." );
74+ throw new RuntimeException ("Application isn't set in some tests." );
7175 }
7276 return application ;
7377 }
@@ -78,28 +82,63 @@ public boolean inTests() {
7882 return inTest ;
7983 }
8084
85+ /**
86+ * Get singleton Io object.
87+ *
88+ * @return io object
89+ */
8190 public Io getIo () {
8291 return io ;
8392 }
8493
94+ public boolean hasLogin () {
95+ return hasLogin ;
96+ }
97+
98+ /**
99+ * Get singleton WorkDir object.
100+ *
101+ * @return singleton work dir object
102+ */
85103 public WorkDir getWorkDir () {
86- return this . workDir ;
104+ return workDir ;
87105 }
88106
107+ /**
108+ * Create local course info file after download.
109+ *
110+ * @param course local copy of course object
111+ * @return cached course data object
112+ */
89113 public CourseInfo createCourseInfo (Course course ) {
90114 return new CourseInfo (settings , course );
91115 }
92116
117+ /**
118+ * Get map of the properties.
119+ *
120+ * @return the whole mutable map
121+ */
93122 public HashMap <String , String > getProperties () {
94123 // Loads properties from the global configuration file in .config/tmc-cli/
95124 return this .properties ;
96125 }
97126
98- public Boolean saveProperties () {
127+ /**
128+ * Save the properties map into the a file.
129+ *
130+ * @return true if success
131+ */
132+ public boolean saveProperties () {
99133 // Saves properties to the global configuration file in .config/tmc-cli/
100134 return SettingsIo .saveProperties (properties );
101135 }
102136
137+ /**
138+ * Lazy load the course info from course directory.
139+ *
140+ * @return local course info
141+ */
103142 public CourseInfo getCourseInfo () {
104143 if (courseInfo != null ) {
105144 return courseInfo ;
@@ -112,33 +151,43 @@ public CourseInfo getCourseInfo() {
112151 io .println ("Course configuration file "
113152 + workDir .getConfigFile ().toString ()
114153 + "is invalid." );
115- //TODO add a way to rewrite the course config file.
154+ //TODO add a way to rewrite the corrupted course config file.
116155 return null ;
117156 }
118157 return this .courseInfo ;
119158 }
120159
121- // Method is used to help testing
122- public void setTmcCore (TmcCore tmcCore ) {
123- this .tmcCore = tmcCore ;
160+ /**
161+ * Getter of TmcCore for TmcUtil class.
162+ * This method should never be used except in TmcUtil class.
163+ *
164+ * @return global tmcutil
165+ */
166+ public TmcCore getTmcCore () {
167+ if (this .tmcCore == null ) {
168+ throw new RuntimeException ("The loadBackend* method was NOT called" );
169+ }
170+ return this .tmcCore ;
124171 }
125172
173+ /**
174+ * Initialize the tmc-core and other cached info.
175+ * Use this method if you need i
176+ * @return true if success
177+ */
126178 public boolean loadBackend () {
127- return loadBackend (true );
128- }
129-
130- public boolean loadBackend (boolean useInternet ) {
131179 if (this .tmcCore != null ) {
132180 return true ;
133181 }
134182
135- if (!createTmcCore ()) {
183+ if (! createTmcCore ()) {
136184 return false ;
137185 }
138186
139- if ( useInternet && ! hasLogin ) {
140- // If no settings are present
187+ //Bug: what if we have wrong login?
188+ if (! hasLogin ) {
141189 if (courseInfo == null ) {
190+ // if user is not in course folder.
142191 io .println ("You are not logged in. Log in using: tmc login" );
143192 } else {
144193 io .println ("You are not logged in as " + courseInfo .getUsername ()
@@ -149,24 +198,31 @@ public boolean loadBackend(boolean useInternet) {
149198 return true ;
150199 }
151200
152- public TmcCore getTmcCore () {
153- if (this .tmcCore == null ) {
154- throw new RuntimeException ("The loadBackend method was NOT called" );
201+ /**
202+ * Initialize the cached data, but don't fail if there is not login.
203+ *
204+ * @return true if success
205+ */
206+ public boolean loadBackendWithoutLogin () {
207+ if (this .tmcCore != null ) {
208+ return true ;
155209 }
156- return this .tmcCore ;
210+
211+ return createTmcCore ();
157212 }
158213
214+ /**
215+ * Copy login info from different settings object and them.
216+ * TODO: separate settings object and login info.
217+ * @param settings login info
218+ */
159219 public void useSettings (Settings settings ) {
160220 if (this .tmcCore == null ) {
161221 createTmcCore (settings );
162222 }
163223 this .settings .set (settings );
164224 }
165225
166- public void restoreSettings () {
167- this .settings .set (settings );
168- }
169-
170226 private void createTmcCore (Settings settings ) {
171227 TaskExecutor tmcLangs ;
172228
@@ -188,6 +244,8 @@ private boolean createTmcCore() {
188244 courseInfo .getServerAddress ());
189245 }
190246 } else {
247+ // Bug: if we are not inside course directory
248+ // then we may not correctly guess the correct settings.
191249 cachedSettings = SettingsIo .load ();
192250 }
193251
0 commit comments