From efee99a335b36ae6265662f3afd7fbfaf75420d2 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 17 Aug 2016 15:29:34 -0400 Subject: refactor --- Gpg.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'Gpg.hs') diff --git a/Gpg.hs b/Gpg.hs index 7002e16..4a0633a 100644 --- a/Gpg.hs +++ b/Gpg.hs @@ -3,9 +3,12 @@ - Licensed under the GNU AGPL version 3 or higher. -} +{-# LANGUAGE OverloadedStrings #-} + module Gpg where import Types +import UI import System.Process import Data.List.Split import Data.Maybe @@ -13,6 +16,25 @@ import System.Exit import qualified Data.ByteString as B import qualified Data.ByteString.UTF8 as BU8 +-- | Pick gpg secret key to back up. +-- +-- If there is only one gpg secret key, +-- the choice is obvious. Otherwise prompt the user with a list. +getKeyToBackup :: UI -> IO SecretKey +getKeyToBackup ui = go =<< Gpg.listSecretKeys + where + go [] = do + showError ui "You have no gpg secret keys to back up." + error "Aborting on no gpg secret keys." + go [(_, kid)] = Gpg.getSecretKey kid + go l = maybe (error "Canceled") Gpg.getSecretKey + =<< promptKeyId ui "Pick gpg secret key" + "Pick gpg secret key to back up:" l + +-- | Use when the gpg keyid will not be known at restore time. +anyKey :: SecretKeySource +anyKey = GpgKey (KeyId "") + listSecretKeys :: IO [(Name, KeyId)] listSecretKeys = mapMaybe parse . lines <$> readProcess "gpg" ["--batch", "--with-colons", "--list-secret-keys"] "" -- cgit v1.2.3