aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 58ae99fff4dfce3ed2fd6f33bde4d09dfd7d9f0d (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
{-# LANGUAGE QuasiQuotes #-}

module Main (
  main
) where

import Data.ByteString.Char8 (pack)
import Data.Maybe (fromJust)
import Data.Version (showVersion)
import Database.MySQL.Base (ConnectInfo(..))
import Database.MySQL.Base.Types (Option(ReadDefaultFile, ReadDefaultGroup))
import Paths_juandelacosa (version) -- from cabal
import System.Environment (getArgs)
import Text.RawString.QQ (r)
import qualified System.Console.Docopt.NoTH as O

import Server (server)

usage :: String
usage =  "juandelacosa " ++ showVersion version
  ++ " manage MariaDB user and roles" ++ [r|

Usage:
  juandelacosa [options]

Options:
  -f, --file=MYCNF         Read this MySQL client config file
  -g, --group=GROUP        Read this options group in the above file [default: client]

  -s, --socket=SOCK        Listen on this UNIX-socket [default: /tmp/juandelacosa.sock]
  -p, --port=PORT          Instead of UNIX-socket, listen on this TCP port (localhost)

  -h, --help               Show this message

|]

main :: IO()
main = do
  doco <- O.parseUsageOrExit usage
  args <- O.parseArgsOrExit doco =<< getArgs
  if args `O.isPresent` O.longOption "help"
  then putStrLn $ O.usage doco
  else do
    let
      file = O.getArg args $ O.longOption "file"
      group = fromJust $ O.getArg args $ O.longOption "group"
      port = O.getArg args $ O.longOption "port"
      socket = fromJust $ O.getArg args $ O.longOption "socket"
    -- XXX: mysql package maps empty strings to NULL
    -- which is what we need, see documentation for mysql_real_connect()
    let myInfo = ConnectInfo {
        connectDatabase = "",
        connectHost     = "",
        connectOptions  = case file of
                          Nothing -> []
                          Just f -> [ ReadDefaultFile f, ReadDefaultGroup (pack group) ],
        connectPassword = "",
        connectPath     = "",
        connectPort     = 0,
        connectSSL      = Nothing,
        connectUser     = ""
      }
    let listen = case port of
          Nothing -> Right socket
          Just p -> Left $ read p
    server listen myInfo