aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreuxane2024-11-24 17:17:02 +0100
committereuxane2024-11-24 17:17:02 +0100
commita2d15bdd78f071549d46aa6ac5467a8a8cf6ebbd (patch)
treeb14b99c89e678197c74a2993ed6d008c36c94726
parenta41641398d09f8457a33445b73a1a402c08c87cd (diff)
downloadtickwatch-a2d15bdd78f071549d46aa6ac5467a8a8cf6ebbd.tar.gz
main: add module
-rw-r--r--main.nim97
1 files changed, 97 insertions, 0 deletions
diff --git a/main.nim b/main.nim
new file mode 100644
index 0000000..04a81f1
--- /dev/null
+++ b/main.nim
@@ -0,0 +1,97 @@
1# tickwatch
2# Author: Euxane TRAN-GIRARD
3# Licence: EUPL-1.2
4
5import std/sugar
6import std/math
7import std/times
8import std/net
9import std/posix
10import std/paths
11import std/strutils
12import std/parseopt
13
14import file
15import ping
16import logger
17
18
19const
20 NAME = "tickwatch"
21 VERSION = staticExec("""
22 command -v git >/dev/null && git describe --tags --always || echo $VERSION
23 """).strip()
24 HELP_TEXT =
25 staticRead("readme.md")
26 .split("```help", 1)[1]
27 .split("```", 1)[0]
28 .strip()
29
30proc registerTerminationHandlers() =
31 proc terminationHandler(sig: cint) {.noconv.} = flushAndQuit()
32 var action = SigAction(sa_handler: terminationHandler)
33 for signal in [SIGTERM, SIGHUP, SIGQUIT, SIGINT]:
34 discard sigaction(signal, action)
35
36proc main() =
37 var
38 scale: Scale = log2
39 symbols = UNICODE_SYMBOLS
40 (min, max) = (0, 1000)
41 probe: (timeout: Duration) -> int
42
43 for optKind, key, val in getopt():
44 if optKind notin {cmdLongOption, cmdShortOption}:
45 raise newException(ValueError, "Invalid argument: " & key)
46
47 case key:
48
49 of "help", "h":
50 echo HELP_TEXT
51 quit(0)
52
53 of "version", "v":
54 echo NAME & " " & VERSION
55 quit(0)
56
57 of "scale":
58 case val:
59 of "logarithmic", "log", "log2": scale = log2
60 of "log10": scale = log10
61 of "ln": scale = ln
62 of "linear", "lin": scale = (val: float) => val
63
64 of "symbols":
65 case val:
66 of "unicode", "utf8", "utf-8": symbols = UNICODE_SYMBOLS
67 of "numeric", "ascii": symbols = NUMERIC_SYMBOLS
68
69 of "min": min = parseInt(val)
70 of "max": max = parseInt(val)
71
72 of "ping":
73 let targetIp = parseIpAddress(val)
74 let mon = initPingMonitor(targetIp, procIdent())
75 probe = (timeout: Duration) => mon.ping(timeout).inMilliseconds.int
76
77 of "value":
78 let mon = initFileValueMonitor(Path val)
79 probe = (timeout: Duration) => mon.readValue()
80
81 of "change":
82 let mon = initFileChangeMonitor(Path val)
83 probe = (timeout: Duration) => mon.readValue()
84
85 if probe == nil:
86 raise newException(ValueError, "Missing monitor argument")
87
88 loop(probe, (val: int) => symbols.indicator(min, max, val, scale))
89
90
91when not defined(test):
92 try:
93 registerTerminationHandlers()
94 main()
95 except CatchableError as err:
96 stderr.writeLine err.msg
97 quit(1)