From 6774069a4f1135abbb175f1e09d93d663fb983de Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 16 Aug 2024 14:48:49 -0500 Subject: [PATCH] simplify --- tunnel-go/main.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/tunnel-go/main.go b/tunnel-go/main.go index 0a58a69..decbc96 100644 --- a/tunnel-go/main.go +++ b/tunnel-go/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/fs" "log" "os" "os/exec" @@ -25,16 +24,6 @@ func activateSshControl() bool { return ok(exec.Command("ssh", "-M", "-f", "-N func connectPort() bool { return ok(exec.Command("ssh", "-fNL", portString(), host)) } func deactivateSshControl() bool { return ok(exec.Command("ssh", "-O", "exit", host)) } -func genSubCmd(use string, short string, run func()) *cobra.Command { - return &cobra.Command{ - Use: use, Short: short, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - host = args[0] - run() - }, - } -} - func tunnelUp() { if !checkControl() { logFail(activateSshControl(), "failed to activate ssh for host: %s", host) @@ -46,7 +35,11 @@ func tunnelDown() { logFail(deactivateSshControl(), "failed to disable ssh control %s", host) } -func getControlSockets(files []fs.DirEntry) (ret []string) { +func getControlSockets() (ret []string) { + files, err := os.ReadDir(sshDir) + if err != nil { + panic(err) + } for _, f := range files { if strings.HasPrefix(f.Name(), "control") { ret = append(ret, f.Name()) @@ -56,11 +49,7 @@ func getControlSockets(files []fs.DirEntry) (ret []string) { } func tunnelShow() { - files, err := os.ReadDir(sshDir) - if err != nil { - panic(err) - } - controls := getControlSockets(files) + controls := getControlSockets() fmt.Printf("%d active connections\n", len(controls)) if len(controls) > 0 { fmt.Println("hosts:") @@ -70,6 +59,13 @@ func tunnelShow() { } } +func genSubCmd(use string, short string, run func()) *cobra.Command { + return &cobra.Command{ + Use: use, Short: short, Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { host = args[0]; run() }, + } +} + var ( sshDir = filepath.Join(os.Getenv("HOME"), ".ssh") rootCmd = &cobra.Command{Use: "tunnel", Short: "control ssh tunnels"}