diff --git a/src/main/java/nl/knaw/dans/dvingestcli/command/.keepme b/docs/arch.md similarity index 100% rename from src/main/java/nl/knaw/dans/dvingestcli/command/.keepme rename to docs/arch.md diff --git a/docs/description.md b/docs/description.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/dev.md b/docs/dev.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 0000000..e69de29 diff --git a/mkdocs.yml b/mkdocs.yml index 9fadedd..737c28d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -22,8 +22,14 @@ repo_name: DANS-KNAW/dd-dataverse-ingest-cli repo_url: https://github.com/DANS-KNAW/dd-dataverse-ingest-cli nav: - - Manual: index.md - + - Manual: + - Introduction: index.md + - Description: description.md +# - Examples: examples.md + - Installation: install.md + - Development: + - Overview: dev.md + - Context: arch.md plugins: - markdownextradata - search diff --git a/pom.xml b/pom.xml index b2cfe1b..0e77298 100644 --- a/pom.xml +++ b/pom.xml @@ -16,8 +16,7 @@ limitations under the License. --> - + 4.0.0 @@ -27,9 +26,9 @@ dd-dataverse-ingest-cli - 0.1.0-SNAPSHOT + 0.1.1-SNAPSHOT - Dd Dataverse Ingest Cli + DD Dataverse Ingest CLI https://github.com/DANS-KNAW/dd-dataverse-ingest-cli CLI for dd-dataverse-ingest 2024 @@ -38,6 +37,7 @@ nl.knaw.dans.dvingestcli.DdDataverseIngestCli 4.7.5 ingest + 0.1.0-SNAPSHOT @@ -79,8 +79,6 @@ commons-io commons-io - - @@ -185,6 +182,52 @@ + + org.apache.maven.plugins + maven-dependency-plugin + + + initialize + + unpack + + + + + nl.knaw.dans + dd-dataverse-ingest-api + ${dd-dataverse-ingest-api.version} + ${project.build.directory}/openapi + + + + + + + + org.openapitools + openapi-generator-maven-plugin + + + generate-dd-dataverse-ingest-client + + generate + + + ${project.build.directory}/openapi/dd-dataverse-ingest-api.yml + java + + nl.knaw.dans.dvingest.client + nl.knaw.dans.dvingest.api + nl.knaw.dans.dvingest.client + none + jersey2 + false + + + + + diff --git a/src/main/assembly/dist/cfg/example-config.yml b/src/main/assembly/dist/cfg/example-config.yml index 7b811a9..6da6828 100644 --- a/src/main/assembly/dist/cfg/example-config.yml +++ b/src/main/assembly/dist/cfg/example-config.yml @@ -1,3 +1,10 @@ +dataverseIngest: + url: "http://localhost:20360" + httpClient: + connectTimeout: 15s + readTimeout: 60s + + # # See https://www.dropwizard.io/en/latest/manual/configuration.html#logging # diff --git a/src/main/java/nl/knaw/dans/dvingestcli/DdDataverseIngestCli.java b/src/main/java/nl/knaw/dans/dvingestcli/DdDataverseIngestCli.java index 58180bb..41210c9 100644 --- a/src/main/java/nl/knaw/dans/dvingestcli/DdDataverseIngestCli.java +++ b/src/main/java/nl/knaw/dans/dvingestcli/DdDataverseIngestCli.java @@ -16,11 +16,16 @@ package nl.knaw.dans.dvingestcli; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; +import nl.knaw.dans.dvingest.client.ApiClient; +import nl.knaw.dans.dvingest.client.DefaultApi; +import nl.knaw.dans.dvingestcli.command.GetImportStatus; +import nl.knaw.dans.dvingestcli.command.StartImport; +import nl.knaw.dans.dvingestcli.config.DdDataverseIngestCliConfig; import nl.knaw.dans.lib.util.AbstractCommandLineApp; +import nl.knaw.dans.lib.util.ClientProxyBuilder; import nl.knaw.dans.lib.util.PicocliVersionProvider; -import nl.knaw.dans.dvingestcli.config.DdDataverseIngestCliConfig; -import picocli.AutoComplete.GenerateCompletion; import picocli.CommandLine; import picocli.CommandLine.Command; @@ -40,8 +45,15 @@ public String getName() { @Override public void configureCommandLine(CommandLine commandLine, DdDataverseIngestCliConfig config) { - // TODO: set up the API client, if applicable + var objectMapper = new ObjectMapper(); + DefaultApi api = new ClientProxyBuilder() + .apiClient(new ApiClient()) + .defaultApiCtor(DefaultApi::new) + .httpClient(config.getDataverseIngest().getHttpClient()) + .basePath(config.getDataverseIngest().getUrl()).build(); + log.debug("Configuring command line"); - // TODO: add options and subcommands + commandLine.addSubcommand(new StartImport(api, objectMapper)); + commandLine.addSubcommand(new GetImportStatus(api, objectMapper)); } } diff --git a/src/main/java/nl/knaw/dans/dvingestcli/command/GetImportStatus.java b/src/main/java/nl/knaw/dans/dvingestcli/command/GetImportStatus.java new file mode 100644 index 0000000..5423349 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingestcli/command/GetImportStatus.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingestcli.command; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import nl.knaw.dans.dvingest.client.DefaultApi; +import picocli.CommandLine.Command; +import picocli.CommandLine.Parameters; + +import java.util.concurrent.Callable; + +@RequiredArgsConstructor +@Command(name = "get-import-status") +public class GetImportStatus implements Callable { + @NonNull + private final DefaultApi api; + + @NonNull + private final ObjectMapper objectMapper; + + @Parameters(index = "0", paramLabel = "location", description = "The path to the import batch", arity = "0..1") + private String location; + + @Override + public Integer call() { + try { + var status = api.ingestGet(location); + System.out.println(objectMapper.writeValueAsString(status)); + System.err.println("Import status retrieved: " + location); + } + catch (Exception e) { + System.err.println("Error getting import status: " + e.getMessage()); + e.printStackTrace(); + return 1; + } + return 0; + } + +} diff --git a/src/main/java/nl/knaw/dans/dvingestcli/command/StartImport.java b/src/main/java/nl/knaw/dans/dvingestcli/command/StartImport.java new file mode 100644 index 0000000..662a7ce --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingestcli/command/StartImport.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingestcli.command; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import nl.knaw.dans.dvingest.api.ImportCommandDto; +import nl.knaw.dans.dvingest.client.ApiException; +import nl.knaw.dans.dvingest.client.DefaultApi; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.concurrent.Callable; + +@Command(name = "start-import", + mixinStandardHelpOptions = true, + description = "Start an import job") +@RequiredArgsConstructor +public class StartImport implements Callable { + @NonNull + private final DefaultApi api; + + @NonNull + private final ObjectMapper objectMapper; + + @Parameters(index = "0", paramLabel = "path", description = "The path to the file to import") + private Path path; + + @Option(names = { "-s", "--single-object" }, description = "Import as single object") + private boolean singleObject; + + @Override + public Integer call() { + try { + var canonicalPath = path.toRealPath().toString(); + var status = api.ingestPost( + new ImportCommandDto() + .path(canonicalPath) + .singleObject(singleObject)); + System.out.println(objectMapper.writeValueAsString(status)); + System.err.println("Import started: " + canonicalPath); + } + catch (JsonProcessingException e) { + System.err.println("Status report could not be parsed: " + e.getMessage()); + return 1; + } + catch (ApiException | IOException e) { + System.err.println("Error starting import: " + e.getMessage()); + e.printStackTrace(); + return 1; + } + return 0; + } +} diff --git a/src/main/java/nl/knaw/dans/dvingestcli/config/DataverseIngestConfig.java b/src/main/java/nl/knaw/dans/dvingestcli/config/DataverseIngestConfig.java new file mode 100644 index 0000000..db57fec --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingestcli/config/DataverseIngestConfig.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingestcli.config; + +import io.dropwizard.client.JerseyClientConfiguration; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.net.URI; + +@Data +public class DataverseIngestConfig { + @NotNull + private URI url; + + @NotNull + private JerseyClientConfiguration httpClient; +} diff --git a/src/main/java/nl/knaw/dans/dvingestcli/config/DdDataverseIngestCliConfig.java b/src/main/java/nl/knaw/dans/dvingestcli/config/DdDataverseIngestCliConfig.java index fd190bc..cfc5282 100644 --- a/src/main/java/nl/knaw/dans/dvingestcli/config/DdDataverseIngestCliConfig.java +++ b/src/main/java/nl/knaw/dans/dvingestcli/config/DdDataverseIngestCliConfig.java @@ -17,7 +17,11 @@ package nl.knaw.dans.dvingestcli.config; import io.dropwizard.core.Configuration; +import lombok.Data; +import lombok.EqualsAndHashCode; +@Data +@EqualsAndHashCode(callSuper = true) public class DdDataverseIngestCliConfig extends Configuration { - // TODO: add configuration fields + private DataverseIngestConfig dataverseIngest; }