summaryrefslogtreecommitdiffhomepage
path: root/CmdLine.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-16 14:58:16 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-16 14:58:16 -0400
commitfccf788a5ce9788d7c073321a3d19941bc1269b1 (patch)
tree76726eb3d3cd6fbb05721e5862e87511d1683b76 /CmdLine.hs
parentc9c476ae7216b80932b80870a2cd06f9339306aa (diff)
downloadkeysafe-fccf788a5ce9788d7c073321a3d19941bc1269b1.tar.gz
more command line interface improvements
Diffstat (limited to 'CmdLine.hs')
-rw-r--r--CmdLine.hs20
1 files changed, 17 insertions, 3 deletions
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