feat works
This commit is contained in:
@@ -21,6 +21,9 @@ public class TinyEncryptArgs {
|
||||
@CommandLine.Option(names = {"--config"}, description = "Encrypt config")
|
||||
File config;
|
||||
|
||||
@CommandLine.Option(names = {"--remove-file"}, description = "Remove origin config")
|
||||
boolean removeFile = false;
|
||||
|
||||
@CommandLine.Parameters(paramLabel = "FILE", description = "Encrypt or Decrypt files")
|
||||
File[] files;
|
||||
|
||||
|
||||
@@ -105,10 +105,19 @@ public class TinyEncryptMain {
|
||||
int index = 1;
|
||||
for (File f : tinyEncryptArgs.files) {
|
||||
log.info("Start processing file: " + f + ", " + index + " of " + total);
|
||||
if (!f.isFile()) {
|
||||
log.info("Skip not a file: " + f);
|
||||
continue;
|
||||
}
|
||||
boolean result;
|
||||
if (tinyEncryptArgs.encrypt) {
|
||||
EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
|
||||
result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
|
||||
} else {
|
||||
EncryptedFileUtil.decryptFile(config, f);
|
||||
result = EncryptedFileUtil.decryptFile(config, f);
|
||||
}
|
||||
if (result && tinyEncryptArgs.removeFile) {
|
||||
log.info("Remove file: " + f);
|
||||
f.delete();
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -21,15 +21,15 @@ public class EncryptedFileUtil {
|
||||
BCUtil.init();
|
||||
}
|
||||
|
||||
public static void decryptFile(TinyEncryptConfig config, File file) {
|
||||
public static boolean decryptFile(TinyEncryptConfig config, File file) {
|
||||
File decFile = getDecryptFile(file);
|
||||
if (decFile == null) {
|
||||
log.warn("File is not tinyenc file, skip: " + decFile);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (decFile.exists()) {
|
||||
log.warn("File exists, skip: " + decFile);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
@@ -49,17 +49,19 @@ public class EncryptedFileUtil {
|
||||
}
|
||||
}
|
||||
log.info("Decrypt file success: " + file);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Decrypt file filed: " + file + ", reason: " + e.getMessage());
|
||||
log.debug("Decrypt file filed: " + file + ", reason: " + e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
||||
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
||||
File encFile = getEncryptFile(file);
|
||||
if (encFile.exists()) {
|
||||
log.warn("File exists, skip: " + encFile);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
@@ -78,9 +80,11 @@ public class EncryptedFileUtil {
|
||||
}
|
||||
}
|
||||
log.info("Encrypt file success: " + file);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Encrypt file filed: " + file + ", reason: " + e.getMessage());
|
||||
log.debug("Encrypt file filed: " + file + ", reason: " + e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user