Skip to content

Commit

Permalink
Fixes #283 - Implement delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Jul 3, 2024
1 parent dc3c2ce commit d9c86a2
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions cli/src/main/java/com/manorrock/sphynx/cli/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,71 @@
*/
package com.manorrock.sphynx.cli;

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;

/**
* The create command.
*
* The delete command.
*
* @author Manfred Riem ([email protected])
*/
@CommandLine.Command(name = "delete", mixinStandardHelpOptions = true)
public class DeleteCommand implements Callable<Integer> {
public class DeleteCommand implements Callable<Integer> {

/**
* Stores the logger.
*/
private static final System.Logger LOGGER = System.getLogger(CreateCommand.class.getName());

/**
* Stores the base directory.
*/
@CommandLine.Option(names = "--base-directory", description = "The base directory used for storage")
protected File baseDirectory = new File(System.getProperty("user.home") + "/.manorrock/sphynx");

/**
* Stores the name.
*/
@CommandLine.Option(names = "--name", description = "The name of the job")
protected String name;

@Override
public Integer call() throws Exception {
System.out.println("TODO - delete job");
/*
* 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);
}
return 0;
}
}

0 comments on commit d9c86a2

Please sign in to comment.