@@ -22,6 +22,8 @@ public class CliContext {
2222 private Application application ;
2323 private TmcCore tmcCore ;
2424 private Settings settings ;
25+ private boolean hasLogin ;
26+ private CourseInfo courseInfo ;
2527
2628 private HashMap <String , String > properties ;
2729 private final boolean inTest ;
@@ -43,7 +45,7 @@ public CliContext(Io io, WorkDir workDir) {
4345 public CliContext (Io io , WorkDir workDir , TmcCore core ) {
4446 inTest = (io != null );
4547 if (!inTest ) {
46- io = new TerminalIo ();
48+ io = new TerminalIo (System . in );
4749 }
4850 // This is only used when we want to mock the tmc core.
4951 if (core != null ) {
@@ -54,6 +56,8 @@ public CliContext(Io io, WorkDir workDir, TmcCore core) {
5456 this .workDir = workDir ;
5557 this .properties = SettingsIo .loadProperties ();
5658 this .tmcCore = core ;
59+ this .hasLogin = false ;
60+ this .courseInfo = null ;
5761 }
5862
5963 protected void setApp (Application app ) {
@@ -68,6 +72,7 @@ public Application getApp() {
6872 }
6973
7074 //TODO get rid of this (use properties)
75+ @ Deprecated
7176 public boolean inTests () {
7277 return inTest ;
7378 }
@@ -118,7 +123,21 @@ public boolean loadBackend(boolean useInternet) {
118123 return true ;
119124 }
120125
121- return createTmcCore ();
126+ if (!createTmcCore ()) {
127+ return false ;
128+ }
129+
130+ if (useInternet && !hasLogin ) {
131+ // If no settings are present
132+ if (courseInfo == null ) {
133+ io .println ("You are not logged in. Log in using: tmc login" );
134+ } else {
135+ io .println ("You are not logged in as " + courseInfo .getUsername ()
136+ + ". Log in using: tmc login" );
137+ }
138+ return false ;
139+ }
140+ return true ;
122141 }
123142
124143 public TmcCore getTmcCore () {
@@ -149,32 +168,31 @@ private void createTmcCore(Settings settings) {
149168 }
150169
151170 private boolean createTmcCore () {
152- SettingsIo settingsio = new SettingsIo ();
153171 Settings settings ;
154172
155173 if (workDir .getConfigFile () != null ) {
156174 // If we're in a course directory, we load settings matching the course
157175 // Otherwise we just load the last used settings
158- CourseInfo courseinfo = CourseInfoIo .load (workDir .getConfigFile ());
159- if (courseinfo == null ) {
160- //io.println("Course configuration file "
161- // + workDir.getConfigFile().toString()
162- // + "is invalid.");
163- //return false;
164- createTmcCore (new Settings ());
165- return true ;
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 ;
166183 }
167- settings = settingsio .load (courseinfo .getUsername (),
168- courseinfo .getServerAddress ());
184+ settings = SettingsIo .load (courseInfo .getUsername (),
185+ courseInfo .getServerAddress ());
169186 } else {
170- settings = settingsio .load ();
187+ settings = SettingsIo .load ();
171188 }
172189
190+ hasLogin = true ;
173191 if (settings == null ) {
174- // If no settings are present
175- io .println ("You are not logged in. Log in using: tmc login" );
176- return false ;
192+ hasLogin = false ;
193+ settings = new Settings ();
177194 }
195+
178196 createTmcCore (settings );
179197 return true ;
180198 }
0 commit comments