Skip to content

Commit d0dfdb9

Browse files
committed
Handle panic
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent c6af09f commit d0dfdb9

File tree

13 files changed

+58
-62
lines changed

13 files changed

+58
-62
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
IMAGE = labstack/tunnel
2-
VERSION = 0.4.0
2+
VERSION = 0.4.1
33

44
publish:
55
git tag v$(VERSION)

cmd/cmd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"github.com/briandowns/spinner"
56
"github.com/spf13/viper"
67
"io/ioutil"
78
"net/rpc"
9+
"os"
810
"time"
911
)
1012

1113
var (
1214
s = spinner.New(spinner.CharSets[32], 100*time.Millisecond)
1315
)
1416

17+
func exit(i interface{}) {
18+
fmt.Println(i)
19+
os.Exit(1)
20+
}
21+
1522
func getClient() (*rpc.Client, error) {
1623
addr, _ := ioutil.ReadFile(viper.GetString("daemon_addr"))
1724
return rpc.Dial("tcp", string(addr))
1825
}
19-

cmd/connect.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"github.com/labstack/gommon/log"
65
"github.com/labstack/tunnel-client/daemon"
76
"github.com/spf13/cobra"
87
)
@@ -22,7 +21,7 @@ var connectCmd = &cobra.Command{
2221
startDaemon()
2322
c, err := getClient()
2423
if err != nil {
25-
log.Fatal(err)
24+
exit(err)
2625
}
2726
defer c.Close()
2827
rep := new(daemon.ConnectReply)
@@ -33,7 +32,7 @@ var connectCmd = &cobra.Command{
3332
Protocol: daemon.Protocol(protocol),
3433
}, rep)
3534
if err != nil {
36-
log.Fatal(err)
35+
exit(err)
3736
}
3837
s.Stop()
3938
psRPC()

