77import fi .helsinki .cs .tmc .cli .io .HelpGenerator ;
88import fi .helsinki .cs .tmc .cli .io .Io ;
99import fi .helsinki .cs .tmc .cli .io .ShutdownHandler ;
10- import fi .helsinki .cs .tmc .cli .io .TerminalIo ;
11- import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfo ;
12- import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfoIo ;
13- import fi .helsinki .cs .tmc .cli .tmcstuff .Settings ;
14- import fi .helsinki .cs .tmc .cli .tmcstuff .SettingsIo ;
1510import fi .helsinki .cs .tmc .cli .tmcstuff .WorkDir ;
1611import fi .helsinki .cs .tmc .cli .updater .TmcCliUpdater ;
1712
18- import fi .helsinki .cs .tmc .core .TmcCore ;
19- import fi .helsinki .cs .tmc .core .domain .Course ;
20- import fi .helsinki .cs .tmc .langs .util .TaskExecutor ;
21- import fi .helsinki .cs .tmc .langs .util .TaskExecutorImpl ;
22-
2313import org .apache .commons .cli .CommandLine ;
2414import org .apache .commons .cli .GnuParser ;
2515import org .apache .commons .cli .Options ;
2616import org .apache .commons .cli .ParseException ;
2717import org .slf4j .Logger ;
2818import org .slf4j .LoggerFactory ;
2919
30- import java .nio .file .Path ;
31- import java .nio .file .Paths ;
3220import java .util .ArrayList ;
3321import java .util .Arrays ;
3422import java .util .Date ;
35- import java .util .HashMap ;
3623import java .util .List ;
24+ import java .util .Map ;
3725
3826/**
3927 * The application class for the program.
@@ -44,45 +32,37 @@ public class Application {
4432 private static final long defaultUpdateInterval = 60 * 60 * 1000 ;
4533 private static final String usage = "tmc [args] COMMAND [command-args]" ;
4634
47- private HashMap <String , String > properties ;
48- private TmcCore tmcCore ;
49- private Settings settings ;
50- private WorkDir workDir ;
51- private Io io ;
52-
53- private boolean inTest ;
5435 private ShutdownHandler shutdownHandler ;
36+ private final CliContext context ;
37+ private final Io io ;
5538
56- private Options options ;
57- private GnuParser parser ;
39+ private final Options options ;
40+ private final GnuParser parser ;
5841 private String commandName ;
5942
60- public Application (Io io ) {
43+ public Application (CliContext context ) {
6144 this .parser = new GnuParser ();
6245 this .options = new Options ();
46+
47+ this .context = context ;
48+ this .io = context .getIo ();
49+
6350 options .addOption ("h" , "help" , false , "Display help information about tmc-cli" );
6451 options .addOption ("v" , "version" , false , "Give the version of the tmc-cli" );
6552
66- inTest = true ;
67- if (io == null ) {
68- inTest = false ;
69- io = new TerminalIo ();
70- shutdownHandler = new ShutdownHandler (io );
53+ //TODO implement the inTests as context.property
54+ if (!context .inTests ()) {
55+ shutdownHandler = new ShutdownHandler (context .getIo ());
7156 shutdownHandler .enable ();
7257 }
73-
74- this .io = io ;
75- this .workDir = new WorkDir ();
76- this .properties = SettingsIo .loadProperties ();
7758 }
7859
7960 public Application (Io io , WorkDir workDir ) {
80- this (io );
81- this .workDir = workDir ;
61+ this (new CliContext (io , workDir ));
8262 }
8363
8464 private boolean runCommand (String name , String [] args ) {
85- AbstractCommand command = CommandFactory .createCommand (this , name );
65+ AbstractCommand command = CommandFactory .createCommand (this . context , name );
8666 if (command == null ) {
8767 io .println ("Command " + name + " doesn't exist." );
8868 return false ;
@@ -135,7 +115,9 @@ public void printHelp(String description) {
135115 }
136116
137117 public void run (String [] args ) {
138- if (!inTest ) {
118+ context .setApp (this );
119+
120+ if (!context .inTests ()) {
139121 versionCheck ();
140122 }
141123
@@ -146,98 +128,18 @@ public void run(String[] args) {
146128
147129 runCommand (commandName , commandArgs );
148130
149- if (!inTest ) {
131+ if (!context . inTests () ) {
150132 shutdownHandler .disable ();
151133 }
152134 }
153135
154- public void createTmcCore (Settings settings ) {
155- TaskExecutor tmcLangs ;
156-
157- tmcLangs = new TaskExecutorImpl ();
158- this .settings = settings ;
159- this .tmcCore = new TmcCore (settings , tmcLangs );
160- /*XXX should we somehow check if the authentication is successful here */
161- Path path = getWorkDir ().getCourseDirectory ();
162- if (path == null ) {
163- settings .setTmcProjectDirectory (Paths .get (System .getProperty ("user.dir" )));
164- return ;
165- }
166- settings .setTmcProjectDirectory (path .getParent ());
167- }
168-
169- // Method is used to help testing
170- public void setTmcCore (TmcCore tmcCore ) {
171- this .tmcCore = tmcCore ;
172- }
173-
174- public TmcCore getTmcCore () {
175- if (this .tmcCore == null ) {
176- SettingsIo settingsio = new SettingsIo ();
177- Settings settings ;
178-
179- if (workDir .getConfigFile () != null ) {
180- // If we're in a course directory, we load settings matching the course
181- // Otherwise we just load the last used settings
182- CourseInfo courseinfo = CourseInfoIo .load (workDir .getConfigFile ());
183- if (courseinfo == null ) {
184- io .println ("Course configuration file "
185- + workDir .getConfigFile ().toString ()
186- + "is invalid." );
187- return null ;
188- }
189- settings = settingsio .load (courseinfo .getUsername (),
190- courseinfo .getServerAddress ());
191- } else {
192- settings = settingsio .load ();
193- }
194-
195- if (settings == null ) {
196- // If no settings are present
197- io .println ("You are not logged in. Log in using: tmc login" );
198- return null ;
199- }
200- createTmcCore (settings );
201- }
202- return this .tmcCore ;
203- }
204-
205- public void setSettings (Settings settings ) {
206- this .settings = settings ;
207- }
208-
209136 public static void main (String [] args ) {
210- Application app = new Application (null );
137+ Application app = new Application (new CliContext ( null ) );
211138 app .run (args );
212139 }
213140
214- public WorkDir getWorkDir () {
215- return this .workDir ;
216- }
217-
218- public void setWorkdir (WorkDir workDir ) {
219- this .workDir = workDir ;
220- }
221-
222- public void setTmcProjectDirectory (Path path ) {
223- this .settings .setTmcProjectDirectory (path );
224- }
225-
226- public HashMap <String , String > getProperties () {
227- // Loads properties from the global configuration file in .config/tmc-cli/
228- return this .properties ;
229- }
230-
231- public Boolean saveProperties () {
232- // Saves properties to the global configuration file in .config/tmc-cli/
233- return SettingsIo .saveProperties (properties );
234- }
235-
236- public CourseInfo createCourseInfo (Course course ) {
237- return new CourseInfo (settings , course );
238- }
239-
240141 private void versionCheck () {
142+ Map <String , String > properties = context .getProperties ();
241143 String previousTimestamp = properties .get (previousUpdateDateKey );
242144 Date previous = null ;
243145
@@ -264,11 +166,12 @@ private void versionCheck() {
264166
265167 long timestamp = now .getTime ();
266168 properties .put (previousUpdateDateKey , Long .toString (timestamp ));
267- saveProperties ();
169+ context . saveProperties ();
268170 }
269171
172+ //TODO rename this as getColorProperty
270173 public Color .AnsiColor getColor (String propertyName ) {
271- String propertyValue = this . properties .get (propertyName );
174+ String propertyValue = context . getProperties () .get (propertyName );
272175 Color .AnsiColor color = Color .getColor (propertyValue );
273176 if (color == null ) {
274177 switch (propertyName ) {
0 commit comments