|
| 1 | +package com.scalar.db.dataloader.cli.command.dataexport; |
| 2 | + |
| 3 | +import com.scalar.db.dataloader.cli.exception.DirectoryValidationException; |
| 4 | +import com.scalar.db.dataloader.cli.exception.InvalidFileExtensionException; |
| 5 | +import com.scalar.db.dataloader.cli.util.DirectoryUtils; |
| 6 | +import java.io.File; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | +import javax.annotation.Nullable; |
| 11 | +import org.apache.commons.io.FilenameUtils; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import picocli.CommandLine; |
| 14 | +import picocli.CommandLine.Model.CommandSpec; |
| 15 | +import picocli.CommandLine.Spec; |
| 16 | + |
| 17 | +@CommandLine.Command(name = "export", description = "Export data from a ScalarDB table") |
| 18 | +public class ExportCommand extends ExportCommandOptions implements Callable<Integer> { |
| 19 | + |
| 20 | + private static final List<String> ALLOWED_EXTENSIONS = Arrays.asList("csv", "json", "jsonl"); |
| 21 | + |
| 22 | + @Spec CommandSpec spec; |
| 23 | + |
| 24 | + @Override |
| 25 | + public Integer call() throws Exception { |
| 26 | + validateOutputDirectory(outputFilePath); |
| 27 | + return 0; |
| 28 | + } |
| 29 | + |
| 30 | + private void validateOutputDirectory(@Nullable String path) |
| 31 | + throws DirectoryValidationException, InvalidFileExtensionException { |
| 32 | + if (path == null || path.isEmpty()) { |
| 33 | + // It is ok for the output file path to be null or empty as a default file name will be used |
| 34 | + // if not provided |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + File file = new File(path); |
| 39 | + |
| 40 | + if (file.isDirectory()) { |
| 41 | + validateDirectory(path); |
| 42 | + } else { |
| 43 | + validateFileExtension(file.getName()); |
| 44 | + validateDirectory(file.getParent()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void validateDirectory(String directoryPath) throws DirectoryValidationException { |
| 49 | + // If the directory path is null or empty, use the current working directory |
| 50 | + if (directoryPath == null || directoryPath.isEmpty()) { |
| 51 | + DirectoryUtils.validateTargetDirectory(DirectoryUtils.getCurrentWorkingDirectory()); |
| 52 | + } else { |
| 53 | + DirectoryUtils.validateTargetDirectory(directoryPath); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void validateFileExtension(String filename) throws InvalidFileExtensionException { |
| 58 | + String extension = FilenameUtils.getExtension(filename); |
| 59 | + if (StringUtils.isBlank(extension)) { |
| 60 | + throw new InvalidFileExtensionException( |
| 61 | + String.format("No file extension was found on the provided file name %s.", filename)); |
| 62 | + } |
| 63 | + if (!ALLOWED_EXTENSIONS.contains(extension.toLowerCase())) { |
| 64 | + throw new InvalidFileExtensionException( |
| 65 | + String.format( |
| 66 | + "Invalid file extension: %s. Allowed extensions are: %s", |
| 67 | + extension, String.join(", ", ALLOWED_EXTENSIONS))); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments