Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add env support for defaultPluginOpts #21

Open
wants to merge 2 commits into
base: rdp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions sheriff/src/Sheriff/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ import GHC.Hs.Lit (HsLit(..))
import TcEvidence
import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T
import qualified System.Environment as SE
import qualified System.IO.Unsafe as SIU
import qualified Text.Read as TR
import qualified Data.Maybe as Maybe

plugin :: Plugin
plugin = defaultPlugin {
Expand Down Expand Up @@ -167,12 +171,19 @@ sheriff opts modSummary tcEnv = do
parsedExceptionsYaml <- liftIO $ parseYAMLFile sheriffExceptionsPath

-- Check the parsed yaml file for indexedDbKeys and throw compilation error if configured
rulesListWithDbRules <- case parsedYaml of
Left err -> do
when failOnFileNotFoundV $ addErr (mkInvalidYamlFileErr (show err))
pure badPracticeRules
Right (YamlTables tables) -> pure $ badPracticeRules <> (map yamlToDbRule tables)
dbRulesList <-
case parsedYaml of
Left err -> do
when failOnFileNotFoundV $ addErr (mkInvalidYamlFileErr (show err))
pure []
Right (YamlTables tables) -> pure $ (map yamlToDbRule tables)

rulesListWithDbRules <- do
addbadPracticeRules <- liftIO $ fromMaybe True . (>>= TR.readMaybe) <$> SE.lookupEnv "SHERIFF_ADD_BAD_PRACTICE_RULES"
case addbadPracticeRules of
True -> return $ badPracticeRules <> dbRulesList
False -> return dbRulesList

rulesList' <- case parsedRulesYaml of
Left err -> do
when failOnFileNotFoundV $ addErr (mkInvalidYamlFileErr (show err))
Expand Down
33 changes: 20 additions & 13 deletions sheriff/src/Sheriff/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Data.Data (Data)
import GHC.Hs.Dump
import Language.Haskell.GHC.ExactPrint (exactPrint)
import Language.Haskell.GHC.ExactPrint.Annotater (Annotate)
import qualified System.Environment as SE
import qualified System.IO.Unsafe as SIU
import qualified Text.Read as TR
import qualified Data.Maybe as Maybe

data PluginOpts = PluginOpts {
saveToFile :: Bool,
Expand All @@ -30,21 +34,24 @@ data PluginOpts = PluginOpts {
defaultPluginOpts :: PluginOpts
defaultPluginOpts =
PluginOpts {
saveToFile = False,
throwCompilationError = True,
failOnFileNotFound = True,
matchAllInsideAnd = False,
savePath = ".juspay/tmp/sheriff/",
indexedKeysPath = ".juspay/indexedKeys.yaml" ,
rulesConfigPath = ".juspay/sheriffRules.yaml",
exceptionsConfigPath = ".juspay/sheriffExceptionRules.yaml",
logDebugInfo = False,
logWarnInfo = True,
logTypeDebugging = False,
shouldCheckExceptions = True,
useIOForSourceCode = False
saveToFile = fetchValueFromEnv False "SHERIFF_SAVE_TO_FILE",
throwCompilationError = fetchValueFromEnv True "SHERIFF_THROW_COMPILATION_ERROR",
failOnFileNotFound = fetchValueFromEnv True "SHERIFF_FAIL_ON_FILE_NOT_FOUND",
matchAllInsideAnd = fetchValueFromEnv False "SHERIFF_MATCH_ALL_INSIDE_AND",
savePath = fetchValueFromEnv ".juspay/tmp/sheriff/" "SHERIFF_SAVE_PATH",
indexedKeysPath = fetchValueFromEnv ".juspay/indexedKeys.yaml" "SHERIFF_INDEXED_KEYS",
rulesConfigPath = fetchValueFromEnv ".juspay/sheriffRules.yaml" "SHERIFF_RULES_CONFIG",
exceptionsConfigPath = fetchValueFromEnv ".juspay/sheriffExceptionRules.yaml" "SHERIFF_EXCEPTION_CONFIG",
logDebugInfo = fetchValueFromEnv False "SHERIFF_LOG_DEBUG",
logWarnInfo = fetchValueFromEnv True "SHERIFF_LOG_WARN",
logTypeDebugging = fetchValueFromEnv False "SHERIFF_LOG_TYPE_DEBUGGING",
shouldCheckExceptions = fetchValueFromEnv True "SHERIFF_CHECk_EXCEPTIONS",
useIOForSourceCode = fetchValueFromEnv False "SHERIFF_USE_IO_FOR_SOURCE_CODE"
}

fetchValueFromEnv :: (Read a) => a -> String -> a
fetchValueFromEnv defaultValue env = Maybe.fromMaybe defaultValue . (>>= TR.readMaybe) $ SIU.unsafePerformIO $ SE.lookupEnv env

instance FromJSON PluginOpts where
parseJSON = withObject "PluginOpts" $ \o -> do
saveToFile <- o .:? "saveToFile" .!= (saveToFile defaultPluginOpts)
Expand Down