1
0
Fork 0
mirror of https://github.com/Luzifer/expose.git synced 2024-12-23 03:11:21 +00:00

Add auto-close for serve command

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-10-15 21:24:17 +02:00
parent c5051d8724
commit 0b9ff9c731
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time"
"github.com/Luzifer/expose/ngrok2" "github.com/Luzifer/expose/ngrok2"
http_helper "github.com/Luzifer/go_helpers/http" http_helper "github.com/Luzifer/go_helpers/http"
@ -50,6 +51,15 @@ var serveCmd = &cobra.Command{
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
if d, err := cmd.Flags().GetDuration("timeout"); err == nil && d > 0 {
fmt.Printf(" (automatic close in %s)", d)
go func() {
<-time.After(d)
c <- os.Interrupt
}()
}
for range c { for range c {
if err := client.StopTunnel(tun.Name); err != nil { if err := client.StopTunnel(tun.Name); err != nil {
return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err) return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err)
@ -65,6 +75,8 @@ var serveCmd = &cobra.Command{
func init() { func init() {
RootCmd.AddCommand(serveCmd) RootCmd.AddCommand(serveCmd)
serveCmd.Flags().DurationP("timeout", "t", 0, "Automatically close tunnel after timeout")
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command // Cobra supports Persistent Flags which will work for this command