simplify
This commit is contained in:
parent
254f0536de
commit
6774069a4f
1 changed files with 13 additions and 17 deletions
|
@ -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"}
|
||||
|
|
Loading…
Reference in a new issue