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

module Main (
  main
) where

import Data.ByteString.Char8 (pack)
import Data.Either.Utils (forceEither)
import Data.Maybe (fromJust)
import Data.Version (showVersion)
import Database.MySQL.Base (ConnectInfo(..))
import Database.MySQL.Base.Types (Option(ReadDefaultFile, ReadDefaultGroup))
import Paths_mywatch (getDataDir, version) -- from cabal
import System.Environment (getArgs)
import Text.InterpolatedString.Perl6 (qc)
import qualified Data.ConfigFile as Cf
import qualified System.Console.Docopt.NoTH as O

import Server (server)

usage :: IO String
usage = do
  dataDir <- getDataDir
  return $
    "mywatch " ++ showVersion version
    ++ " view queries on many MySQL servers" ++ [qc|

Usage:
  mywatch [options] MYCNF

Options:

  -d, --datadir=DIR        Data directory including static files [default: {dataDir}]

  -s, --socket=SOCK        Listen on this UNIX-socket [default: /tmp/mywatch.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 = fromJust $ O.getArg args $ O.argument "MYCNF"
      port = O.getArg args $ O.longOption "port"
      socket = fromJust $ O.getArg args $ O.longOption "socket"
      datadir = fromJust $ O.getArg args $ O.longOption "datadir"
    servers <- filter ("client" /=) . Cf.sections . forceEither <$> Cf.readfile Cf.emptyCP file
    let
      myInfo = map (\g -> ConnectInfo {
        connectDatabase = "",
        connectHost     = "",
        connectPassword = "",
        connectPath     = "",
        connectPort     = 0,
        connectSSL      = Nothing,
        connectUser     = "",
        connectOptions  = [ ReadDefaultFile file, ReadDefaultGroup (pack g) ]
      }) servers
      listen = maybe (Right socket) (Left . read) port
    server listen myInfo datadir