|
26 | 26 | public class CourseInfoCommand extends AbstractCommand { |
27 | 27 | private Course course; |
28 | 28 | private Exercise exercise; |
| 29 | + private CourseInfo info; |
| 30 | + private WorkDir workDir; |
29 | 31 | private Io io; |
30 | 32 |
|
31 | 33 | @Override |
32 | 34 | public void getOptions(Options options) { |
33 | | - options.addOption("a", false, "Show all info for a specific course"); |
| 35 | + options.addOption("a", false, "Show all information for a specific course"); |
| 36 | + options.addOption("i", false, "Get the information from the server"); |
34 | 37 | } |
35 | 38 |
|
36 | 39 | @Override |
37 | 40 | public void run(CommandLine args, Io io) { |
38 | 41 | this.io = io; |
39 | | - TmcCore core = getApp().getTmcCore(); |
40 | | - if (core == null) { |
| 42 | + workDir = getApp().getWorkDir(); |
| 43 | + |
| 44 | + if (!args.hasOption("i")) { |
| 45 | + printLocalCourseOrExercise(args); |
41 | 46 | return; |
42 | 47 | } |
43 | 48 |
|
44 | | - WorkDir workDir = getApp().getWorkDir(); |
45 | 49 | String[] stringArgs = args.getArgs(); |
46 | | - if (stringArgs.length == 0 && workDir.getConfigFile() == null) { |
47 | | - io.println("You must give the course name as a parameter."); |
| 50 | + if (stringArgs.length == 0) { |
| 51 | + io.println("You must give a course as a parameter."); |
48 | 52 | return; |
49 | 53 | } |
50 | 54 |
|
51 | | - // if in course directory |
| 55 | + TmcCore core = getApp().getTmcCore(); |
| 56 | + if (core == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + course = TmcUtil.findCourse(core, stringArgs[0]); |
| 60 | + if (course == null) { |
| 61 | + io.println("The course " + stringArgs[0] + " doesn't exist on this server."); |
| 62 | + return; |
| 63 | + } |
| 64 | + printCourse(args.hasOption("a")); |
| 65 | + } |
| 66 | + |
| 67 | + private void printLocalCourseOrExercise(CommandLine args) { |
52 | 68 | if (workDir.getConfigFile() != null) { |
53 | | - CourseInfo info = CourseInfoIo.load(workDir.getConfigFile()); |
| 69 | + info = CourseInfoIo.load(workDir.getConfigFile()); |
54 | 70 | course = info.getCourse(); |
| 71 | + printCourseOrExercise(args); |
| 72 | + } else { |
| 73 | + this.io.println("You have to be in a course directory" |
| 74 | + + " or use the -i option with the course name " |
| 75 | + + "to get the information from the server."); |
| 76 | + } |
| 77 | + } |
55 | 78 |
|
56 | | - // if in exercise directory and no parameters given, print info for that exercise. |
57 | | - // else if exercise or course name given as a parameter, check which one it is and print info for that |
58 | | - if (workDir.getExerciseNames().size() == 1 && stringArgs.length == 0) { |
59 | | - String currentExercise = workDir.getExerciseNames().get(0); |
60 | | - exercise = info.getExercise(currentExercise); |
61 | | - printOneExercise(args.hasOption("a")); |
62 | | - return; |
63 | | - |
64 | | - } else if (stringArgs.length != 0) { |
65 | | - if (info.getExercise(stringArgs[0]) != null) { |
66 | | - exercise = info.getExercise(stringArgs[0]); |
67 | | - printOneExercise(args.hasOption("a")); |
68 | | - return; |
69 | | - |
70 | | - } else { |
71 | | - course = TmcUtil.findCourse(core, stringArgs[0]); |
72 | | - if (course != null) { |
73 | | - printCourse(args.hasOption("a")); |
74 | | - } else { |
75 | | - io.println("No such course or exercise."); |
76 | | - } |
77 | | - return; |
78 | | - } |
79 | | - } |
80 | | - printCourse(args.hasOption("a")); |
| 79 | + private void printCourseOrExercise(CommandLine args) { |
| 80 | + String[] stringArgs = args.getArgs(); |
| 81 | + |
| 82 | + // if in exercise directory and no parameters given, print info for that exercise. |
| 83 | + if (workDir.getExerciseNames().size() == 1 && stringArgs.length == 0) { |
| 84 | + String currentExercise = workDir.getExerciseNames().get(0); |
| 85 | + exercise = info.getExercise(currentExercise); |
| 86 | + printOneExercise(args.hasOption("a")); |
81 | 87 | return; |
82 | 88 | } |
83 | 89 |
|
84 | | - course = TmcUtil.findCourse(core, stringArgs[0]); |
85 | | - if (course == null) { |
86 | | - io.println("The course " + stringArgs[0] + " doesn't exist on this server."); |
| 90 | + if (stringArgs.length != 0) { |
| 91 | + printCourseOrExerciseFromParameters(args); |
87 | 92 | return; |
88 | 93 | } |
89 | 94 | printCourse(args.hasOption("a")); |
90 | 95 | } |
91 | 96 |
|
| 97 | + private void printCourseOrExerciseFromParameters(CommandLine args) { |
| 98 | + String[] stringArgs = args.getArgs(); |
| 99 | + // if parameter is given, check if it is an exercise or a course. If neither, print an error message. |
| 100 | + if (info.getExercise(stringArgs[0]) != null) { |
| 101 | + exercise = info.getExercise(stringArgs[0]); |
| 102 | + printOneExercise(args.hasOption("a")); |
| 103 | + return; |
| 104 | + |
| 105 | + } |
| 106 | + course = info.getCourse(); |
| 107 | + if (course != null && course.getName().equals(stringArgs[0])) { |
| 108 | + printCourse(args.hasOption("a")); |
| 109 | + } else { |
| 110 | + this.io.println("Wrong course directory." |
| 111 | + + " Navigate to the correct course directory or" |
| 112 | + + " use the -i option with the course name" |
| 113 | + + " to get the information from the server."); |
| 114 | + } |
| 115 | + } |
| 116 | + |
92 | 117 | private void printCourse(boolean showAll) { |
93 | 118 | printCourseShort(); |
94 | 119 | if (showAll) { |
|
0 commit comments