mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-16 09:11:08 +01:00
Refactoring
This commit is contained in:
parent
ddcd4bff00
commit
cb4d4444e5
11 changed files with 69 additions and 30 deletions
50
pgpainless-cert-d-cli/build.gradle
Normal file
50
pgpainless-cert-d-cli/build.gradle
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
plugins {
|
||||
id 'application'
|
||||
}
|
||||
|
||||
group 'org.pgpainless'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
|
||||
// Logging
|
||||
testImplementation "ch.qos.logback:logback-classic:$logbackVersion"
|
||||
|
||||
implementation project(":pgpainless-cert-d")
|
||||
|
||||
// picocli for cli
|
||||
implementation "info.picocli:picocli:4.6.2"
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
mainClassName = 'pgp.cert_d.cli.PGPCertDCli'
|
||||
|
||||
jar {
|
||||
dependsOn(":pgpainless-cert-d:assemble", ":pgp-certificate-store:assemble", ":pgp-cert-d-java:assemble", ":pgpainless-core:assemble")
|
||||
manifest {
|
||||
attributes 'Main-Class': "$mainClassName"
|
||||
}
|
||||
|
||||
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
|
||||
|
||||
from {
|
||||
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
|
||||
} {
|
||||
exclude "META-INF/*.SF"
|
||||
exclude "META-INF/*.DSA"
|
||||
exclude "META-INF/*.RSA"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d.cli;
|
||||
|
||||
import org.pgpainless.certificate_store.CertificateReader;
|
||||
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
||||
import pgp.cert_d.cli.commands.Get;
|
||||
import pgp.cert_d.cli.commands.Import;
|
||||
import pgp.cert_d.exception.NotAStoreException;
|
||||
import pgp.certificate_store.CertificateStore;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@CommandLine.Command(
|
||||
subcommands = {
|
||||
Import.class,
|
||||
Get.class,
|
||||
}
|
||||
)
|
||||
public class PGPCertDCli {
|
||||
|
||||
@CommandLine.Option(names = "--base-directory", paramLabel = "DIRECTORY", description = "Overwrite the default certificate directory")
|
||||
File baseDirectory;
|
||||
|
||||
private static CertificateStore certificateStore;
|
||||
|
||||
private int executionStrategy(CommandLine.ParseResult parseResult) {
|
||||
try {
|
||||
initStore();
|
||||
} catch (NotAStoreException e) {
|
||||
return -1;
|
||||
}
|
||||
return new CommandLine.RunLast().execute(parseResult);
|
||||
}
|
||||
|
||||
private void initStore() throws NotAStoreException {
|
||||
SharedPGPCertificateDirectoryImpl certificateDirectory;
|
||||
if (baseDirectory != null) {
|
||||
certificateDirectory = new SharedPGPCertificateDirectoryImpl(
|
||||
baseDirectory,
|
||||
new CertificateReader());
|
||||
} else {
|
||||
certificateDirectory = new SharedPGPCertificateDirectoryImpl(
|
||||
new CertificateReader());
|
||||
}
|
||||
certificateStore = new SharedPGPCertificateDirectoryAdapter(certificateDirectory);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PGPCertDCli cli = new PGPCertDCli();
|
||||
new CommandLine(cli)
|
||||
.setExecutionStrategy(parserResult -> cli.executionStrategy(parserResult))
|
||||
.execute(args);
|
||||
}
|
||||
|
||||
public static CertificateStore getCertificateDirectory() {
|
||||
return certificateStore;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d.cli.commands;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pgp.cert_d.cli.PGPCertDCli;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "get",
|
||||
description = "Retrieve certificates from the store")
|
||||
public class Get implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Get.class);
|
||||
|
||||
@CommandLine.Parameters(
|
||||
paramLabel = "IDENTIFIER",
|
||||
arity = "1",
|
||||
description = "Certificate identifier (fingerprint or special name)"
|
||||
)
|
||||
String identifer;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Certificate certificate = PGPCertDCli.getCertificateDirectory()
|
||||
.getCertificate(identifer);
|
||||
if (certificate == null) {
|
||||
return;
|
||||
}
|
||||
Streams.pipeAll(certificate.getInputStream(), System.out);
|
||||
} catch (IOException e) {
|
||||
LOGGER.info("IO Error", e);
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d.cli.commands;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pgp.cert_d.cli.PGPCertDCli;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "import",
|
||||
description = "Import or update a certificate")
|
||||
public class Import implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Import.class);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Certificate certificate = PGPCertDCli.getCertificateDirectory().insertCertificate(System.in, new MergeCallback() {
|
||||
@Override
|
||||
public Certificate merge(Certificate data, Certificate existing) {
|
||||
return data;
|
||||
}
|
||||
});
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println(certificate.getFingerprint());
|
||||
// CHECKSTYLE:ON
|
||||
} catch (IOException e) {
|
||||
LOGGER.info("IO-Error", e);
|
||||
System.exit(-1);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.info("Thread interrupted.", e);
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Subcommands.
|
||||
*/
|
||||
package pgp.cert_d.cli.commands;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Command Line Interface for the Shared PGP Certificate Directory.
|
||||
*/
|
||||
package pgp.cert_d.cli;
|
||||
Loading…
Add table
Add a link
Reference in a new issue