From 9ddbd0949c5d5049dbb702ec8e87a15ec6632b95 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sun, 12 Jun 2016 04:44:51 -0400 Subject: [PATCH] Rename to AppError and Main.hs code reorganization --- app/Main.hs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 9d3f03e..c5c87fd 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -28,24 +28,31 @@ data Options = Options , oFromStdIn :: Bool } +data AppError + = TagError TagSearchOutcome + | InvalidConfigError [ParseConfigError] + main :: IO () -main = runProgram =<< execParser (withInfo parseOptions pHeader pDescription pFooter) +main = runProgram =<< parseCLI where runProgram options = withRuntime $ runExceptT (run options) >>= either renderError return + +parseCLI :: IO Options +parseCLI = + execParser (withInfo parseOptions pHeader pDescription pFooter) + where pHeader = "Unused: Analyze potentially unused code" pDescription = "Unused allows a developer to leverage an existing tags file\ \ (located at .git/tags, tags, or tmp/tags) to identify tokens\ \ in a codebase that are unused." pFooter = "CLI USAGE: $ unused" -data LocalizedError = TagError TagSearchOutcome | InvalidConfigError [ParseConfigError] - -renderError :: LocalizedError -> IO () +renderError :: AppError -> IO () renderError (TagError e) = V.missingTagsFileError e renderError (InvalidConfigError e) = V.invalidConfigError e -run :: Options -> ExceptT LocalizedError IO () +run :: Options -> ExceptT AppError IO () run options = do terms' <- withException TagError $ calculateTagInput options languageConfig <- withException InvalidConfigError loadAllConfigurations @@ -76,10 +83,6 @@ withCache :: Options -> IO SearchResults -> IO SearchResults withCache Options{ oWithoutCache = True } = id withCache Options{ oWithoutCache = False } = fmap SearchResults . cached "term-matches" . fmap fromResults -withInfo :: Parser a -> String -> String -> String -> ParserInfo a -withInfo opts h d f = - info (helper <*> opts) $ header h <> progDesc d <> footer f - optionFilters :: Options -> (TermMatchSet -> TermMatchSet) optionFilters o = foldl1 (.) filters @@ -94,6 +97,10 @@ optionFilters o = | null (oLikelihoods o) = [High] | otherwise = oLikelihoods o +withInfo :: Parser a -> String -> String -> String -> ParserInfo a +withInfo opts h d f = + info (helper <*> opts) $ header h <> progDesc d <> footer f + parseOptions :: Parser Options parseOptions = Options