mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-10-26 13:19:58 +03:00
Rename to AppError and Main.hs code reorganization
This commit is contained in:
parent
07c0fb0d3f
commit
9ddbd0949c
25
app/Main.hs
25
app/Main.hs
@ -28,24 +28,31 @@ data Options = Options
|
|||||||
, oFromStdIn :: Bool
|
, oFromStdIn :: Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data AppError
|
||||||
|
= TagError TagSearchOutcome
|
||||||
|
| InvalidConfigError [ParseConfigError]
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = runProgram =<< execParser (withInfo parseOptions pHeader pDescription pFooter)
|
main = runProgram =<< parseCLI
|
||||||
where
|
where
|
||||||
runProgram options = withRuntime $
|
runProgram options = withRuntime $
|
||||||
runExceptT (run options) >>= either renderError return
|
runExceptT (run options) >>= either renderError return
|
||||||
|
|
||||||
|
parseCLI :: IO Options
|
||||||
|
parseCLI =
|
||||||
|
execParser (withInfo parseOptions pHeader pDescription pFooter)
|
||||||
|
where
|
||||||
pHeader = "Unused: Analyze potentially unused code"
|
pHeader = "Unused: Analyze potentially unused code"
|
||||||
pDescription = "Unused allows a developer to leverage an existing tags file\
|
pDescription = "Unused allows a developer to leverage an existing tags file\
|
||||||
\ (located at .git/tags, tags, or tmp/tags) to identify tokens\
|
\ (located at .git/tags, tags, or tmp/tags) to identify tokens\
|
||||||
\ in a codebase that are unused."
|
\ in a codebase that are unused."
|
||||||
pFooter = "CLI USAGE: $ unused"
|
pFooter = "CLI USAGE: $ unused"
|
||||||
|
|
||||||
data LocalizedError = TagError TagSearchOutcome | InvalidConfigError [ParseConfigError]
|
renderError :: AppError -> IO ()
|
||||||
|
|
||||||
renderError :: LocalizedError -> IO ()
|
|
||||||
renderError (TagError e) = V.missingTagsFileError e
|
renderError (TagError e) = V.missingTagsFileError e
|
||||||
renderError (InvalidConfigError e) = V.invalidConfigError e
|
renderError (InvalidConfigError e) = V.invalidConfigError e
|
||||||
|
|
||||||
run :: Options -> ExceptT LocalizedError IO ()
|
run :: Options -> ExceptT AppError IO ()
|
||||||
run options = do
|
run options = do
|
||||||
terms' <- withException TagError $ calculateTagInput options
|
terms' <- withException TagError $ calculateTagInput options
|
||||||
languageConfig <- withException InvalidConfigError loadAllConfigurations
|
languageConfig <- withException InvalidConfigError loadAllConfigurations
|
||||||
@ -76,10 +83,6 @@ withCache :: Options -> IO SearchResults -> IO SearchResults
|
|||||||
withCache Options{ oWithoutCache = True } = id
|
withCache Options{ oWithoutCache = True } = id
|
||||||
withCache Options{ oWithoutCache = False } = fmap SearchResults . cached "term-matches" . fmap fromResults
|
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 :: Options -> (TermMatchSet -> TermMatchSet)
|
||||||
optionFilters o =
|
optionFilters o =
|
||||||
foldl1 (.) filters
|
foldl1 (.) filters
|
||||||
@ -94,6 +97,10 @@ optionFilters o =
|
|||||||
| null (oLikelihoods o) = [High]
|
| null (oLikelihoods o) = [High]
|
||||||
| otherwise = oLikelihoods o
|
| 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 :: Parser Options
|
||||||
parseOptions =
|
parseOptions =
|
||||||
Options
|
Options
|
||||||
|
Loading…
Reference in New Issue
Block a user