From fccf788a5ce9788d7c073321a3d19941bc1269b1 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 16 Aug 2016 14:58:16 -0400 Subject: more command line interface improvements --- CmdLine.hs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'CmdLine.hs') diff --git a/CmdLine.hs b/CmdLine.hs index ca574bb..6413cf7 100644 --- a/CmdLine.hs +++ b/CmdLine.hs @@ -3,14 +3,16 @@ - Licensed under the GNU AGPL version 3 or higher. -} -module CmdLine (CmdLine(..), Mode(..), get, parse) where +module CmdLine (CmdLine(..), Mode(..), get, parse, selectMode) where import Types +import qualified Gpg import Options.Applicative import qualified Data.ByteString.UTF8 as BU8 +import System.Directory data CmdLine = CmdLine - { mode :: Mode + { mode :: Maybe Mode , secretkeysource :: Maybe SecretKeySource , testMode :: Bool , gui :: Bool @@ -22,7 +24,7 @@ data Mode = Backup | Restore | Benchmark parse :: Parser CmdLine parse = CmdLine - <$> (backup <|> restore <|> benchmark) + <$> optional (backup <|> restore <|> benchmark) <*> optional (gpgswitch <|> fileswitch) <*> testmodeswitch <*> guiswitch @@ -63,3 +65,15 @@ get = execParser opts ( fullDesc <> header "keysafe - securely back up secret keys" ) + +-- | When a mode is not specified on the command line, +-- default to backing up if a secret key exists, and otherwise restoring. +selectMode :: CmdLine -> IO Mode +selectMode cmdline = case mode cmdline of + Just m -> return m + Nothing -> case secretkeysource cmdline of + Just (KeyFile f) -> present <$> doesFileExist f + _ -> present . not . null <$> Gpg.listSecretKeys + where + present True = Backup + present False = Restore -- cgit v1.2.3