aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 45a6a0a97145d97902060081514cbff01023ccd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module Main
  ( main
  ) where

import Data.ByteString.Char8 (pack)
import Data.Version (showVersion)
import Database.MySQL.Base (ConnectInfo(..))
import Database.MySQL.Base.Types (Option(ReadDefaultFile, ReadDefaultGroup))
import Paths_juandelacosa (getDataDir, version) -- from cabal
import System.IO.Unsafe (unsafePerformIO)

import Options.Applicative
  ( Parser
  , (<**>)
  , (<|>)
  , auto
  , execParser
  , fullDesc
  , header
  , help
  , helper
  , info
  , long
  , metavar
  , option
  , optional
  , short
  , showDefault
  , strOption
  , value
  )

import Server (Listen(Port, Socket), server)

data Config =
  Config
    { file :: Maybe FilePath
    , group :: String
    , datadir :: FilePath
    , listen :: Listen
    }

parseListen :: Parser Listen
parseListen = port <|> socket
  where
    port =
      Port <$>
      option
        auto
        (long "port" <>
         short 'p' <>
         metavar "INT" <> help "listen on this TCP port (localhost only)")
    socket =
      Socket <$>
      option
        auto
        (long "socket" <>
         short 's' <>
         metavar "PATH" <>
         value "/tmp/juandelacosa.sock" <>
         showDefault <> help "Listen on this UNIX-socket")

{-# NOINLINE dataDir #-}
dataDir :: FilePath
dataDir = unsafePerformIO getDataDir

parseConfig :: Parser Config
parseConfig =
  Config <$>
  optional
    (strOption
       (long "file" <>
        short 'f' <> metavar "FILE" <> help "Read this MySQL client config file")) <*>
  strOption
    (long "group" <>
     short 'g' <>
     metavar "STRING" <>
     value "client" <>
     showDefault <> help "Read this options group in the above file") <*>
  strOption
    (long "datadir" <>
     short 'd' <>
     metavar "DIR" <>
     value dataDir <>
     showDefault <> help "Data directory including static files") <*>
  parseListen

run :: Config -> IO ()
run cfg = do
  let myInfo =
        ConnectInfo
          { connectDatabase = ""
          , connectHost = ""
          , connectOptions =
              case file cfg of
                Nothing -> []
                Just f ->
                  [ReadDefaultFile f, ReadDefaultGroup (pack $ group cfg)]
          , connectPassword = ""
          , connectPath = ""
          , connectPort = 0
          , connectSSL = Nothing
          , connectUser = ""
          }
  server (listen cfg) myInfo (datadir cfg)

main :: IO ()
main = run =<< execParser opts
  where
    opts = info (parseConfig <**> helper) (fullDesc <> header desc)
    desc =
      "juandelacosa " ++
      showVersion version ++ " - manage MariaDB user and roles"