feat: add args

This commit is contained in:
2023-10-30 00:31:06 +08:00
parent 71879f2085
commit aac3d90525
5 changed files with 88 additions and 8 deletions

View File

@@ -12,6 +12,7 @@
},
"repo": {
"dependencies": [
"info.picocli:picocli:4.6.1",
"me.hatter:commons:3.0",
"org.bouncycastle:bcprov-ext-jdk15on:1.70",
"org.bouncycastle:bcpg-jdk15on:1.70",

View 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;
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,6 @@
package me.hatter.tool.signpdf.main;
public interface SignPdfConstant {
String NAME = "sign-pdf";
String VERSION = "0.0.0";
}

View File

@@ -19,15 +19,28 @@ import java.util.List;
public class SignPdfMain {
public static void main(String[] args) throws Exception {
final SignOptions signOptions = new SignOptions();
signOptions.setName("Example User");
signOptions.setLocation("Hangzhou, ZJ");
signOptions.setReason("Testing");
final SignPdfArgs signPdfArgs = SignPdfArgsUtil.parseArgs(args);
if (signPdfArgs == null) {
return;
}
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 String name = inFile.getName();
final String substring = name.substring(0, name.lastIndexOf('.'));
final File outFile = new File(inFile.getParent(), substring + "_signed.pdf");
final SignOptions signOptions = new SignOptions();
signOptions.setName(signPdfArgs.name);
signOptions.setLocation(signPdfArgs.location);
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(
RFile.from("__certs.pem").string()