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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"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 connectPort() bool { return ok(exec.Command("ssh", "-fNL", portString(), host)) }
|
||||||
func deactivateSshControl() bool { return ok(exec.Command("ssh", "-O", "exit", 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() {
|
func tunnelUp() {
|
||||||
if !checkControl() {
|
if !checkControl() {
|
||||||
logFail(activateSshControl(), "failed to activate ssh for host: %s", host)
|
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)
|
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 {
|
for _, f := range files {
|
||||||
if strings.HasPrefix(f.Name(), "control") {
|
if strings.HasPrefix(f.Name(), "control") {
|
||||||
ret = append(ret, f.Name())
|
ret = append(ret, f.Name())
|
||||||
|
@ -56,11 +49,7 @@ func getControlSockets(files []fs.DirEntry) (ret []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func tunnelShow() {
|
func tunnelShow() {
|
||||||
files, err := os.ReadDir(sshDir)
|
controls := getControlSockets()
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
controls := getControlSockets(files)
|
|
||||||
fmt.Printf("%d active connections\n", len(controls))
|
fmt.Printf("%d active connections\n", len(controls))
|
||||||
if len(controls) > 0 {
|
if len(controls) > 0 {
|
||||||
fmt.Println("hosts:")
|
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 (
|
var (
|
||||||
sshDir = filepath.Join(os.Getenv("HOME"), ".ssh")
|
sshDir = filepath.Join(os.Getenv("HOME"), ".ssh")
|
||||||
rootCmd = &cobra.Command{Use: "tunnel", Short: "control ssh tunnels"}
|
rootCmd = &cobra.Command{Use: "tunnel", Short: "control ssh tunnels"}
|
||||||
|
|
Loading…
Reference in a new issue