summaryrefslogtreecommitdiff
path: root/Command/Import.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-02-08 15:04:58 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-02-08 15:04:58 -0400
commitd2770cfaa62a25fee2944af086530f58939ddfb5 (patch)
treebf597707871a598b31c3ab62a210c2b6450ab01d /Command/Import.hs
parent072b440e4dddd32c590726ec9610bb6796314a71 (diff)
better option handling
At least it avoids the big truth table lookup
Diffstat (limited to 'Command/Import.hs')
-rw-r--r--Command/Import.hs56
1 files changed, 26 insertions, 30 deletions
diff --git a/Command/Import.hs b/Command/Import.hs
index 305396c7f..025563b49 100644
--- a/Command/Import.hs
+++ b/Command/Import.hs
@@ -21,42 +21,38 @@ cmd = [withOptions opts $ notBareRepo $ command "import" paramPaths seek
SectionCommon "move and add files from outside git working copy"]
opts :: [Option]
-opts =
- [ duplicateOption
- , deduplicateOption
- , cleanDuplicatesOption
- , skipDuplicatesOption
- ] ++ fileMatchingOptions
+opts = duplicateModeOptions ++ fileMatchingOptions
-duplicateOption :: Option
-duplicateOption = flagOption [] "duplicate" "do not delete source files"
-
-deduplicateOption :: Option
-deduplicateOption = flagOption [] "deduplicate" "delete source files whose content was imported before"
+data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
+ deriving (Eq, Enum, Bounded)
-cleanDuplicatesOption :: Option
-cleanDuplicatesOption = flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
+associatedOption :: DuplicateMode -> Maybe Option
+associatedOption Default = Nothing
+associatedOption Duplicate = Just $
+ flagOption [] "duplicate" "do not delete source files"
+associatedOption DeDuplicate = Just $
+ flagOption [] "deduplicate" "delete source files whose content was imported before"
+associatedOption CleanDuplicates = Just $
+ flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
+associatedOption SkipDuplicates = Just $
+ flagOption [] "skip-duplicates" "import only new files"
-skipDuplicatesOption :: Option
-skipDuplicatesOption = flagOption [] "skip-duplicates" "import only new files"
-
-data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
- deriving (Eq)
+duplicateModeOptions :: [Option]
+duplicateModeOptions = mapMaybe associatedOption [minBound..maxBound]
getDuplicateMode :: Annex DuplicateMode
-getDuplicateMode = gen
- <$> getflag duplicateOption
- <*> getflag deduplicateOption
- <*> getflag cleanDuplicatesOption
- <*> getflag skipDuplicatesOption
+getDuplicateMode = go . catMaybes <$> mapM getflag [minBound..maxBound]
where
- getflag = Annex.getFlag . optionName
- gen False False False False = Default
- gen True False False False = Duplicate
- gen False True False False = DeDuplicate
- gen False False True False = CleanDuplicates
- gen False False False True = SkipDuplicates
- gen _ _ _ _ = error "bad combination of --duplicate, --deduplicate, --clean-duplicates, --skip-duplicates"
+ getflag m = case associatedOption m of
+ Nothing -> return Nothing
+ Just o -> ifM (Annex.getFlag (optionName o))
+ ( return (Just m)
+ , return Nothing
+ )
+ go [] = Default
+ go [m] = m
+ go ms = error $ "cannot combine " ++
+ unwords (map (optionParam . fromJust . associatedOption) ms)
seek :: CommandSeek
seek ps = do