~samhh/unerf

ref: 959581cac938088da3c542853b7878bd1aa76c97 unerf/exec/Main.hs -rw-r--r-- 1.1 KiB
959581ca — Sam A. Horvath-Hunt Signify presence of scripts with exit code 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Main (main) where

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)

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

main :: IO ()
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)] -> 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

outputScripts :: [Text] -> Text
outputScripts = (": " <>) . T.intercalate ", "