From c769c4126b3a9bd2a6fd814a801b66513d4bab51 Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Sun, 9 Apr 2017 00:25:04 +0800 Subject: [PATCH] Polish initial PGP key error messages --- pass/Models/PasswordStore.swift | 34 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index ee782ef..ff05a3b 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -220,27 +220,21 @@ class PasswordStore { } public func initPGPKey(_ keyType: PGPKeyType) throws { - var keyPath = "" switch keyType { case .public: - keyPath = Globals.pgpPublicKeyPath - case .secret: - keyPath = Globals.pgpPrivateKeyPath - default: - throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."]) - } - - if let key = importKey(from: keyPath) { - switch keyType { - case .public: - self.publicKey = key - case .secret: - self.privateKey = key - default: - throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."]) + let keyPath = Globals.pgpPublicKeyPath + self.publicKey = importKey(from: keyPath) + if self.publicKey == nil { + throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the public PGP key."]) } - } else { - throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."]) + case .secret: + let keyPath = Globals.pgpPrivateKeyPath + self.privateKey = importKey(from: keyPath) + if self.privateKey == nil { + throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the private PGP key."]) + } + default: + throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key: unknown PGP key type."]) } } @@ -272,9 +266,7 @@ class PasswordStore { let fm = FileManager.default if fm.fileExists(atPath: keyPath) { if let keys = pgp.importKeys(fromFile: keyPath, allowDuplicates: false) as? [PGPKey] { - if keys.count > 0 { - return keys[0] - } + return keys.first } } return nil