|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "fmt" |
6 | | - "net" |
7 | | - "os" |
8 | | - "path/filepath" |
9 | | - "strings" |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
10 | 10 |
|
11 | | - "github.com/labstack/tunnel-client/daemon" |
12 | | - "github.com/spf13/cobra" |
| 11 | + "github.com/labstack/tunnel-client/daemon" |
| 12 | + "github.com/spf13/cobra" |
13 | 13 |
|
14 | | - "github.com/mitchellh/go-homedir" |
15 | | - "github.com/spf13/viper" |
| 14 | + "github.com/mitchellh/go-homedir" |
| 15 | + "github.com/spf13/viper" |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | var configuration string |
19 | 19 | var protocol string |
20 | 20 | var rootCmd = &cobra.Command{ |
21 | | - Use: "tunnel [address]", |
22 | | - Short: "Tunnel lets you expose local servers to the internet securely", |
23 | | - Long: "Signup @ https://tunnel.labstack.com to get an api key and get started", |
24 | | - Args: func(cmd *cobra.Command, args []string) error { |
25 | | - if len(args) < 1 { |
26 | | - return errors.New("requires a target address") |
27 | | - } |
28 | | - return nil |
29 | | - }, |
30 | | - Run: func(cmd *cobra.Command, args []string) { |
31 | | - s.Start() |
32 | | - startDaemon() |
33 | | - c, err := getClient() |
34 | | - if err != nil { |
35 | | - exit(err) |
36 | | - } |
37 | | - defer c.Close() |
38 | | - rep := new(daemon.ConnectReply) |
39 | | - addr := args[0] |
40 | | - _, _, err = net.SplitHostPort(addr) |
41 | | - if err != nil && strings.Contains(err.Error(), "missing port") { |
42 | | - addr = ":" + addr |
43 | | - } |
44 | | - err = c.Call("Server.Connect", &daemon.ConnectRequest{ |
45 | | - Configuration: configuration, |
46 | | - Address: addr, |
47 | | - Protocol: daemon.Protocol(protocol), |
48 | | - }, rep) |
49 | | - if err != nil { |
50 | | - exit(err) |
51 | | - } |
52 | | - s.Stop() |
53 | | - psRPC() |
54 | | - }, |
| 21 | + Use: "tunnel [address]", |
| 22 | + Short: "Tunnel lets you expose local servers to the internet securely", |
| 23 | + Long: "Signup @ https://tunnel.labstack.com to get an api key and get started", |
| 24 | + Args: func(cmd *cobra.Command, args []string) error { |
| 25 | + if len(args) < 1 { |
| 26 | + return errors.New("requires a target address") |
| 27 | + } |
| 28 | + return nil |
| 29 | + }, |
| 30 | + Run: func(cmd *cobra.Command, args []string) { |
| 31 | + s.Start() |
| 32 | + startDaemon() |
| 33 | + c, err := getClient() |
| 34 | + if err != nil { |
| 35 | + exit(err) |
| 36 | + } |
| 37 | + defer c.Close() |
| 38 | + rep := new(daemon.ConnectReply) |
| 39 | + addr := args[0] |
| 40 | + _, _, err = net.SplitHostPort(addr) |
| 41 | + if err != nil && strings.Contains(err.Error(), "missing port") { |
| 42 | + addr = ":" + addr |
| 43 | + } |
| 44 | + err = c.Call("Server.Connect", &daemon.ConnectRequest{ |
| 45 | + Configuration: configuration, |
| 46 | + Address: addr, |
| 47 | + Protocol: daemon.Protocol(protocol), |
| 48 | + }, rep) |
| 49 | + if err != nil { |
| 50 | + exit(err) |
| 51 | + } |
| 52 | + s.Stop() |
| 53 | + psRPC() |
| 54 | + }, |
55 | 55 | } |
56 | 56 |
|
57 | 57 | func Execute() { |
58 | | - if err := rootCmd.Execute(); err != nil { |
59 | | - exit(err) |
60 | | - } |
| 58 | + if err := rootCmd.Execute(); err != nil { |
| 59 | + exit(err) |
| 60 | + } |
61 | 61 | } |
62 | 62 |
|
63 | 63 | func init() { |
64 | | - cobra.OnInitialize(initialize) |
| 64 | + cobra.OnInitialize(initialize) |
65 | 65 | } |
66 | 66 |
|
67 | 67 | func initialize() { |
68 | | - // Create directories |
69 | | - dir, err := homedir.Dir() |
70 | | - if err != nil { |
71 | | - fmt.Printf("failed to find the home directory: %v", err) |
72 | | - } |
73 | | - root := filepath.Join(dir, ".tunnel") |
74 | | - if err = os.MkdirAll(root, 0755); err != nil { |
75 | | - fmt.Printf("failed to create root directory: %v", err) |
76 | | - } |
77 | | - if _, err := os.OpenFile(filepath.Join(root, "config.yaml"), os.O_RDONLY|os.O_CREATE, 0644); err != nil { |
78 | | - fmt.Printf("failed to create config file: %v", err) |
79 | | - } |
| 68 | + // Create directories |
| 69 | + dir, err := homedir.Dir() |
| 70 | + if err != nil { |
| 71 | + fmt.Printf("failed to find the home directory: %v", err) |
| 72 | + } |
| 73 | + root := filepath.Join(dir, ".tunnel") |
| 74 | + if err = os.MkdirAll(root, 0755); err != nil { |
| 75 | + fmt.Printf("failed to create root directory: %v", err) |
| 76 | + } |
| 77 | + if _, err := os.OpenFile(filepath.Join(root, "config.yaml"), os.O_RDONLY|os.O_CREATE, 0644); err != nil { |
| 78 | + fmt.Printf("failed to create config file: %v", err) |
| 79 | + } |
80 | 80 |
|
81 | | - // Config |
82 | | - viper.AutomaticEnv() |
83 | | - viper.Set("root", root) |
84 | | - viper.Set("log_file", filepath.Join(root, "daemon.log")) |
85 | | - viper.Set("daemon_pid", filepath.Join(root, "daemon.pid")) |
86 | | - viper.Set("daemon_addr", filepath.Join(root, "daemon.addr")) |
87 | | - viper.Set("hostname", "labstack.me") |
88 | | - viper.Set("api_url", "https://tunnel.labstack.com/api/v1") |
89 | | - if dev := viper.GetString("DC") == "dev"; dev { |
90 | | - viper.Set("host", "labstack.d:22") |
91 | | - viper.Set("api_url", "http://tunnel.labstack.d/api/v1") |
92 | | - viper.SetConfigName("config.dev") |
93 | | - } else { |
94 | | - viper.SetConfigName("config") |
95 | | - } |
96 | | - viper.AddConfigPath(root) |
97 | | - viper.ReadInConfig() |
98 | | - viper.WatchConfig() |
| 81 | + // Config |
| 82 | + viper.AutomaticEnv() |
| 83 | + viper.Set("root", root) |
| 84 | + viper.Set("log_file", filepath.Join(root, "daemon.log")) |
| 85 | + viper.Set("daemon_pid", filepath.Join(root, "daemon.pid")) |
| 86 | + viper.Set("daemon_addr", filepath.Join(root, "daemon.addr")) |
| 87 | + viper.Set("hostname", "labstack.me") |
| 88 | + viper.Set("api_url", "https://tunnel.labstack.com/api/v1") |
| 89 | + if dev := viper.GetString("DC") == "dev"; dev { |
| 90 | + viper.Set("host", "labstack.d:22") |
| 91 | + viper.Set("api_url", "http://tunnel.labstack.d/api/v1") |
| 92 | + viper.SetConfigName("config.dev") |
| 93 | + } else { |
| 94 | + viper.SetConfigName("config") |
| 95 | + } |
| 96 | + viper.AddConfigPath(root) |
| 97 | + viper.ReadInConfig() |
| 98 | + viper.WatchConfig() |
99 | 99 | } |
100 | 100 |
|
101 | 101 | func init() { |
102 | | - rootCmd.PersistentFlags().StringVarP(&configuration, "configuration", "c", "", |
103 | | - "configuration name from the console") |
104 | | - rootCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", daemon.ProtocolHTTP, |
105 | | - "connection protocol (http, tcp, tls)") |
| 102 | + rootCmd.PersistentFlags().StringVarP(&configuration, "configuration", "c", "", |
| 103 | + "configuration name from the console") |
| 104 | + rootCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", string(daemon.ProtocolHTTP), |
| 105 | + "connection protocol (http, tcp, tls)") |
106 | 106 | } |
0 commit comments