cmd/daemon.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"github.com/labstack/gommon/log"
65
"github.com/labstack/tunnel-client/daemon"
76
"github.com/mitchellh/go-ps"
87
"io/ioutil"
@@ -20,7 +19,7 @@ func checkKey() {
2019

2120
func startDaemon() {
2221
if viper.GetString("api_key") == "" {
23-
log.Fatal("to use tunnel you need an api key (https://tunnel.labstack.com) in $HOME/.tunnel/config.yaml")
22+
exit("To use tunnel you need an api key (https://tunnel.labstack.com) in $HOME/.tunnel/config.yaml")
2423
}
2524
start := true
2625
d, err := ioutil.ReadFile(viper.GetString("daemon_pid"))
@@ -33,21 +32,21 @@ func startDaemon() {
3332
if start {
3433
e, err := os.Executable()
3534
if err != nil {
36-
log.Fatal(err)
35+
exit(err)
3736
}
3837
c := exec.Command(e, "daemon", "start")
3938
c.SysProcAttr = sysProcAttr
4039
f, err := os.OpenFile(viper.GetString("log_file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
4140
if err != nil {
42-
log.Fatal(err)
41+
exit(err)
4342
}
4443
c.Stdout = f
4544
c.Stderr = f
4645
if err := c.Start(); err != nil {
47-
log.Fatal(err)
46+
exit(err)
4847
}
4948
if err := ioutil.WriteFile(viper.GetString("daemon_pid"), []byte(strconv.Itoa(c.Process.Pid)), 0644); err != nil {
50-
log.Fatal(err)
49+
exit(err)
5150
}
5251
time.Sleep(time.Second) // Let the daemon start
5352
}
@@ -68,14 +67,14 @@ var daemonCmd = &cobra.Command{
6867
} else if args[0] == "stop" {
6968
c, err := getClient()
7069
if err != nil {
71-
log.Fatal(err)
70+
exit(err)
7271
}
7372
defer c.Close()
7473
req := new(daemon.StopDaemonRequest)
7574
rep := new(daemon.StopDaemonReply)
7675
err = c.Call("Server.StopDaemon", req, rep)
7776
if err != nil {
78-
log.Fatal(err)
77+
exit(err)
7978
}
8079
}
8180
},

cmd/ping.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/spf13/cobra"
55
"github.com/spf13/viper"
66
"net"
7-
"os"
87
"time"
98
)
109

@@ -15,7 +14,7 @@ var pingCmd = &cobra.Command{
1514
host := viper.GetString("host")
1615
conn, err := net.DialTimeout("tcp", host, 5*time.Second)
1716
if err != nil {
18-
os.Exit(1)
17+
exit(err)
1918
}
2019
defer conn.Close()
2120
},

cmd/ps.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"github.com/hako/durafmt"
55
"github.com/jedib0t/go-pretty/table"
6-
"github.com/labstack/gommon/log"
76
"github.com/labstack/tunnel-client/daemon"
87
"github.com/spf13/cobra"
98
"os"
@@ -22,15 +21,15 @@ func psRPC() {
2221
startDaemon()
2322
c, err := getClient()
2423
if err != nil {
25-
log.Fatal(err)
24+
exit(err)
2625
}
2726
defer c.Close()
2827
req := new(daemon.PSRequest)
2928
rep := new(daemon.PSReply)
3029
s.Start()
3130
err = c.Call("Server.PS", req, rep)
3231
if err != nil {
33-
log.Fatal(err)
32+
exit(err)
3433
}
3534
s.Stop()
3635
tbl := table.NewWriter()

cmd/rm.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"github.com/labstack/gommon/log"
65
"github.com/labstack/tunnel-client/daemon"
76

87
"github.com/spf13/cobra"
@@ -22,7 +21,7 @@ var rmCmd = &cobra.Command{
2221
startDaemon()
2322
c, err := getClient()
2423
if err != nil {
25-
log.Fatal(err)
24+
exit(err)
2625
}
2726
defer c.Close()
2827
rep := new(daemon.RMReply)
@@ -33,7 +32,7 @@ var rmCmd = &cobra.Command{
3332
Force: force,
3433
}, rep)
3534
if err != nil {
36-
log.Fatal(err)
35+
exit(err)
3736
}
3837
psRPC()
3938
},

cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/labstack/gommon/log"
65
"os"
76
"path/filepath"
87

@@ -25,7 +24,7 @@ var rootCmd = &cobra.Command{
2524

2625
func Execute() {
2726
if err := rootCmd.Execute(); err != nil {
28-
log.Fatal(err)
27+
exit(err)
2928
}
3029
}
3130

cmd/start.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"github.com/labstack/gommon/log"
65
"github.com/labstack/tunnel-client/daemon"
76

87
"github.com/spf13/cobra"
@@ -21,7 +20,7 @@ var startCmd = &cobra.Command{
2120
startDaemon()
2221
c, err := getClient()
2322
if err != nil {
24-
log.Fatal(err)
23+
exit(err)
2524
}
2625
defer c.Close()
2726
rep := new(daemon.StartReply)
@@ -31,7 +30,7 @@ var startCmd = &cobra.Command{
3130
ID: args[0],
3231
}, rep)
3332
if err != nil {
34-
log.Fatal(err)
33+
exit(err)
3534
}
3635
psRPC()
3736
},

cmd/stop.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"github.com/labstack/gommon/log"
65
"github.com/labstack/tunnel-client/daemon"
76

87
"github.com/spf13/cobra"
@@ -21,7 +20,7 @@ var stopCmd = &cobra.Command{
2120
startDaemon()
2221
c, err := getClient()
2322
if err != nil {
24-
log.Fatal(err)
23+
exit(err)
2524
}
2625
defer c.Close()
2726
rep := new(daemon.StopReply)
@@ -31,7 +30,7 @@ var stopCmd = &cobra.Command{
3130
ID: args[0],
3231
}, rep)
3332
if err != nil {
34-
log.Fatal(err)
33+
exit(err)
3534
}
3635
psRPC()
3736
},

0 commit comments

Comments
 (0)