Last active
August 29, 2015 14:14
-
-
Save spencerwi/bba4512e58d10b51aa88 to your computer and use it in GitHub Desktop.
Convert Java properties to environment variables (naively)
This file contains hidden or 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
#!/usr/bin/env runhaskell | |
module JavaPropsToEnvVars where | |
import Data.List (break, stripPrefix) | |
type PropertyName = String | |
type PropertyValue = String | |
type Property = (PropertyName, PropertyValue) | |
containsProperty :: String -> Bool | |
containsProperty = elem '=' | |
propertyFromString :: String -> Property | |
propertyFromString = (\(name, val) -> (name, (dropWhile ((==) '=') val))) . break ((==) '=') | |
sanitizeProperty :: Property -> String | |
sanitizeProperty (name, val) = (map sanitizeDots name) ++ "=" ++ (quoteStringIfNeeded val) | |
where sanitizeDots '.' = '_' | |
sanitizeDots c = c | |
quoteStringIfNeeded val | |
| (head val) == '"' = val | |
| otherwise = "\"" ++ val ++ "\"" | |
convertJavaPropsToEnvironmentVars :: String -> String | |
convertJavaPropsToEnvironmentVars = unlines . (map sanitizeProperty) . (map propertyFromString) . (filter containsProperty) . lines | |
main = interact convertJavaPropsToEnvironmentVars | |
-- vim:ft=haskell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment