hwylcli for the win
This commit is contained in:
parent
d86dff8360
commit
367d2e5523
4 changed files with 38 additions and 36 deletions
|
@ -8,5 +8,5 @@ buildNimblePackage {
|
|||
|
||||
src = lib.cleanSource ./.;
|
||||
doCheck = false;
|
||||
nimbleDepsHash = "sha256-frbWVz7ow3wAbQCVfDk9KWN8Va2jdkW9hp7OKxzltis=";
|
||||
nimbleDepsHash = "sha256-68ausfWeGbiy/zcZYADarO7b2tusEnRSkFKYlcZ6boE=";
|
||||
}
|
||||
|
|
|
@ -1,24 +1,14 @@
|
|||
{
|
||||
"version": 2,
|
||||
"packages": {
|
||||
"cligen": {
|
||||
"version": "1.7.7",
|
||||
"vcsRevision": "f50f887eacfe33866e22f8a0d322e29a1c830cf9",
|
||||
"url": "https://github.com/c-blake/cligen.git",
|
||||
"downloadMethod": "git",
|
||||
"dependencies": [],
|
||||
"checksums": {
|
||||
"sha1": "237596b6be88dcf9ee0bd9732a7baaac526f0c07"
|
||||
}
|
||||
},
|
||||
"hwylterm": {
|
||||
"version": "0.1.0",
|
||||
"vcsRevision": "56bb2bb7c1da402c2c52fb3f7f10620c7c2fca7e",
|
||||
"vcsRevision": "f1c3fdfe78dea7285cb759b4962d6d1848fce760",
|
||||
"url": "https://github.com/daylinmorgan/hwylterm",
|
||||
"downloadMethod": "git",
|
||||
"dependencies": [],
|
||||
"checksums": {
|
||||
"sha1": "8810cf69a74de580e1ab08f0c153afbf33c8a98e"
|
||||
"sha1": "885ef6dbddf0e4cb3a16e97943d2298fdd053569"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -10,20 +10,15 @@ proc checkHost(host: seq[string]): string =
|
|||
|
||||
proc check(name: string): bool = (execCmd &"ssh -O check {name}") == 0
|
||||
proc startSsh(name: string) = discard execCmd &"ssh -M -f -N {name}"
|
||||
proc activateTunnel(name: string, port: int): int = execCmd &"""ssh -fNL "{port}:localhost:{port}" {name}"""
|
||||
proc exitSsh(name: string):int = execCmd &"ssh -O exit {name}"
|
||||
|
||||
proc up(port: int, host: seq[string]) =
|
||||
## activate a tunnel
|
||||
let name = checkHost host
|
||||
proc activateTunnel(name: string, port: int) =
|
||||
echo "activating connection to: ", name
|
||||
if not check(name):
|
||||
startSsh(name)
|
||||
quit activateTunnel(name, port)
|
||||
quit execCmd &"""ssh -fNL "{port}:localhost:{port}" {name}"""
|
||||
|
||||
proc down(host: seq[string]) =
|
||||
## disable all tunnels for a host
|
||||
let name = checkHost host
|
||||
proc deactivateTunnels(name: string) =
|
||||
echo "deactivating connection to: ", name
|
||||
quit exitSsh(name)
|
||||
|
||||
|
@ -33,8 +28,7 @@ proc collectControllers(): seq[string] =
|
|||
.map(extractFilename)
|
||||
.filterIt(it.startsWith("control"))
|
||||
|
||||
proc killCmd() =
|
||||
## kill all open tunnels
|
||||
proc killTunnels() =
|
||||
let controllers = collectControllers()
|
||||
if controllers.len == 0: quit 0
|
||||
for c in controllers:
|
||||
|
@ -43,8 +37,7 @@ proc killCmd() =
|
|||
if code != 0:
|
||||
quit $bb"[red][[Error]failed to exit connection for host: " & host
|
||||
|
||||
proc show() =
|
||||
## show active connections
|
||||
proc showTunnels() =
|
||||
let controllers = collectControllers()
|
||||
echo bbfmt"[yellow]{controllers.len}[/] active connections"
|
||||
if controllers.len == 0: quit 0
|
||||
|
@ -52,14 +45,34 @@ proc show() =
|
|||
echo controllers.mapIt(" " & it.split('-')[1]).join("\n")
|
||||
|
||||
when isMainModule:
|
||||
import cligen, hwylterm/cligen
|
||||
hwylCli(clCfg)
|
||||
let hostUse =
|
||||
$bb("$command [green]hostname[/] [[[i]flags[/]]\n${doc}[b]Options[/]:\n$options")
|
||||
dispatchMulti(
|
||||
[up , usage=hostUse ],
|
||||
[down , usage=hostUse ],
|
||||
[show , usage=clCfg.use ],
|
||||
[killCmd, usage=clCfg.use, cmdName = "kill"],
|
||||
)
|
||||
import hwylterm/[hwylcli]
|
||||
hwylCli:
|
||||
name "tunnel"
|
||||
settings NoArgsShowHelp
|
||||
subcommands:
|
||||
--- up
|
||||
? "[b]tunnel up[/] [green]host[/] [[[faint]-h|-p[/]]"
|
||||
... "activate a tunnel"
|
||||
required port
|
||||
flags:
|
||||
port:
|
||||
- p
|
||||
T int
|
||||
? "port number"
|
||||
* 8555
|
||||
run:
|
||||
let host = checkHost(args)
|
||||
activateTunnel(host, port)
|
||||
--- down
|
||||
? "[b]tunnel down[/] [green]host[/] [[[faint]-h[/]]"
|
||||
... "disable all tunnels for a host"
|
||||
run:
|
||||
let host = checkHost(args)
|
||||
deactivateTunnels(host)
|
||||
--- show
|
||||
... "show active connections"
|
||||
run: showTunnels()
|
||||
--- kill
|
||||
... "kill all open tunnels"
|
||||
run: killTunnels()
|
||||
|
||||
|
|
|
@ -10,6 +10,5 @@ bin = @["tunnel"]
|
|||
|
||||
# Dependencies
|
||||
requires "nim >= 2.0.8"
|
||||
requires "cligen >= 1.7.6"
|
||||
requires "https://github.com/daylinmorgan/hwylterm#HEAD"
|
||||
|
||||
|
|
Loading…
Reference in a new issue