Do fuzzy search suggestion for missing secret.

This commit is contained in:
Dillon Kearns 2019-10-29 17:59:08 -07:00
parent d3a61d5146
commit 7d14d8de88
4 changed files with 35 additions and 5 deletions

View File

@ -32,7 +32,8 @@
"lukewestby/elm-string-interpolate": "1.0.4 <= v < 2.0.0",
"mdgriffith/elm-markup": "3.0.1 <= v < 4.0.0",
"miniBill/elm-codec": "1.2.0 <= v < 2.0.0",
"noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0"
"noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0",
"tripokey/elm-fuzzy": "5.2.1 <= v < 6.0.0"
},
"test-dependencies": {
"avh4/elm-program-test": "3.1.0 <= v < 4.0.0",

View File

@ -30,7 +30,8 @@
"mdgriffith/elm-ui": "1.1.5",
"miniBill/elm-codec": "1.2.0",
"noahzgordon/elm-color-extra": "1.0.2",
"rtfeldman/elm-hex": "1.0.0"
"rtfeldman/elm-hex": "1.0.0",
"tripokey/elm-fuzzy": "5.2.1"
},
"indirect": {
"elm/bytes": "1.0.8",

View File

@ -2,6 +2,7 @@ module Secrets exposing (..)
import BuildError exposing (BuildError)
import Dict exposing (Dict)
import Fuzzy
import Json.Decode as Decode exposing (Decoder)
import TerminalText as Terminal
@ -51,13 +52,22 @@ buildError secretName availableEnvironmentVariables =
[ Terminal.text "I expected to find this Secret in your environment variables but didn't find a match:\nSecrets.get \""
, Terminal.red (Terminal.text secretName)
, Terminal.text "\"\n\n"
-- , Terminal.text "Maybe you meant one of:\n"
-- , Terminal.text (String.join ", " availableEnvironmentVariables)
, Terminal.text "So maybe "
, Terminal.yellow <| Terminal.text (sortMatches secretName availableEnvironmentVariables |> List.head |> Maybe.withDefault "")
, Terminal.text " should be "
, Terminal.green <| Terminal.text secretName
]
}
sortMatches missingSecret availableSecrets =
let
simpleMatch config separators needle hay =
Fuzzy.match config separators needle hay |> .score
in
List.sortBy (simpleMatch [] [] missingSecret) availableSecrets
decoder : Decoder Secrets
decoder =
Decode.dict Decode.string

View File

@ -9,6 +9,8 @@ type Text
type Color
= Red
| Blue
| Green
| Yellow
text : String -> Text
@ -16,6 +18,16 @@ text value =
RawText value
green : Text -> Text
green inner =
Style (colorToString Green) inner
yellow : Text -> Text
yellow inner =
Style (colorToString Yellow) inner
red : Text -> Text
red inner =
Style (colorToString Red) inner
@ -49,6 +61,12 @@ colorToString color =
Blue ->
"[34m"
Green ->
"[32;1m"
Yellow ->
"[33;1m"
toString : List Text -> String
toString list =