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

Commit d1b5a5e

Browse files
author
Aleksi Salmela
committed
Refactor the paste command.
1 parent b552046 commit d1b5a5e

2 files changed

Lines changed: 57 additions & 28 deletions

File tree

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@
2424
public class PasteCommand extends AbstractCommand {
2525
private static final Logger logger = LoggerFactory.getLogger(SubmitCommand.class);
2626

27+
private CliContext ctx;
2728
private Io io;
2829

30+
String message;
31+
boolean noMessage;
32+
boolean openInBrowser;
33+
34+
@Override
35+
public String[] getUsages() {
36+
return new String[] {"tmc paste [-o] [-n] [-m MESSAGE] [EXERCISE]"};
37+
}
38+
2939
@Override
3040
public void getOptions(Options options) {
3141
options.addOption("n", "no-message", false, "Don't send a message with your paste");
@@ -35,40 +45,52 @@ public void getOptions(Options options) {
3545

3646
@Override
3747
public void run(CliContext context, CommandLine args) {
38-
CliContext ctx = context;
48+
this.ctx = context;
3949
this.io = ctx.getIo();
40-
if (!ctx.loadBackend()) {
41-
return;
42-
}
50+
51+
this.message = args.getOptionValue("m");
52+
this.noMessage = args.hasOption("n");
53+
this.openInBrowser = args.hasOption("o");
4354
WorkDir workdir = ctx.getWorkDir();
4455
String[] stringArgs = args.getArgs();
4556

46-
Boolean valid;
47-
if (stringArgs.length == 0) {
48-
valid = workdir.addPath();
49-
} else if (stringArgs.length == 1) {
50-
valid = workdir.addPath(stringArgs[0]);
51-
} else {
52-
io.errorln("Error: Too many arguments. Expected 1, got " + stringArgs.length);
57+
if (noMessage && message != null) {
58+
io.errorln("You can't have the no-message flag and message set at the same time.");
59+
printUsage(context);
60+
return;
61+
}
62+
if (stringArgs.length > 1) {
63+
io.errorln("Error: Too many arguments.");
64+
printUsage(context);
5365
return;
5466
}
55-
if (!valid) {
56-
io.errorln("The command can be used in an exercise directory without the exercise name"
57-
+ " or in a course directory with the name as an argument.");
67+
68+
if (stringArgs.length == 1) {
69+
if (!workdir.addPath(stringArgs[0])) {
70+
io.errorln("The path '" + stringArgs[0] + "' is not valid exercise.");
71+
return;
72+
}
73+
} else {
74+
//TODO replace the following call with workdir.getCurrentExercise()
75+
if (!workdir.addPath()) {
76+
io.errorln("You are not in exercise directory.");
77+
return;
78+
}
79+
}
80+
81+
if (!ctx.loadBackend()) {
5882
return;
5983
}
6084

61-
List<String> exercisenames = workdir.getExerciseNames();
62-
if (exercisenames.size() != 1) {
85+
List<String> exerciseNames = workdir.getExerciseNames();
86+
if (exerciseNames.size() != 1) {
6387
io.errorln("Error: Matched too many exercises.");
88+
printUsage(context);
6489
return;
6590
}
6691

67-
String message = "";
68-
if (!args.hasOption("n")) {
69-
if (args.hasOption("m")) {
70-
message = args.getOptionValue("m");
71-
} else if (io.readConfirmation("Attach a message to your paste?", true)) {
92+
if (!noMessage && message == null) {
93+
if (io.readConfirmation("Attach a message to your paste?", true)) {
7294
message = ExternalsUtil.getUserEditedMessage(
7395
"\n"
7496
+ "# Write a message for your paste in this file and save it.\n"
@@ -79,10 +101,17 @@ public void run(CliContext context, CommandLine args) {
79101
true);
80102
}
81103
}
104+
sendPaste(message, exerciseNames.get(0));
105+
}
106+
107+
private void sendPaste(String message, String exerciseName) {
108+
if (message == null) {
109+
message = "";
110+
}
82111

83-
String exerciseName = exercisenames.get(0);
84112
CourseInfo info = ctx.getCourseInfo();
85113
Exercise exercise = info.getExercise(exerciseName);
114+
86115
URI uri = TmcUtil.sendPaste(ctx, exercise, message);
87116
if (uri == null && exercise.hasDeadlinePassed()) {
88117
io.errorln("Unable to send the paste."
@@ -96,7 +125,7 @@ public void run(CliContext context, CommandLine args) {
96125

97126
io.println("Paste sent for exercise " + exercise.getName());
98127
io.println(uri.toString());
99-
if (args.hasOption("o")) {
128+
if (openInBrowser) {
100129
ExternalsUtil.openInBrowser(uri);
101130
}
102131
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public void run(CliContext context, CommandLine args) {
4444
CliContext ctx = context;
4545
Io io = ctx.getIo();
4646

47-
String[] exercisesFromArgs = parseArgs(args);
48-
if (exercisesFromArgs == null) {
47+
String[] paths = parseArgs(args);
48+
if (paths == null) {
4949
return;
5050
}
5151

@@ -54,9 +54,9 @@ public void run(CliContext context, CommandLine args) {
5454
}
5555

5656
WorkDir workDir = ctx.getWorkDir();
57-
for (String exercise : exercisesFromArgs) {
58-
if (!workDir.addPath(exercise)) {
59-
io.errorln("The argument \"" + exercise + "\" is not a valid exercise.");
57+
for (String path : paths) {
58+
if (!workDir.addPath(path)) {
59+
io.errorln("The path \"" + path + "\" is not a valid exercise.");
6060
return;
6161
}
6262
}

0 commit comments

Comments
 (0)