From 34e66b3ab80fb201f49998ab46bb7a35370012c0 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sun, 14 Nov 2021 02:27:36 +0100 Subject: too much type level stuff to read a config file This got kinda out of hand, but it can now (a) read a json config file and (b) patch that with another json given on the command line to change some of the options given in the file. No, I probably didn't need to make the `patch` function sufficiently general to work with arbitrary records, but it was kinda fun to do. --- src/Main.hs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index 9fefd82..5dcf13c 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,7 +6,7 @@ module Main where -import Data.Aeson (encode) +import Data.Aeson (eitherDecode, encode) import Data.Aeson.Encode.Pretty (encodePretty) import qualified Data.ByteString.Char8 as C8 import qualified Data.ByteString.Lazy as LB @@ -14,10 +14,12 @@ import Data.Maybe (fromMaybe) import WithCli import CheckDir (recursiveCheckDir) -import WriteRepo (writeAdjustedRepository) +import Control.Monad.Identity (Identity) +import LintConfig (LintConfig (..), patch) +import System.Exit (exitWith) import Types (Level (..)) import Util (printPretty) -import System.Exit (exitWith) +import WriteRepo (writeAdjustedRepository) -- | the options this cli tool can take data Options = Options @@ -34,6 +36,8 @@ data Options = Options , pretty :: Bool -- ^ pretty-print the json to make it human-readable , out :: Maybe String + , config :: Maybe (LintConfig Maybe) + , configFile :: Maybe FilePath } deriving (Show, Generic, HasArguments) @@ -45,6 +49,18 @@ run options = do let repo = fromMaybe "." (repository options) let entry = fromMaybe "main.json" (entrypoint options) let level = fromMaybe Suggestion (lintlevel options) + print (config options) + + lintconfig <- case configFile options of + Nothing -> error "Need a config file!" + Just path -> LB.readFile path >>= \res -> + case eitherDecode res :: Either String (LintConfig Identity) of + Left err -> error $ "config file invalid: " <> err + Right file -> case config options of + Just p -> pure (patch file p) + Nothing -> pure file + + print lintconfig lints <- recursiveCheckDir repo entry -- cgit v1.2.3