feat: v0.3.3, code style
This commit is contained in:
@@ -32,21 +32,22 @@ public class TinyEncryptMain {
|
||||
log = LogTools.getLogTool(TinyEncryptMain.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
private static TinyEncryptArgs parseTinyEncryptArgs(String[] args) {
|
||||
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
|
||||
CommandLine cmd = new CommandLine(tinyEncryptArgs);
|
||||
cmd.parseArgs(args);
|
||||
|
||||
if (cmd.isUsageHelpRequested()) {
|
||||
cmd.usage(cmd.getOut());
|
||||
return;
|
||||
return null;
|
||||
} else if (cmd.isVersionHelpRequested()) {
|
||||
cmd.printVersionHelp(cmd.getOut());
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
return tinyEncryptArgs;
|
||||
}
|
||||
|
||||
// ====================================================================================
|
||||
if (tinyEncryptArgs.doInitConfig) {
|
||||
private static void doInitConfig(TinyEncryptArgs tinyEncryptArgs) {
|
||||
if (StringUtil.isEmpty(tinyEncryptArgs.key)) {
|
||||
log.error("Default key is not assigned");
|
||||
return;
|
||||
@@ -68,10 +69,9 @@ public class TinyEncryptMain {
|
||||
writeTinyEncryptConfig.setLocalPrivateKeyPem(KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate()));
|
||||
writeTinyEncryptConfigRFile.write(JSON.toJSONString(writeTinyEncryptConfig, true));
|
||||
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)) {
|
||||
log.error("No file assigned");
|
||||
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;
|
||||
}
|
||||
// ====================================================================================
|
||||
@@ -130,21 +163,10 @@ public class TinyEncryptMain {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
TinyEncryptConfig config = loadTinyEncryptConfig(tinyEncryptArgs);
|
||||
if (config == null) {
|
||||
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)) {
|
||||
log.error("FILE is not assigned.");
|
||||
@@ -173,7 +195,9 @@ public class TinyEncryptMain {
|
||||
}
|
||||
if (result && tinyEncryptArgs.removeFile) {
|
||||
log.info("Remove file: " + f);
|
||||
f.delete();
|
||||
if (!f.delete()) {
|
||||
log.warn("Remove file: " + f + " failed.");
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ public class EncryptedFileUtil {
|
||||
.message("File: " + file)
|
||||
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
|
||||
.show().getResult();
|
||||
// return false;
|
||||
}
|
||||
|
||||
public static boolean decryptFile(TinyEncryptConfig config, File file) {
|
||||
|
||||
Reference in New Issue
Block a user