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 all commits
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
20 changes: 15 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,18 @@ 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)

let rulesListWithDbRules =
if useDefaultBadPracticeRules pluginOpts
then badPracticeRules <> dbRulesList
else dbRulesList

rulesList' <- case parsedRulesYaml of
Left err -> do
when failOnFileNotFoundV $ addErr (mkInvalidYamlFileErr (show err))
Expand Down
42 changes: 28 additions & 14 deletions sheriff/src/Sheriff/Types.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
module Sheriff.Types where

import Data.Aeson as A
Expand All @@ -10,6 +11,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 @@ -24,27 +29,36 @@ data PluginOpts = PluginOpts {
logDebugInfo :: Bool,
logWarnInfo :: Bool,
logTypeDebugging :: Bool,
useIOForSourceCode :: Bool
useIOForSourceCode :: Bool,
useDefaultBadPracticeRules :: Bool
} deriving (Show, Eq)

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 = fetchValueFromEnvString ".juspay/tmp/sheriff/" "SHERIFF_SAVE_PATH",
indexedKeysPath = fetchValueFromEnvString ".juspay/indexedKeys.yaml" "SHERIFF_INDEXED_KEYS",
rulesConfigPath = fetchValueFromEnvString ".juspay/sheriffRules.yaml" "SHERIFF_RULES_CONFIG",
exceptionsConfigPath = fetchValueFromEnvString ".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",
useDefaultBadPracticeRules = fetchValueFromEnv True "SHERIFF_USE_DEFAULT_BAD_PRACTICE_RULES"
}

fetchValueFromEnvString :: String -> String -> String
fetchValueFromEnvString defaultValue env =
Maybe.fromMaybe defaultValue $ SIU.unsafePerformIO $ SE.lookupEnv env

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