From 367d2e55238b937c89a317f79b12410c11d4e7e5 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 6 Nov 2024 18:36:42 -0600 Subject: [PATCH] hwylcli for the win --- tunnel/default.nix | 2 +- tunnel/nimble.lock | 14 ++--------- tunnel/tunnel.nim | 57 +++++++++++++++++++++++++++----------------- tunnel/tunnel.nimble | 1 - 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/tunnel/default.nix b/tunnel/default.nix index 1bbaaa8..97330e0 100644 --- a/tunnel/default.nix +++ b/tunnel/default.nix @@ -8,5 +8,5 @@ buildNimblePackage { src = lib.cleanSource ./.; doCheck = false; - nimbleDepsHash = "sha256-frbWVz7ow3wAbQCVfDk9KWN8Va2jdkW9hp7OKxzltis="; + nimbleDepsHash = "sha256-68ausfWeGbiy/zcZYADarO7b2tusEnRSkFKYlcZ6boE="; } diff --git a/tunnel/nimble.lock b/tunnel/nimble.lock index 7da188e..0234e53 100644 --- a/tunnel/nimble.lock +++ b/tunnel/nimble.lock @@ -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" } } }, diff --git a/tunnel/tunnel.nim b/tunnel/tunnel.nim index 03c39fb..5189c63 100644 --- a/tunnel/tunnel.nim +++ b/tunnel/tunnel.nim @@ -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() diff --git a/tunnel/tunnel.nimble b/tunnel/tunnel.nimble index 7ec80e7..9069876 100644 --- a/tunnel/tunnel.nimble +++ b/tunnel/tunnel.nimble @@ -10,6 +10,5 @@ bin = @["tunnel"] # Dependencies requires "nim >= 2.0.8" -requires "cligen >= 1.7.6" requires "https://github.com/daylinmorgan/hwylterm#HEAD"