feat: add args
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
},
|
},
|
||||||
"repo": {
|
"repo": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
|
"info.picocli:picocli:4.6.1",
|
||||||
"me.hatter:commons:3.0",
|
"me.hatter:commons:3.0",
|
||||||
"org.bouncycastle:bcprov-ext-jdk15on:1.70",
|
"org.bouncycastle:bcprov-ext-jdk15on:1.70",
|
||||||
"org.bouncycastle:bcpg-jdk15on:1.70",
|
"org.bouncycastle:bcpg-jdk15on:1.70",
|
||||||
|
|||||||
27
src/main/java/me/hatter/tool/signpdf/main/SignPdfArgs.java
Normal file
27
src/main/java/me/hatter/tool/signpdf/main/SignPdfArgs.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package me.hatter.tool.signpdf.main;
|
||||||
|
|
||||||
|
import picocli.CommandLine;
|
||||||
|
|
||||||
|
@CommandLine.Command(name = SignPdfConstant.NAME,
|
||||||
|
version = SignPdfConstant.NAME + " v" + SignPdfConstant.VERSION,
|
||||||
|
description = "\n" +
|
||||||
|
"Sign PDF tool\n" +
|
||||||
|
"\n" +
|
||||||
|
"Argument details:")
|
||||||
|
public class SignPdfArgs {
|
||||||
|
@CommandLine.Option(names = {"--in"}, description = "PDF file in")
|
||||||
|
String in;
|
||||||
|
@CommandLine.Option(names = {"--out"}, description = "PDF file out")
|
||||||
|
String out;
|
||||||
|
@CommandLine.Option(names = {"--name"}, description = "Name")
|
||||||
|
String name;
|
||||||
|
@CommandLine.Option(names = {"--location"}, description = "Location")
|
||||||
|
String location;
|
||||||
|
@CommandLine.Option(names = {"--reason"}, description = "Reason")
|
||||||
|
String reason;
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display a help message")
|
||||||
|
boolean helpRequested = false;
|
||||||
|
@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "Display a version message")
|
||||||
|
boolean versionRequested = false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package me.hatter.tool.signpdf.main;
|
||||||
|
|
||||||
|
import me.hatter.tools.commons.log.LogTool;
|
||||||
|
import me.hatter.tools.commons.log.LogTools;
|
||||||
|
import picocli.CommandLine;
|
||||||
|
|
||||||
|
public class SignPdfArgsUtil {
|
||||||
|
private static final LogTool log = LogTools.getLogTool(SignPdfArgsUtil.class);
|
||||||
|
|
||||||
|
public static SignPdfArgs parseArgs(String[] args) {
|
||||||
|
try {
|
||||||
|
return innerParseArgs(args);
|
||||||
|
} catch (CommandLine.UnmatchedArgumentException unmatchedArgumentException) {
|
||||||
|
log.error("Parse args failed: " + unmatchedArgumentException.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SignPdfArgs innerParseArgs(String[] args) {
|
||||||
|
final SignPdfArgs signPdfArgs = new SignPdfArgs();
|
||||||
|
final CommandLine cmd = new CommandLine(signPdfArgs);
|
||||||
|
cmd.parseArgs(args);
|
||||||
|
|
||||||
|
if (cmd.isUsageHelpRequested()) {
|
||||||
|
cmd.usage(cmd.getOut());
|
||||||
|
} else if (cmd.isVersionHelpRequested()) {
|
||||||
|
cmd.printVersionHelp(cmd.getOut());
|
||||||
|
} else {
|
||||||
|
return signPdfArgs;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package me.hatter.tool.signpdf.main;
|
||||||
|
|
||||||
|
public interface SignPdfConstant {
|
||||||
|
String NAME = "sign-pdf";
|
||||||
|
String VERSION = "0.0.0";
|
||||||
|
}
|
||||||
@@ -19,15 +19,28 @@ import java.util.List;
|
|||||||
public class SignPdfMain {
|
public class SignPdfMain {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final SignOptions signOptions = new SignOptions();
|
final SignPdfArgs signPdfArgs = SignPdfArgsUtil.parseArgs(args);
|
||||||
signOptions.setName("Example User");
|
if (signPdfArgs == null) {
|
||||||
signOptions.setLocation("Hangzhou, ZJ");
|
return;
|
||||||
signOptions.setReason("Testing");
|
}
|
||||||
|
if (StringUtil.isEmpty(signPdfArgs.in) || StringUtil.isEmpty(signPdfArgs.out)) {
|
||||||
|
throw new RuntimeException("PDF file in/out cannot be empty.");
|
||||||
|
}
|
||||||
|
|
||||||
final File inFile = new File("Resume.pdf");
|
final SignOptions signOptions = new SignOptions();
|
||||||
final String name = inFile.getName();
|
signOptions.setName(signPdfArgs.name);
|
||||||
final String substring = name.substring(0, name.lastIndexOf('.'));
|
signOptions.setLocation(signPdfArgs.location);
|
||||||
final File outFile = new File(inFile.getParent(), substring + "_signed.pdf");
|
signOptions.setReason(signPdfArgs.reason);
|
||||||
|
|
||||||
|
final File inFile = new File(signPdfArgs.in);
|
||||||
|
final File outFile = new File(signPdfArgs.out);
|
||||||
|
|
||||||
|
if (!inFile.exists()) {
|
||||||
|
throw new RuntimeException("PDF file in not exists.");
|
||||||
|
}
|
||||||
|
if (outFile.exists()) {
|
||||||
|
throw new RuntimeException("PDF file out exists.");
|
||||||
|
}
|
||||||
|
|
||||||
final List<X509Certificate> certs = X509CertUtil.parseX509CertificateList(
|
final List<X509Certificate> certs = X509CertUtil.parseX509CertificateList(
|
||||||
RFile.from("__certs.pem").string()
|
RFile.from("__certs.pem").string()
|
||||||
|
|||||||
Reference in New Issue
Block a user