TSFoster / elm-envfile / Envfile

Convert Dict String String to envfile format and back.

Note: This parser follows the simple rules laid out in Docker's guide to its use of envfiles.

Encode

encode : Dict String String -> String

Take a Dict String String, ignore the keys that bash wouldn’t interpret as a variable name and encode the rest as variable declarations, one on each line.

import Dict

[ ("hello", "world")
, ("_john", "_WAYNE")
, ("234notavar", "Won’t be encoded")
, ("also not a var", "Also won’t be encoded")
, ("thing1", "thing\n2")
]
    |> Dict.fromList
    |> encode
    |> String.lines
    |> List.sort
--> [ "_john=_WAYNE"
--> , "hello=world"
--> , "thing1=thing\\n2"
--> ]

Parse

parser : Parser (Dict String String)

A Parser to read an envfile as a Dict String String.

import Parser
import Dict

Parser.run parser <| String.join "\n"
    [ "hello=world"
    , "# this is a comment"
    , ""
    , ""
    , "_abc=def\\nghi"
    ]
--> Ok (Dict.fromList [ ("_abc", "def\nghi") , ("hello", "world") ])