summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-17 15:29:34 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-17 15:29:34 -0400
commitefee99a335b36ae6265662f3afd7fbfaf75420d2 (patch)
tree3cd7d329d61bca02be08eaea87745af698ea6663
parent194d7d2702712668f05ea2ac956a0dcecc5c4b70 (diff)
downloadkeysafe-efee99a335b36ae6265662f3afd7fbfaf75420d2.tar.gz
refactor
-rw-r--r--Gpg.hs22
-rw-r--r--keysafe.hs23
2 files changed, 24 insertions, 21 deletions
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"] ""
diff --git a/keysafe.hs b/keysafe.hs
index c88668e..7756b26 100644
--- a/keysafe.hs
+++ b/keysafe.hs
@@ -58,11 +58,11 @@ dispatch cmdline ui tunables = do
backup storage ui tunables secretkeysource
=<< getSecretKey secretkeysource
go CmdLine.Backup Nothing =
- backup storage ui tunables anyGpgKey =<< pickGpgKeyToBackup ui
+ backup storage ui tunables Gpg.anyKey =<< Gpg.getKeyToBackup ui
go CmdLine.Restore (Just secretkeydest) =
restore storage ui secretkeydest
go CmdLine.Restore Nothing =
- restore storage ui anyGpgKey
+ restore storage ui Gpg.anyKey
go CmdLine.Benchmark _ =
benchmarkTunables tunables
@@ -70,25 +70,6 @@ getSecretKey :: SecretKeySource -> IO SecretKey
getSecretKey (GpgKey kid) = Gpg.getSecretKey kid
getSecretKey (KeyFile f) = SecretKey <$> B.readFile f
--- | 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.
-pickGpgKeyToBackup :: UI -> IO SecretKey
-pickGpgKeyToBackup 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.
-anyGpgKey :: SecretKeySource
-anyGpgKey = GpgKey (KeyId "")
-
backup :: Storage -> UI -> Tunables -> SecretKeySource -> SecretKey -> IO ()
backup storage ui tunables secretkeysource secretkey = do
username <- userName