From da246de5363c6eea2fbd1e6ddc40828a4d1619ba Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 30 Oct 2024 16:20:09 -0500 Subject: [PATCH] promote tunnel-nim to sole implementation --- flake.nix | 5 +- tunnel-go/default.nix | 27 --------- tunnel-go/go.mod | 16 ----- tunnel-go/go.sum | 32 ---------- tunnel-go/main.go | 91 ---------------------------- {tunnel-nim => tunnel}/.gitignore | 0 {tunnel-nim => tunnel}/config.nims | 0 {tunnel-nim => tunnel}/default.nix | 0 {tunnel-nim => tunnel}/nimble.lock | 0 {tunnel-nim => tunnel}/todo.md | 0 {tunnel-nim => tunnel}/tunnel.nim | 0 {tunnel-nim => tunnel}/tunnel.nimble | 0 12 files changed, 1 insertion(+), 170 deletions(-) delete mode 100644 tunnel-go/default.nix delete mode 100644 tunnel-go/go.mod delete mode 100644 tunnel-go/go.sum delete mode 100644 tunnel-go/main.go rename {tunnel-nim => tunnel}/.gitignore (100%) rename {tunnel-nim => tunnel}/config.nims (100%) rename {tunnel-nim => tunnel}/default.nix (100%) rename {tunnel-nim => tunnel}/nimble.lock (100%) rename {tunnel-nim => tunnel}/todo.md (100%) rename {tunnel-nim => tunnel}/tunnel.nim (100%) rename {tunnel-nim => tunnel}/tunnel.nimble (100%) diff --git a/flake.nix b/flake.nix index 77739f5..0f2a50b 100644 --- a/flake.nix +++ b/flake.nix @@ -32,15 +32,12 @@ { packages = forAllSystems (pkgs: rec { utils = pkgs.callPackage ./meta.nix { inherit tunnel; }; - tunnel-go = pkgs.callPackage ./tunnel-go { }; - tunnel-nim = pkgs.callPackage ./tunnel-nim { }; - tunnel = tunnel-nim; + tunnel = pkgs.callPackage ./tunnel-nim { }; default = utils; }); devShells = forAllSystems (pkgs: { default = pkgs.mkShell { packages = with pkgs; [ - go nim ]; }; diff --git a/tunnel-go/default.nix b/tunnel-go/default.nix deleted file mode 100644 index 5a92e52..0000000 --- a/tunnel-go/default.nix +++ /dev/null @@ -1,27 +0,0 @@ -{ - lib, - installShellFiles, - buildGoModule, - makeWrapper, - ... -}: -let - inherit (lib) cleanSource; -in -buildGoModule rec { - pname = "tunnel"; - version = "unstable"; - - src = cleanSource ./.; - vendorHash = "sha256-PwZJMEVaPHqZs7bM+9XLxVA36GfV3EN6bja86hkfO90="; - - nativeBuildInputs = [ - installShellFiles - makeWrapper - ]; - - postInstall = '' - installShellCompletion --cmd ${pname} \ - --zsh <($out/bin/${pname} completion zsh) - ''; -} diff --git a/tunnel-go/go.mod b/tunnel-go/go.mod deleted file mode 100644 index 8947218..0000000 --- a/tunnel-go/go.mod +++ /dev/null @@ -1,16 +0,0 @@ -module git.dayl.in/daylin/utils/tunnel - -go 1.22.5 - -require ( - github.com/bitfield/script v0.22.1 - github.com/spf13/cobra v1.8.1 -) - -require ( - github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/itchyny/gojq v0.12.13 // indirect - github.com/itchyny/timefmt-go v0.1.5 // indirect - github.com/spf13/pflag v1.0.5 // indirect - mvdan.cc/sh/v3 v3.7.0 // indirect -) diff --git a/tunnel-go/go.sum b/tunnel-go/go.sum deleted file mode 100644 index af39969..0000000 --- a/tunnel-go/go.sum +++ /dev/null @@ -1,32 +0,0 @@ -github.com/bitfield/script v0.22.1 h1:DphxoC5ssYciwd0ZS+N0Xae46geAD/0mVWh6a2NUxM4= -github.com/bitfield/script v0.22.1/go.mod h1:fv+6x4OzVsRs6qAlc7wiGq8fq1b5orhtQdtW0dwjUHI= -github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= -github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= -github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU= -github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4= -github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE= -github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= -github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= -golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg= -mvdan.cc/sh/v3 v3.7.0/go.mod h1:K2gwkaesF/D7av7Kxl0HbF5kGOd2ArupNTX3X44+8l8= diff --git a/tunnel-go/main.go b/tunnel-go/main.go deleted file mode 100644 index decbc96..0000000 --- a/tunnel-go/main.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - "os/exec" - "path/filepath" - "strings" - - "github.com/spf13/cobra" -) - -func logFail(cond bool, s string, args ...interface{}) { - if !cond { - log.Fatalf(s, args...) - } -} - -func ok(cmd *exec.Cmd) bool { err := cmd.Run(); return err == nil } -func portString() string { return fmt.Sprintf("%[1]d:localhost:%[1]d", port) } -func checkControl() bool { return ok(exec.Command("ssh", "-O", "check", host)) } -func activateSshControl() bool { return ok(exec.Command("ssh", "-M", "-f", "-N", host)) } -func connectPort() bool { return ok(exec.Command("ssh", "-fNL", portString(), host)) } -func deactivateSshControl() bool { return ok(exec.Command("ssh", "-O", "exit", host)) } - -func tunnelUp() { - if !checkControl() { - logFail(activateSshControl(), "failed to activate ssh for host: %s", host) - } - logFail(connectPort(), "failed to connect to host: %s with port: %d", host, port) -} - -func tunnelDown() { - logFail(deactivateSshControl(), "failed to disable ssh control %s", host) -} - -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()) - } - } - return -} - -func tunnelShow() { - controls := getControlSockets() - fmt.Printf("%d active connections\n", len(controls)) - if len(controls) > 0 { - fmt.Println("hosts:") - for _, c := range controls { - fmt.Printf(" %s\n", strings.Split(c, "-")[1]) - } - } -} - -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"} - upCmd = genSubCmd("up hostname [flags]", "activate ssh tunnel", tunnelUp) - downCmd = genSubCmd("down hostname [flags]", "deactivate ssh tunnel", tunnelDown) - showCmd = &cobra.Command{Use: "show", Short: "show activate tunnels", Run: func(cmd *cobra.Command, args []string) { tunnelShow() }} - port uint64 - host string -) - -func init() { - rootCmd.CompletionOptions.HiddenDefaultCmd = true - rootCmd.AddCommand(upCmd, downCmd, showCmd) - upCmd.Flags().Uint64VarP(&port, "port", "p", 0, "port number") - upCmd.MarkFlagRequired("port") -} - -func main() { - err := rootCmd.Execute() - if err != nil { - os.Exit(1) - } -} diff --git a/tunnel-nim/.gitignore b/tunnel/.gitignore similarity index 100% rename from tunnel-nim/.gitignore rename to tunnel/.gitignore diff --git a/tunnel-nim/config.nims b/tunnel/config.nims similarity index 100% rename from tunnel-nim/config.nims rename to tunnel/config.nims diff --git a/tunnel-nim/default.nix b/tunnel/default.nix similarity index 100% rename from tunnel-nim/default.nix rename to tunnel/default.nix diff --git a/tunnel-nim/nimble.lock b/tunnel/nimble.lock similarity index 100% rename from tunnel-nim/nimble.lock rename to tunnel/nimble.lock diff --git a/tunnel-nim/todo.md b/tunnel/todo.md similarity index 100% rename from tunnel-nim/todo.md rename to tunnel/todo.md diff --git a/tunnel-nim/tunnel.nim b/tunnel/tunnel.nim similarity index 100% rename from tunnel-nim/tunnel.nim rename to tunnel/tunnel.nim diff --git a/tunnel-nim/tunnel.nimble b/tunnel/tunnel.nimble similarity index 100% rename from tunnel-nim/tunnel.nimble rename to tunnel/tunnel.nimble