From 374e59dcdabe24b5ad95a53510f9247956da0353 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 23 Jan 2025 16:21:59 -0600 Subject: [PATCH] update hwylterm --- tunnel/.gitignore | 2 ++ tunnel/default.nix | 2 +- tunnel/nimble.lock | 4 +-- tunnel/src/tunnel.nim | 73 +++++++++++++++++++------------------------ tunnel/tunnel.nimble | 2 +- 5 files changed, 38 insertions(+), 45 deletions(-) diff --git a/tunnel/.gitignore b/tunnel/.gitignore index 4154f4b..97af2c0 100644 --- a/tunnel/.gitignore +++ b/tunnel/.gitignore @@ -1,3 +1,5 @@ nimbledeps/ nimble.paths nimble.develop +bin/ +src/tunnel diff --git a/tunnel/default.nix b/tunnel/default.nix index a9f8fbe..ebf41b3 100644 --- a/tunnel/default.nix +++ b/tunnel/default.nix @@ -8,5 +8,5 @@ buildNimblePackage { src = lib.cleanSource ./.; doCheck = false; - nimbleDepsHash = "sha256-F83auZWVeIYXPBb7Zn2f1/mHEMB9pjpgeYz5UYHlu+0="; + nimbleDepsHash = "sha256-NajR821HbgR6TABZJOkMIf7EZq4dgDAZ02PZlKwtKCA="; } diff --git a/tunnel/nimble.lock b/tunnel/nimble.lock index 65c4a5e..e313b2b 100644 --- a/tunnel/nimble.lock +++ b/tunnel/nimble.lock @@ -3,12 +3,12 @@ "packages": { "hwylterm": { "version": "0.1.0", - "vcsRevision": "65a952e9f0d75659d39f8a3b146753e291ee0a66", + "vcsRevision": "50d0ee5a3bbba88e1d73f5eb3cf0d1e9911885ca", "url": "https://github.com/daylinmorgan/hwylterm", "downloadMethod": "git", "dependencies": [], "checksums": { - "sha1": "10894d492bbe1d0ea32ee9bb0686abfcd3b0b0fe" + "sha1": "7b353603c818f4be92238719c62bb97be7e00580" } } }, diff --git a/tunnel/src/tunnel.nim b/tunnel/src/tunnel.nim index 4e1d09b..d08ea94 100644 --- a/tunnel/src/tunnel.nim +++ b/tunnel/src/tunnel.nim @@ -1,16 +1,10 @@ import std/[os, osproc, sequtils, strformat, strutils, sugar] -import hwylterm +import hwylterm, hwylterm/[hwylcli] -proc checkHost(host: seq[string]): string = - case host.len: - of 1: return host[0] - of 0: quit $bb"[red]expected hostname" - else: quit $bb"[red]expected one positional argument" - proc check(name: string): bool = (execCmd &"ssh -O check {name}") == 0 proc startSsh(name: string) = discard execCmd &"ssh -M -f -N {name}" -proc exitSsh(name: string):int = execCmd &"ssh -O exit {name}" +proc exitSsh(name: string): int = execCmd &"ssh -O exit {name}" proc activateTunnel(name: string, port: int) = echo "activating connection to: ", name @@ -32,7 +26,7 @@ proc killTunnels() = let controllers = collectControllers() if controllers.len == 0: quit 0 for c in controllers: - let host = c.lastPathPart().split("-")[2] + let host = c.lastPathPart().split("-")[1] let code = exitSsh(host) if code != 0: quit $bb"[red][[Error]failed to exit connection for host: " & host @@ -44,39 +38,36 @@ proc showTunnels() = echo "hosts:" echo controllers.mapIt(" " & it.split('-')[1]).join("\n") -when isMainModule: - import hwylterm/[hwylcli] - hwylCli: - name "tunnel" - settings ShowHelp - subcommands: +hwylCli: + name "tunnel" + settings ShowHelp + subcommands: + [up] + ... "activate a tunnel" + args: + host string + required port + flags: + port: + - p + T int + ? "port number" + * 8555 + run: + activateTunnel(host, port) - [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] + ... "disable all tunnels for a host" + args: + host string + run: + deactivateTunnels(host) - [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() - [show] - ... "show active connections" - run: showTunnels() - - [kill] - ... "kill all open tunnels" - run: killTunnels() + [kill] + ... "kill all open tunnels" + run: killTunnels() diff --git a/tunnel/tunnel.nimble b/tunnel/tunnel.nimble index 5e2afcc..2a82a28 100644 --- a/tunnel/tunnel.nimble +++ b/tunnel/tunnel.nimble @@ -9,5 +9,5 @@ bin = @["tunnel"] # Dependencies requires "nim >= 2.0.8" -requires "https://github.com/daylinmorgan/hwylterm#65a952e" +requires "https://github.com/daylinmorgan/hwylterm#50d0ee5"