Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Last active June 17, 2020 01:50
Show Gist options
  • Save mgreenly/20987037935ce40ec62cfa86b7d3a230 to your computer and use it in GitHub Desktop.
Save mgreenly/20987037935ce40ec62cfa86b7d3a230 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RecordWildCards #-}
import Data.Maybe
import Data.List
-- this defines the data type that will be read from the config file, all of the values are optional
data Proxy = Proxy
{ proxyKey :: Maybe String
, proxyUser :: Maybe String
, proxyHost :: Maybe String
, proxyPort :: Maybe Int
} deriving Show
-- the mock config, the data we actually load
config = Proxy
{ proxyKey = Just "/home/.ssh/id_rsa"
, proxyUser = Just "bob"
, proxyHost = Just "example.com"
, proxyPort = Just 8080
}
-- the actual function
proxyCommand :: Proxy -> Maybe String
proxyCommand Proxy{proxyHost=Nothing} = Nothing
proxyCommand Proxy{..} = Just $
intercalate " " ["ssh" ++ key, "-w %h:%p", ("ssh://" ++ user ++ host ++ port)]
where
host = fromJust proxyHost
key = fromMaybe "" $ (" -i"++) <$> proxyKey
user = fromMaybe "" $ (++"@") <$> proxyUser
port = fromMaybe "" $ ((":"++).show) <$> proxyPort
-- convert the config data into the command and display it
main = do
putStrLn $ show $ proxyCommand config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment