formatting
This commit is contained in:
parent
6774069a4f
commit
cca9e7e68c
1 changed files with 12 additions and 18 deletions
|
@ -1,24 +1,18 @@
|
||||||
import std/[os, osproc, sequtils, strformat, strutils, sugar]
|
import std/[os, osproc, sequtils, strformat, strutils, sugar]
|
||||||
|
|
||||||
import cligen
|
import cligen
|
||||||
|
|
||||||
proc checkHost(host: seq[string]): string =
|
proc checkHost(host: seq[string]): string =
|
||||||
case host.len:
|
case host.len:
|
||||||
of 0: quit "expected hostname"
|
|
||||||
of 1: return host[0]
|
of 1: return host[0]
|
||||||
else: quit "expected one positinal argument"
|
of 0: quit "expected hostname"
|
||||||
|
else: quit "expected one positinal argument"
|
||||||
|
|
||||||
|
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 check(name: string): bool =
|
proc up(port: int, host: seq[string]) =
|
||||||
(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
|
## activate a tunnel
|
||||||
let name = checkHost host
|
let name = checkHost host
|
||||||
echo "activating connection to", name
|
echo "activating connection to", name
|
||||||
|
@ -27,7 +21,7 @@ proc up(port: int, host: seq[string]) =
|
||||||
quit activateTunnel(name, port)
|
quit activateTunnel(name, port)
|
||||||
|
|
||||||
|
|
||||||
proc down(host: seq[string]) =
|
proc down(host: seq[string]) =
|
||||||
## disable all tunnels
|
## disable all tunnels
|
||||||
let name = checkHost host
|
let name = checkHost host
|
||||||
echo "deactivating connection to", name
|
echo "deactivating connection to", name
|
||||||
|
@ -36,7 +30,7 @@ proc down(host: seq[string]) =
|
||||||
proc show() =
|
proc show() =
|
||||||
## show active connections
|
## show active connections
|
||||||
let sshDir = (getEnv "HOME") / ".ssh"
|
let sshDir = (getEnv "HOME") / ".ssh"
|
||||||
let controllers =
|
let controllers =
|
||||||
collect(for _,p in walkDir(sshDir): p)
|
collect(for _,p in walkDir(sshDir): p)
|
||||||
.map(extractFilename)
|
.map(extractFilename)
|
||||||
.filterIt(it.startsWith("control"))
|
.filterIt(it.startsWith("control"))
|
||||||
|
@ -44,9 +38,9 @@ proc show() =
|
||||||
if controllers.len == 0: quit 0
|
if controllers.len == 0: quit 0
|
||||||
echo "hosts:"
|
echo "hosts:"
|
||||||
echo controllers.mapIt(" " & it.split('-')[1]).join("\n")
|
echo controllers.mapIt(" " & it.split('-')[1]).join("\n")
|
||||||
|
|
||||||
const
|
const
|
||||||
hostUsage = "$command [flags] hostname\n${doc}Options:\n$options"
|
hostUsage = "$command [flags] hostname\n${doc}Options:\n$options"
|
||||||
usage = "$command [flags]\n${doc}Options:\n$options"
|
usage = "$command [flags]\n${doc}Options:\n$options"
|
||||||
|
|
||||||
dispatchMulti([up, usage=hostUsage], [down, usage=hostUsage], [show, usage=usage])
|
dispatchMulti([up, usage=hostUsage], [down, usage=hostUsage], [show, usage=usage])
|
||||||
|
|
Loading…
Reference in a new issue