From 28dab6b5630cc9eccba4e8cf63862f66ea107ec5 Mon Sep 17 00:00:00 2001 From: Guille Date: Tue, 19 Nov 2019 10:25:35 +0100 Subject: [PATCH 1/4] Migrate the -sd CLI arg to Path to be able to map it later We will ensure the storage directory is absolute in the arg mapper --- src/org/opendatakit/briefcase/operations/Common.java | 12 ++++++------ src/org/opendatakit/briefcase/operations/Export.java | 2 +- .../briefcase/operations/ImportFromODK.java | 2 +- .../briefcase/operations/PullFormFromAggregate.java | 2 +- .../briefcase/operations/PushFormToAggregate.java | 2 +- src/org/opendatakit/briefcase/ui/BriefcaseCLI.java | 3 ++- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/org/opendatakit/briefcase/operations/Common.java b/src/org/opendatakit/briefcase/operations/Common.java index 94ee6b4da..9d6ba996a 100644 --- a/src/org/opendatakit/briefcase/operations/Common.java +++ b/src/org/opendatakit/briefcase/operations/Common.java @@ -25,18 +25,18 @@ import org.opendatakit.common.cli.Param; public class Common { - static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory"); + public static final Param DEPRECATED_AGGREGATE_SERVER = Param.arg("url", "aggregate_url", "Aggregate server URL"); + public static final Param SERVER_URL = Param.arg("U", "odk_url", "ODK Server URL", RequestBuilder::url); + public static final Param MAX_HTTP_CONNECTIONS = Param.arg("mhc", "max_http_connections", "Maximum simultaneous HTTP connections (defaults to 8)", Integer::parseInt); + static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory", Paths::get); static final Param FORM_ID = Param.arg("id", "form_id", "Form ID"); static final Param PROJECT_ID = Param.arg("pid", "project_id", "ODK Project ID number", Integer::parseInt); static final Param CREDENTIALS_USERNAME = Param.arg("u", "odk_username", "ODK Username"); static final Param CREDENTIALS_EMAIL = Param.arg("E", "odk_email", "ODK Email"); static final Param CREDENTIALS_PASSWORD = Param.arg("p", "odk_password", "ODK Password"); - public static final Param DEPRECATED_AGGREGATE_SERVER = Param.arg("url", "aggregate_url", "Aggregate server URL"); - public static final Param SERVER_URL = Param.arg("U", "odk_url", "ODK Server URL", RequestBuilder::url); - public static final Param MAX_HTTP_CONNECTIONS = Param.arg("mhc", "max_http_connections", "Maximum simultaneous HTTP connections (defaults to 8)", Integer::parseInt); - static Path getOrCreateBriefcaseDir(String storageDir) { - Path briefcaseDir = BriefcasePreferences.buildBriefcaseDir(Paths.get(storageDir)); + static Path getOrCreateBriefcaseDir(Path storageDir) { + Path briefcaseDir = BriefcasePreferences.buildBriefcaseDir(storageDir); if (!Files.exists(briefcaseDir)) { System.err.println("The directory " + briefcaseDir.toString() + " doesn't exist. Creating it"); UncheckedFiles.createBriefcaseDir(briefcaseDir); diff --git a/src/org/opendatakit/briefcase/operations/Export.java b/src/org/opendatakit/briefcase/operations/Export.java index 15531104c..3ed96a79f 100644 --- a/src/org/opendatakit/briefcase/operations/Export.java +++ b/src/org/opendatakit/briefcase/operations/Export.java @@ -96,7 +96,7 @@ public class Export { Arrays.asList(PEM_FILE, EXCLUDE_MEDIA, OVERWRITE, START, END, PULL_BEFORE, SPLIT_SELECT_MULTIPLES, INCLUDE_GEOJSON_EXPORT, REMOVE_GROUP_NAMES, SMART_APPEND) ); - public static void export(String storageDir, String formid, Path exportDir, String baseFilename, boolean exportMedia, boolean overwriteFiles, boolean pullBefore, Optional startDate, Optional endDate, Optional maybePemFile, boolean splitSelectMultiples, boolean includeGeoJsonExport, boolean removeGroupNames, boolean smartAppend) { + public static void export(Path storageDir, String formid, Path exportDir, String baseFilename, boolean exportMedia, boolean overwriteFiles, boolean pullBefore, Optional startDate, Optional endDate, Optional maybePemFile, boolean splitSelectMultiples, boolean includeGeoJsonExport, boolean removeGroupNames, boolean smartAppend) { CliEventsCompanion.attach(log); Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir); FormCache formCache = FormCache.from(briefcaseDir); diff --git a/src/org/opendatakit/briefcase/operations/ImportFromODK.java b/src/org/opendatakit/briefcase/operations/ImportFromODK.java index 50b39d1f5..e98b860e4 100644 --- a/src/org/opendatakit/briefcase/operations/ImportFromODK.java +++ b/src/org/opendatakit/briefcase/operations/ImportFromODK.java @@ -50,7 +50,7 @@ public class ImportFromODK { Arrays.asList(FORM_ID) ); - public static void importODK(String storageDir, Path odkDir, Optional formId) { + public static void importODK(Path storageDir, Path odkDir, Optional formId) { CliEventsCompanion.attach(log); Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir); FormCache formCache = FormCache.from(briefcaseDir); diff --git a/src/org/opendatakit/briefcase/operations/PullFormFromAggregate.java b/src/org/opendatakit/briefcase/operations/PullFormFromAggregate.java index c91211591..162d9c0fc 100644 --- a/src/org/opendatakit/briefcase/operations/PullFormFromAggregate.java +++ b/src/org/opendatakit/briefcase/operations/PullFormFromAggregate.java @@ -81,7 +81,7 @@ public class PullFormFromAggregate { Arrays.asList(RESUME_LAST_PULL, INCLUDE_INCOMPLETE, FORM_ID, START_FROM_DATE, MAX_HTTP_CONNECTIONS) ); - public static void pullFormFromAggregate(String storageDir, Optional formId, String username, String password, URL server, boolean resumeLastPull, Optional startFromDate, boolean includeIncomplete, Optional maybeMaxHttpConnections) { + public static void pullFormFromAggregate(Path storageDir, Optional formId, String username, String password, URL server, boolean resumeLastPull, Optional startFromDate, boolean includeIncomplete, Optional maybeMaxHttpConnections) { CliEventsCompanion.attach(log); Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir); FormCache formCache = FormCache.from(briefcaseDir); diff --git a/src/org/opendatakit/briefcase/operations/PushFormToAggregate.java b/src/org/opendatakit/briefcase/operations/PushFormToAggregate.java index e1226f3c2..6e1b96df7 100644 --- a/src/org/opendatakit/briefcase/operations/PushFormToAggregate.java +++ b/src/org/opendatakit/briefcase/operations/PushFormToAggregate.java @@ -68,7 +68,7 @@ public class PushFormToAggregate { Arrays.asList(FORCE_SEND_BLANK, MAX_HTTP_CONNECTIONS, FORM_ID) ); - private static void pushFormToAggregate(String storageDir, Optional formid, String username, String password, URL server, boolean forceSendBlank, Optional maybeMaxConnections) { + private static void pushFormToAggregate(Path storageDir, Optional formid, String username, String password, URL server, boolean forceSendBlank, Optional maybeMaxConnections) { CliEventsCompanion.attach(log); Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir); FormCache formCache = FormCache.from(briefcaseDir); diff --git a/src/org/opendatakit/briefcase/ui/BriefcaseCLI.java b/src/org/opendatakit/briefcase/ui/BriefcaseCLI.java index 92c0fb9f0..2b181899c 100644 --- a/src/org/opendatakit/briefcase/ui/BriefcaseCLI.java +++ b/src/org/opendatakit/briefcase/ui/BriefcaseCLI.java @@ -21,6 +21,7 @@ import static org.opendatakit.briefcase.operations.ImportFromODK.importODK; import static org.opendatakit.briefcase.operations.PullFormFromAggregate.pullFormFromAggregate; +import java.nio.file.Path; import java.nio.file.Paths; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -289,7 +290,7 @@ public void run() { String password = cli.getOptionValue(ODK_PASSWORD); String server = cli.getOptionValue(AGGREGATE_URL); String formid = cli.getOptionValue(FORM_ID); - String storageDir = cli.getOptionValue(STORAGE_DIRECTORY); + Path storageDir = Paths.get(cli.getOptionValue(STORAGE_DIRECTORY)); String fileName = cli.getOptionValue(EXPORT_FILENAME); String exportPath = cli.getOptionValue(EXPORT_DIRECTORY); String startDateString = cli.getOptionValue(EXPORT_START_DATE); From 88a1a2d9428bfa0460d3c5b81d22a12cd05966ca Mon Sep 17 00:00:00 2001 From: Guille Date: Tue, 19 Nov 2019 10:32:22 +0100 Subject: [PATCH 2/4] Ensure the -sd CLI arg is always parsed as an absolute path We will use the user.dir property (the directory where the java command was launched) to resolve the arg's value if it's a relative path --- src/org/opendatakit/briefcase/operations/Common.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/org/opendatakit/briefcase/operations/Common.java b/src/org/opendatakit/briefcase/operations/Common.java index 9d6ba996a..ad6de8778 100644 --- a/src/org/opendatakit/briefcase/operations/Common.java +++ b/src/org/opendatakit/briefcase/operations/Common.java @@ -28,7 +28,12 @@ public class Common { public static final Param DEPRECATED_AGGREGATE_SERVER = Param.arg("url", "aggregate_url", "Aggregate server URL"); public static final Param SERVER_URL = Param.arg("U", "odk_url", "ODK Server URL", RequestBuilder::url); public static final Param MAX_HTTP_CONNECTIONS = Param.arg("mhc", "max_http_connections", "Maximum simultaneous HTTP connections (defaults to 8)", Integer::parseInt); - static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory", Paths::get); + static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory", path -> { + Path storageDir = Paths.get(path); + return storageDir.isAbsolute() + ? storageDir + : Paths.get(System.getProperty("user.dir")).resolve(storageDir); + }); static final Param FORM_ID = Param.arg("id", "form_id", "Form ID"); static final Param PROJECT_ID = Param.arg("pid", "project_id", "ODK Project ID number", Integer::parseInt); static final Param CREDENTIALS_USERNAME = Param.arg("u", "odk_username", "ODK Username"); From 3e1ab7ca8af9c1c667cc559ad9642bf540952d33 Mon Sep 17 00:00:00 2001 From: Guille Date: Tue, 19 Nov 2019 10:34:12 +0100 Subject: [PATCH 3/4] Extract method to add semantics, docs and make it reusable We will use it in other path input args too --- .../briefcase/operations/Common.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/org/opendatakit/briefcase/operations/Common.java b/src/org/opendatakit/briefcase/operations/Common.java index ad6de8778..e1eb02e04 100644 --- a/src/org/opendatakit/briefcase/operations/Common.java +++ b/src/org/opendatakit/briefcase/operations/Common.java @@ -28,12 +28,7 @@ public class Common { public static final Param DEPRECATED_AGGREGATE_SERVER = Param.arg("url", "aggregate_url", "Aggregate server URL"); public static final Param SERVER_URL = Param.arg("U", "odk_url", "ODK Server URL", RequestBuilder::url); public static final Param MAX_HTTP_CONNECTIONS = Param.arg("mhc", "max_http_connections", "Maximum simultaneous HTTP connections (defaults to 8)", Integer::parseInt); - static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory", path -> { - Path storageDir = Paths.get(path); - return storageDir.isAbsolute() - ? storageDir - : Paths.get(System.getProperty("user.dir")).resolve(storageDir); - }); + static final Param STORAGE_DIR = Param.arg("sd", "storage_directory", "Briefcase storage directory", Common::absolutePath); static final Param FORM_ID = Param.arg("id", "form_id", "Form ID"); static final Param PROJECT_ID = Param.arg("pid", "project_id", "ODK Project ID number", Integer::parseInt); static final Param CREDENTIALS_USERNAME = Param.arg("u", "odk_username", "ODK Username"); @@ -48,4 +43,15 @@ static Path getOrCreateBriefcaseDir(Path storageDir) { } return briefcaseDir; } + + /** + * Returns an absolute path to the given string path, using the user.dir property + * to resolve input relative paths if necessary + */ + private static Path absolutePath(String path) { + Path storageDir = Paths.get(path); + return storageDir.isAbsolute() + ? storageDir + : Paths.get(System.getProperty("user.dir")).resolve(storageDir); + } } From 7d8488429654f3ccf31671c636e5db898abf7e30 Mon Sep 17 00:00:00 2001 From: Guille Date: Tue, 19 Nov 2019 10:35:11 +0100 Subject: [PATCH 4/4] Reuse the extracted method in all the path input args --- src/org/opendatakit/briefcase/operations/Common.java | 2 +- src/org/opendatakit/briefcase/operations/Export.java | 5 ++--- src/org/opendatakit/briefcase/operations/ImportFromODK.java | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/org/opendatakit/briefcase/operations/Common.java b/src/org/opendatakit/briefcase/operations/Common.java index e1eb02e04..4b84f6439 100644 --- a/src/org/opendatakit/briefcase/operations/Common.java +++ b/src/org/opendatakit/briefcase/operations/Common.java @@ -48,7 +48,7 @@ static Path getOrCreateBriefcaseDir(Path storageDir) { * Returns an absolute path to the given string path, using the user.dir property * to resolve input relative paths if necessary */ - private static Path absolutePath(String path) { + static Path absolutePath(String path) { Path storageDir = Paths.get(path); return storageDir.isAbsolute() ? storageDir diff --git a/src/org/opendatakit/briefcase/operations/Export.java b/src/org/opendatakit/briefcase/operations/Export.java index 3ed96a79f..86fa8892f 100644 --- a/src/org/opendatakit/briefcase/operations/Export.java +++ b/src/org/opendatakit/briefcase/operations/Export.java @@ -24,7 +24,6 @@ import static org.opendatakit.briefcase.reused.http.Http.DEFAULT_HTTP_CONNECTIONS; import java.nio.file.Path; -import java.nio.file.Paths; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Arrays; @@ -61,13 +60,13 @@ public class Export { private static final Logger log = LoggerFactory.getLogger(Export.class); private static final Param EXPORT = Param.flag("e", "export", "Export a form"); - private static final Param EXPORT_DIR = Param.arg("ed", "export_directory", "Export directory", Paths::get); + private static final Param EXPORT_DIR = Param.arg("ed", "export_directory", "Export directory", Common::absolutePath); private static final Param FILE = Param.arg("f", "export_filename", "Filename for export operation"); private static final Param START = Param.localDate("start", "export_start_date", "Export start date (inclusive)"); private static final Param END = Param.localDate("end", "export_end_date", "Export end date (inclusive)"); private static final Param EXCLUDE_MEDIA = Param.flag("em", "exclude_media_export", "Exclude media in export"); private static final Param OVERWRITE = Param.flag("oc", "overwrite_csv_export", "Overwrite files during export"); - private static final Param PEM_FILE = Param.arg("pf", "pem_file", "PEM file for form decryption", Paths::get); + private static final Param PEM_FILE = Param.arg("pf", "pem_file", "PEM file for form decryption", Common::absolutePath); private static final Param PULL_BEFORE = Param.flag("pb", "pull_before", "Pull before export"); private static final Param SPLIT_SELECT_MULTIPLES = Param.flag("ssm", "split_select_multiples", "Split select multiple fields"); private static final Param INCLUDE_GEOJSON_EXPORT = Param.flag("ig", "include_geojson", "Include a GeoJSON file with spatial data"); diff --git a/src/org/opendatakit/briefcase/operations/ImportFromODK.java b/src/org/opendatakit/briefcase/operations/ImportFromODK.java index e98b860e4..5f7cf824a 100644 --- a/src/org/opendatakit/briefcase/operations/ImportFromODK.java +++ b/src/org/opendatakit/briefcase/operations/ImportFromODK.java @@ -20,7 +20,6 @@ import static org.opendatakit.briefcase.operations.Common.STORAGE_DIR; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.Optional; import org.opendatakit.briefcase.model.FormStatus; @@ -37,7 +36,7 @@ public class ImportFromODK { private static final Logger log = LoggerFactory.getLogger(ImportFromODK.class); private static final Param IMPORT = Param.flag("pc", "pull_collect", "Pull from Collect"); - private static final Param ODK_DIR = Param.arg("od", "odk_directory", "ODK directory", Paths::get); + private static final Param ODK_DIR = Param.arg("od", "odk_directory", "ODK directory", Common::absolutePath); public static final Operation IMPORT_FROM_ODK = Operation.of( IMPORT,