-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_import_creds.groovy
151 lines (133 loc) · 5.49 KB
/
utils_import_creds.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* :name = Import Credentials :description=
*
* @author Manuel Souto Pico, Kos Ivantsov
* @date 2024-06-20
* @update 2024-08-29 [overwrite lines if the imported file contains creds for repos already existing in 'repositories.properties']
* @version 0.3
*/
import java.text.SimpleDateFormat
import java.util.Date
import java.util.TimeZone
import javax.swing.JFileChooser
import org.omegat.util.StaticUtils
import static javax.swing.JOptionPane.*
// Define constants
configDir = StaticUtils.getConfigDir()
credsFile = new File(configDir + File.separator + "repositories.properties")
// Titles and info in console
scriptName = "Import Credentials"
scriptConsoleTag = "${scriptName}\n${"="*scriptName.size()}"
// Function to check for binary files
def isBinaryFile(File file) {
buffer = new byte[1024]
int bytesRead = file.withInputStream { it.read(buffer) }
// Check if buffer contains non-printable characters
for (int i = 0; i < bytesRead; i++) {
byte b = buffer[i]
if (b < 0x09 || (b > 0x0D && b < 0x20) || b == 0x7F) {
return true
}
}
return false
}
// Function to check if file contains "!username=" and "!password="
def containsRequiredText(File file) {
text = file.text
return text.contains("!username=") && text.contains("!password=")
}
// Function to strip username and password from lines
// It will be used when comparing lines in the existing and the imported files
def normalizeLine(line) {
line = line
.replaceAll(/!password=.*/, /!password=/)
.replaceAll(/!username=.*/, /!username=/)
return line.trim()
}
// Select a creds file to import
console.println("${scriptConsoleTag}")
fileChooser = new JFileChooser()
fileChooser.dialogTitle = scriptName
fileChooser.fileSelectionMode = JFileChooser.FILES_ONLY
boolean validFileSelected = false
// Check if the user selected a supported file or closed the dialog
while (!validFileSelected) {
int result = fileChooser.showOpenDialog()
if (result == JFileChooser.APPROVE_OPTION) {
// Get the selected file
selectedFile = fileChooser.getSelectedFile()
console.println("Selected file: ${selectedFile.getAbsolutePath()}")
// Check the file extension
if (selectedFile.name.endsWith(".done")) {
// Show an error dialog if the file has ".done" extension
message = "The selected file has already been processed.\nSelect another file."
showMessageDialog(null, message, "Invalid File", ERROR_MESSAGE)
console.println(message)
} else if (isBinaryFile(selectedFile)) {
// Show an error dialog if the file is a binary
message = "The selected file is a binary file and cannot be used.\nSelect another file."
showMessageDialog(null, message, "Invalid File", ERROR_MESSAGE)
console.println(message)
} else if (!containsRequiredText(selectedFile)) {
// Show an error dialog if the file does not contain '!username=' and '!password='
message = "The selected file does not contain credentials.\nSelect another file."
showMessageDialog(null, message, "Invalid File", ERROR_MESSAGE)
console.println(message)
} else {
// Valid file is selected
console.println("The selected file is being imported.")
validFileSelected = true
}
// Dialog closed
} else if (result == JFileChooser.CANCEL_OPTION || result == JFileChooser.ERROR_OPTION) {
console.println("No file selected")
return
}
}
// Create timestamps
date = new Date()
bakFormat = new SimpleDateFormat("yyyyMMdd.HHmmss")
bakFormattedDate = bakFormat.format(date)
headerFormat = new SimpleDateFormat("'#'EEE MMM dd HH:mm:ss z yyyy")
headerFormat.setTimeZone(TimeZone.getTimeZone("CET"))
formattedDate = headerFormat.format(date)
// Create or backup creds file
if (credsFile.exists()) {
// make backup
backupFilePath = credsFile.getAbsolutePath() + "." + bakFormattedDate + ".bak"
backupFile = new File(backupFilePath)
backupFile.text = credsFile.text
console.println("Backing up the creds file as ${backupFilePath}.")
} else {
console.println("Creating the creds file in OmegaT config dir.")
credsFile.createNewFile() // empty
credsFile.text = formattedDate + "\n"
//credsFile.write(formattedDate)
}
// Collect lines from the credentials file and the selected file into arrays
credsFileLines = credsFile.text.tokenize("\n")
selectedFileLines = selectedFile.text.tokenize("\n")
// Remove comments in the selected file
selectedFileLines.removeIf { line ->
line.trim().startsWith("#")
}
// Collect URLs in the selected file; lines with these URLs will be deleted from the credentials lines before merging
linesToRemove = selectedFileLines.collect { normalizeLine(it) }
// Create a new array where the lines with the collected URLs are deleted
credsLinesToKeep = []
credsFileLines.each { line ->
normalizedLine = normalizeLine(line)
if (!linesToRemove.contains(normalizedLine)) {
credsLinesToKeep.add(line)
}
}
// Combine contents of OmegaT creds file and the selected file, dedupe and write
mergedCreds = (credsLinesToKeep + selectedFileLines).unique()
credsContents = new StringWriter()
credsContents << mergedCreds.join("\n")
credsFile.text = credsContents.toString()
// Mark selected file as done
doneFilePath = selectedFile.getAbsolutePath() + ".done"
doneFile = new File(doneFilePath)
selectedFile.renameTo(doneFile)
console.println("Credentials imported from $selectedFile")
return