feat: v0.3.3, code style

This commit is contained in:
2021-06-05 09:37:54 +08:00
parent 084bc7198b
commit 6c1c0fc97e
3 changed files with 107 additions and 84 deletions

View File

@@ -32,21 +32,22 @@ public class TinyEncryptMain {
log = LogTools.getLogTool(TinyEncryptMain.class); log = LogTools.getLogTool(TinyEncryptMain.class);
} }
public static void main(String[] args) { private static TinyEncryptArgs parseTinyEncryptArgs(String[] args) {
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs(); TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
CommandLine cmd = new CommandLine(tinyEncryptArgs); CommandLine cmd = new CommandLine(tinyEncryptArgs);
cmd.parseArgs(args); cmd.parseArgs(args);
if (cmd.isUsageHelpRequested()) { if (cmd.isUsageHelpRequested()) {
cmd.usage(cmd.getOut()); cmd.usage(cmd.getOut());
return; return null;
} else if (cmd.isVersionHelpRequested()) { } else if (cmd.isVersionHelpRequested()) {
cmd.printVersionHelp(cmd.getOut()); cmd.printVersionHelp(cmd.getOut());
return; return null;
}
return tinyEncryptArgs;
} }
// ==================================================================================== private static void doInitConfig(TinyEncryptArgs tinyEncryptArgs) {
if (tinyEncryptArgs.doInitConfig) {
if (StringUtil.isEmpty(tinyEncryptArgs.key)) { if (StringUtil.isEmpty(tinyEncryptArgs.key)) {
log.error("Default key is not assigned"); log.error("Default key is not assigned");
return; return;
@@ -68,10 +69,9 @@ public class TinyEncryptMain {
writeTinyEncryptConfig.setLocalPrivateKeyPem(KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate())); writeTinyEncryptConfig.setLocalPrivateKeyPem(KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate()));
writeTinyEncryptConfigRFile.write(JSON.toJSONString(writeTinyEncryptConfig, true)); writeTinyEncryptConfigRFile.write(JSON.toJSONString(writeTinyEncryptConfig, true));
log.info("Write file success: " + writeTinyEncryptConfigRFile.file()); log.info("Write file success: " + writeTinyEncryptConfigRFile.file());
return;
} }
if (tinyEncryptArgs.fileInfo) { private static void fileInfo(TinyEncryptArgs tinyEncryptArgs) {
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) { if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
log.error("No file assigned"); log.error("No file assigned");
return; return;
@@ -115,6 +115,39 @@ public class TinyEncryptMain {
} }
} }
} }
}
private static TinyEncryptConfig loadTinyEncryptConfig(TinyEncryptArgs tinyEncryptArgs) {
TinyEncryptConfig config;
if (tinyEncryptArgs.config != null) {
config = RFile.from(tinyEncryptArgs.config).parseJSONObject(TinyEncryptConfig.class);
} else {
RFile defaultTinyEncryptConfigFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
if (defaultTinyEncryptConfigFile.notExists()) {
log.error("Config file not assigned, and no default config file: " + DEFAULT_TINY_ENCRYPT_CONFIG);
return null;
}
config = defaultTinyEncryptConfigFile.parseJSONObject(TinyEncryptConfig.class);
}
if (StringUtil.isNotBlank(tinyEncryptArgs.key)) {
log.info("Using key from args: " + tinyEncryptArgs.key);
config.setDefaultKeyName(tinyEncryptArgs.key);
}
return config;
}
public static void main(String[] args) {
TinyEncryptArgs tinyEncryptArgs = parseTinyEncryptArgs(args);
if (tinyEncryptArgs == null) {
return;
}
// ====================================================================================
if (tinyEncryptArgs.doInitConfig) { // --init-config
doInitConfig(tinyEncryptArgs);
return;
}
if (tinyEncryptArgs.fileInfo) { // --info
fileInfo(tinyEncryptArgs);
return; return;
} }
// ==================================================================================== // ====================================================================================
@@ -130,21 +163,10 @@ public class TinyEncryptMain {
return; return;
} }
TinyEncryptConfig config; TinyEncryptConfig config = loadTinyEncryptConfig(tinyEncryptArgs);
if (tinyEncryptArgs.config != null) { if (config == null) {
config = RFile.from(tinyEncryptArgs.config).parseJSONObject(TinyEncryptConfig.class);
} else {
RFile defaultTinyEncryptConfigFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
if (defaultTinyEncryptConfigFile.notExists()) {
log.error("Config file not assigned, and no default config file: " + DEFAULT_TINY_ENCRYPT_CONFIG);
return; return;
} }
config = defaultTinyEncryptConfigFile.parseJSONObject(TinyEncryptConfig.class);
}
if (StringUtil.isNotBlank(tinyEncryptArgs.key)) {
log.info("Using key from args: " + tinyEncryptArgs.key);
config.setDefaultKeyName(tinyEncryptArgs.key);
}
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) { if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
log.error("FILE is not assigned."); log.error("FILE is not assigned.");
@@ -173,7 +195,9 @@ public class TinyEncryptMain {
} }
if (result && tinyEncryptArgs.removeFile) { if (result && tinyEncryptArgs.removeFile) {
log.info("Remove file: " + f); log.info("Remove file: " + f);
f.delete(); if (!f.delete()) {
log.warn("Remove file: " + f + " failed.");
}
} }
index++; index++;
} }

View File

@@ -1,7 +1,7 @@
package me.hatter.tools.tinyencrypt.config; package me.hatter.tools.tinyencrypt.config;
public class TinyEncryptConstant { public class TinyEncryptConstant {
public static final String VERSION = "0.3.2"; public static final String VERSION = "0.3.3";
public static final String ENC_FILE_EXT = ".tinyenc"; public static final String ENC_FILE_EXT = ".tinyenc";
} }

View File

@@ -55,7 +55,6 @@ public class EncryptedFileUtil {
.message("File: " + file) .message("File: " + file)
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8)) .text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
.show().getResult(); .show().getResult();
// return false;
} }
public static boolean decryptFile(TinyEncryptConfig config, File file) { public static boolean decryptFile(TinyEncryptConfig config, File file) {