Skip to content

Commit

Permalink
Fixes #365 - Refactor DeleteCommand to use JobUtils class (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Oct 9, 2024
1 parent 74cead1 commit 6e73cfa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
6 changes: 6 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<artifactId>picocli</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.manorrock.sphynx</groupId>
<artifactId>sphynx-shared</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
31 changes: 9 additions & 22 deletions cli/src/main/java/com/manorrock/sphynx/cli/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
*/
package com.manorrock.sphynx.cli;

import com.manorrock.sphynx.shared.JobUtils;
import java.io.File;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.TRACE;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.UUID;
import java.util.concurrent.Callable;
import picocli.CommandLine;

Expand All @@ -61,37 +59,26 @@ public class DeleteCommand implements Callable<Integer> {
/**
* Stores the name.
*/
@CommandLine.Option(names = "--name", description = "The name of the job")
@CommandLine.Option(names = "--name", description = "The name of the job", required = true)
protected String name;

@Override
public Integer call() throws Exception {
/*
* Step 1 - determine name.
*/
LOGGER.log(DEBUG, "Determining name of the job");
if (name == null || name.trim().equals("")) {
LOGGER.log(TRACE, "No name specified");
LOGGER.log(TRACE, "Generating name");
name = UUID.randomUUID().toString();
}
LOGGER.log(INFO, "Using name: " + name);

/*
* Step 2 - delete directory structure recusively.
*/
File jobDirectory = new File(baseDirectory, "jobs" + File.separator + name);
if (!jobDirectory.exists()) {
LOGGER.log(ERROR, "Unable to delete job directory as it does not exist");
return 1;
} else {
Path jobPath = jobDirectory.toPath();
Files.walk(jobPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
LOGGER.log(INFO, "Deleted job: " + name);
int returnCode = JobUtils.deleteJob(baseDirectory, name);

switch (returnCode) {
case 1 -> {
LOGGER.log(ERROR, "Unable to delete job directory as it does not exist");
}
}
return 0;
return returnCode;
}
}

0 comments on commit 6e73cfa

Please sign in to comment.