summaryrefslogtreecommitdiffhomepage
path: root/CmdLine.hs
blob: 28227d404e07982b2dd37ace97fec92583071c88 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{- Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module CmdLine where

import Types
import ServerList

import Options.Applicative
import Network.URI
import Network.Wai.Handler.Warp (Port)
import qualified Data.Text as T

data CmdLine = CmdLine
	{ mode :: Mode
	}

data Mode
	= UserMode UserOpts
	| DeveloperMode DeveloperOpts
	| DownloadMode DownloadOpts
	| WatchMode WatchOpts
	| GraphvizMode GraphvizOpts
	| ReplayMode ReplayOpts
	| VerifyMode VerifyOpts
	| ServerMode ServerOpts
	| ControlMode ControlOpts

data UserOpts = UserOpts
	{ cmdToRun :: Maybe String
	, useServer :: URI
	}

data DeveloperOpts = DeveloperOpts
	{ debugUrl :: URI
	}

data DownloadOpts = DownloadOpts
	{ downloadUrl :: URI
	}

data WatchOpts = WatchOpts
	{ watchUrl :: URI
	}

data GraphvizOpts = GraphvizOpts
	{ graphvizLogFile :: FilePath
	, graphvizShowHashes :: Bool
	}

data ReplayOpts = ReplayOpts
	{ replayLogFile :: FilePath
	}

data VerifyOpts = VerifyOpts
	{ verifyLogFile :: FilePath
	}

data ServerOpts = ServerOpts
	{ serverDirectory :: FilePath
	, serverPort :: Port
	, serverEmail :: Maybe EmailAddress
	, serverDeleteOldLogs :: Bool
	}

data ControlOpts = ControlOpts
	{ controlWindowEnabled :: Bool
	}

parseCmdLine :: Parser CmdLine
parseCmdLine = CmdLine <$> parseMode

parseMode :: Parser Mode
parseMode = (UserMode <$> parseuser)
	<|> (DeveloperMode <$> parsedeveloper)
	<|> (ReplayMode <$> parsereplay)
	<|> (VerifyMode <$> parseverify)
	<|> (DownloadMode <$> parsedownload)
	<|> (WatchMode <$> parsewatch)
	<|> (GraphvizMode <$> parsegraphviz)
	<|> (ServerMode <$> parseserver)
	<|> (ControlMode <$> parsecontrol)
  where
	parseuser = UserOpts
		<$> optional (strOption
			( long "run"
			<> metavar "command"
			<> help "program to run (default: login shell)"
			))
		<*> option readurl
			( long "use-server"
			<> metavar "url"
			<> value defaultServerUrl
			<> showDefault
			<> help "url of debug-me server to use"
			)
	parsedeveloper = DeveloperOpts
		<$> argument readurl
			( metavar "url"
			<> help "debug a user on the given url"
			)
	parsegraphviz = GraphvizOpts
		<$> option str
			( long "graphviz"
			<> metavar "logfile"
			<> help "visualize log file with graphviz"
			)
		<*> switch
			( long "show-hashes"
			<> help "display hashes in graphviz"
			)
	parsereplay = ReplayOpts
		<$> option str
			( long "replay"
			<> metavar "logfile"
			<> help "replay log file"
			)
	parseverify = VerifyOpts
		<$> option str
			( long "verify"
			<> metavar "logfile"
			<> help "verify log file"
			)
	parsedownload = DownloadOpts
		<$> option readurl
			( long "download"
			<> metavar "url"
			<> help "download log file from server"
			)
	parsewatch = WatchOpts
		<$> option readurl
			( long "watch"
			<> metavar "url"
			<> help "display a debug-me session non-interactively"
			)
	parseserver = ServerOpts
		<$> strOption
			( long "server"
			<> metavar "logdir"
			<> help "run server, storing logs in the specified directory"
			)
		<*> option auto
			( long "port"
			<> metavar "N"
			<> value 8081
			<> showDefault
			<> help "port for server to listen on"
			)
		<*> optional (T.pack <$> strOption
			( long "from-email"
			<> metavar "address"
			<> help "email session logs using this from address"
			))
		<*> switch
			( long "delete-old-logs"
			<> help "delete session logs after session is done"
			)
	parsecontrol = ControlOpts
		<$> switch
			( long "control"
			<> help "control running debug-me session"
			)

getCmdLine :: IO CmdLine
getCmdLine = execParser opts
  where
	opts = info (helper <*> parseCmdLine)
		( fullDesc
		<> header "debug-me - provable remote debugging sessions"
		)

readurl :: ReadM URI
readurl = eitherReader $ maybe (Left "url parse error") Right . parseURI