-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from roman/v0.0.0.2
v0.0.0.2
- Loading branch information
Showing
14 changed files
with
248 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.stack-work/ | ||
.#* | ||
etc-spec/.tasty-rerun-log | ||
.tasty-rerun-log |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module System.Etc.Internal.Extra.EnvMisspell ( | ||
EnvMisspell (..) | ||
, getEnvMisspellings | ||
, getEnvMisspellingsPure | ||
, renderEnvMisspellings | ||
, hPrintEnvMisspellings | ||
, reportEnvMisspellingWarnings | ||
) where | ||
|
||
import Protolude hiding ((<$>), (<>)) | ||
|
||
import Data.Vector (Vector) | ||
import System.Environment (getEnvironment) | ||
|
||
import qualified Data.HashMap.Strict as HashMap | ||
import qualified Data.Text as Text | ||
import qualified Data.Vector as Vector | ||
import qualified Text.EditDistance as Distance | ||
|
||
import System.Etc.Internal.Spec.Types | ||
import Text.PrettyPrint.ANSI.Leijen | ||
|
||
data EnvMisspell | ||
= EnvMisspell { | ||
currentText :: Text | ||
, suggestionText :: Text | ||
} | ||
deriving (Show, Eq, Generic) | ||
|
||
lookupSpecEnvKeys :: ConfigSpec a -> Vector Text | ||
lookupSpecEnvKeys spec = | ||
let | ||
foldEnvSettings val acc = | ||
case val of | ||
ConfigValue _ sources -> | ||
maybe acc (`Vector.cons` acc) (envVar sources) | ||
SubConfig hsh -> | ||
HashMap.foldr foldEnvSettings acc hsh | ||
in | ||
foldEnvSettings (SubConfig $ specConfigValues spec) Vector.empty | ||
|
||
{-| | ||
-} | ||
getEnvMisspellingsPure :: ConfigSpec a -> Vector Text -> Vector EnvMisspell | ||
getEnvMisspellingsPure spec env = do | ||
specEnvName <- lookupSpecEnvKeys spec | ||
currentEnvName <- env | ||
|
||
let | ||
distance = | ||
Distance.levenshteinDistance | ||
Distance.defaultEditCosts | ||
(Text.unpack specEnvName) | ||
(Text.unpack currentEnvName) | ||
|
||
guard (distance >= 1 && distance < 4) | ||
return $ EnvMisspell currentEnvName specEnvName | ||
|
||
{-| | ||
-} | ||
getEnvMisspellings :: ConfigSpec a -> IO (Vector EnvMisspell) | ||
getEnvMisspellings spec = | ||
getEnvironment | ||
& fmap (Vector.fromList . map (Text.pack . fst)) | ||
& fmap (getEnvMisspellingsPure spec) | ||
|
||
{-| | ||
-} | ||
renderEnvMisspellings :: Vector EnvMisspell -> Doc | ||
renderEnvMisspellings misspells | ||
| Vector.null misspells = | ||
mempty | ||
| otherwise = | ||
misspells | ||
& Vector.map | ||
(\misspell -> | ||
text "WARNING: Environment variable `" | ||
<> text (Text.unpack $ currentText misspell) | ||
<> text "' found, perhaps you meant `" | ||
<> text (Text.unpack $ suggestionText misspell) | ||
<> text "'") | ||
& Vector.foldl' (<$>) mempty | ||
& (<$> mempty) | ||
& (<$> mempty) | ||
|
||
{-| | ||
-} | ||
hPrintEnvMisspellings :: Handle -> Vector EnvMisspell -> IO () | ||
hPrintEnvMisspellings h = | ||
hPutDoc h . renderEnvMisspellings | ||
|
||
{-| | ||
-} | ||
reportEnvMisspellingWarnings :: ConfigSpec a -> IO () | ||
reportEnvMisspellingWarnings spec = | ||
getEnvMisspellings spec >>= | ||
hPrintEnvMisspellings stderr |
2 changes: 1 addition & 1 deletion
2
etc/src/System/Etc/Internal/Printer.hs → etc/src/System/Etc/Internal/Extra/Printer.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedLists #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
module System.Etc.Extra.EnvMisspellTest where | ||
|
||
import Protolude | ||
|
||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.HUnit (assertBool, assertEqual, testCase) | ||
|
||
import qualified Data.Vector as Vector | ||
|
||
import System.Etc | ||
|
||
tests :: TestTree | ||
tests = | ||
testGroup "env misspells" | ||
[ | ||
testCase "it warns when misspell is present" $ do | ||
let | ||
input = | ||
mconcat | ||
[ | ||
"{\"etc/entries\": {" | ||
, " \"greeting\": { \"etc/spec\": { \"env\": \"GREETING\" }}}}" | ||
] | ||
|
||
(spec :: ConfigSpec ()) <- parseConfigSpec input | ||
|
||
let | ||
result = | ||
getEnvMisspellingsPure spec ["GREEING"] | ||
|
||
assertBool "expecting to get a warning for typo" | ||
(not $ Vector.null result) | ||
|
||
assertEqual "expecting to get typo for key GREETING" | ||
(EnvMisspell "GREEING" "GREETING") | ||
(Vector.head result) | ||
] |
Oops, something went wrong.