Check duplicated tags before parsing a file as yaml
This commit is contained in:
parent
f7eabd8258
commit
54c7acf651
2 changed files with 23 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ public enum AppError: Error {
|
|||
case GitResetError
|
||||
case PGPPublicKeyNotExistError
|
||||
case WrongPasswordFilename
|
||||
case YamlLoadError
|
||||
case UnknownError
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +37,8 @@ extension AppError: LocalizedError {
|
|||
return "PGP public key doesn't exist."
|
||||
case .WrongPasswordFilename:
|
||||
return "Cannot write to the password file."
|
||||
case .YamlLoadError:
|
||||
return "Cannot be parsed as a YAML file."
|
||||
case .UnknownError:
|
||||
return "Unknown error."
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,9 +126,29 @@ public class Password {
|
|||
// construct the otp token
|
||||
self.updateOtpToken()
|
||||
}
|
||||
|
||||
// check whether the file has lines with duplicated field names
|
||||
private func checkDuplicatedFields(lines: String) -> Bool {
|
||||
var keys = Set<String>()
|
||||
var hasDuplicatedFields = false
|
||||
lines.enumerateLines { (line, stop) -> () in
|
||||
let (key, _) = Password.getKeyValuePair(from: line)
|
||||
if let key = key {
|
||||
if keys.contains(key) {
|
||||
hasDuplicatedFields = true
|
||||
stop = true
|
||||
}
|
||||
keys.insert(key)
|
||||
}
|
||||
}
|
||||
return hasDuplicatedFields
|
||||
}
|
||||
|
||||
private func getAdditionalFields(fromYaml: String) throws {
|
||||
guard !fromYaml.isEmpty else { return }
|
||||
if checkDuplicatedFields(lines: fromYaml) {
|
||||
throw AppError.YamlLoadError
|
||||
}
|
||||
let yamlFile = try Yams.load(yaml: fromYaml) as! [String: Any]
|
||||
additions.append(contentsOf: yamlFile.map { ($0, String(describing: $1)) })
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue