~samhh/unerf

959581cac938088da3c542853b7878bd1aa76c97 — Sam A. Horvath-Hunt 3 months ago 331aa84
Signify presence of scripts with exit code

This also removes the output text when no scripts are found.
1 files changed, 17 insertions(+), 9 deletions(-)

M exec/Main.hs
M exec/Main.hs => exec/Main.hs +17 -9
@@ 1,23 1,31 @@
module Main (main) where

import           CLI                (Opts (..), parse)
import qualified Data.Map           as M
import qualified Data.Text          as T
import           CLI         (Opts (..), parse)
import qualified Data.Map    as M
import qualified Data.Text   as T
import           Prelude
import           Unerf              (checkAll, defaultPath)
import           Unerf.Tool         (Scripts, Tool)
import           Unerf       (checkAll, defaultPath)
import           Unerf.Tool  (Scripts, Tool)

-- | Isomorphic to `Maybe` but with distinct meaning.
data Result a
  = Output a
  | NoOutput

main :: IO ()
main = putTextLn =<< fmap (outputTools . M.toList) . checkAll =<< getPath
main = exit =<< fmap toOutput . checkAll =<< getPath
  where getPath = sanitisePath . fromMaybe defaultPath . path <$> parse
        toOutput = outputTools . M.toList
        exit NoOutput   = exitFailure
        exit (Output x) = putTextLn x

-- | Fixes inputs without a trailing slash, doesn't break those with one.
sanitisePath :: Text -> Text
sanitisePath = (<> "/")

outputTools :: [(Tool, Scripts)] -> Text
outputTools [] = "Nothing found."
outputTools xs = T.intercalate "\n" $ uncurry outputTool <$> xs
outputTools :: [(Tool, Scripts)] -> Result Text
outputTools [] = NoOutput
outputTools xs = Output . T.intercalate "\n" $ uncurry outputTool <$> xs

outputTool :: Tool -> Scripts -> Text
outputTool x ys = show x <> foldMap (outputScripts . toList) ys