Browse Source

Use custom cli classes

tags/v2.0.0-rc1
9seconds 5 years ago
parent
commit
94084674c0
4 changed files with 34 additions and 44 deletions
  1. 22
    14
      cli_access.go
  2. 8
    1
      cli_generate_secret.go
  3. 4
    23
      main.go
  4. 0
    6
      utils.go

+ 22
- 14
cli_access.go View File

3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
 	"encoding/json"
5
 	"encoding/json"
6
+	"fmt"
6
 	"io/ioutil"
7
 	"io/ioutil"
7
 	"net"
8
 	"net"
8
 	"net/http"
9
 	"net/http"
31
 	TmeQrCode string `json:"tme_qrcode"`
32
 	TmeQrCode string `json:"tme_qrcode"`
32
 }
33
 }
33
 
34
 
34
-func runAccess(cli *CLI) {
35
+type cliCommandAccess struct {
36
+	ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
37
+	Hex        bool   `help:"Print secret in hex encoding."`
38
+}
39
+
40
+func (c *cliCommandAccess) Run(cli *CLI) error {
35
 	filefp, err := os.Open(cli.Access.ConfigPath)
41
 	filefp, err := os.Open(cli.Access.ConfigPath)
36
 	if err != nil {
42
 	if err != nil {
37
-		exit(err)
43
+		return fmt.Errorf("cannot open config file: %w", err)
38
 	}
44
 	}
39
 
45
 
40
 	defer filefp.Close()
46
 	defer filefp.Close()
41
 
47
 
42
 	conf, err := parseConfig(filefp)
48
 	conf, err := parseConfig(filefp)
43
 	if err != nil {
49
 	if err != nil {
44
-		exit(err)
50
+		return fmt.Errorf("cannot parse config: %w", err)
45
 	}
51
 	}
46
 
52
 
47
 	ntw, err := makeNetwork(conf)
53
 	ntw, err := makeNetwork(conf)
48
 	if err != nil {
54
 	if err != nil {
49
-		exit(err)
55
+		return fmt.Errorf("cannot build a network: %w", err)
50
 	}
56
 	}
51
 
57
 
52
 	ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
58
 	ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
53
 	ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
59
 	ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
54
 
60
 
55
 	if ipv4 == nil {
61
 	if ipv4 == nil {
56
-		ipv4 = runAccessGetIP(ntw, "tcp4")
62
+		ipv4 = c.getIP(ntw, "tcp4")
57
 	}
63
 	}
58
 
64
 
59
 	if ipv6 == nil {
65
 	if ipv6 == nil {
60
-		ipv6 = runAccessGetIP(ntw, "tcp6")
66
+		ipv6 = c.getIP(ntw, "tcp6")
61
 	}
67
 	}
62
 
68
 
63
 	resp := runAccessResponse{
69
 	resp := runAccessResponse{
64
-		IPv4: runMakeAccessResponseURLs(ipv4, conf, cli),
65
-		IPv6: runMakeAccessResponseURLs(ipv6, conf, cli),
70
+		IPv4: c.makeResponseURLs(ipv4, conf, cli),
71
+		IPv6: c.makeResponseURLs(ipv6, conf, cli),
66
 	}
72
 	}
67
 	resp.Secret.Base64 = conf.Secret.Base64()
73
 	resp.Secret.Base64 = conf.Secret.Base64()
68
 	resp.Secret.Hex = conf.Secret.Hex()
74
 	resp.Secret.Hex = conf.Secret.Hex()
73
 	encoder.SetIndent("", "  ")
79
 	encoder.SetIndent("", "  ")
74
 
80
 
75
 	if err := encoder.Encode(resp); err != nil {
81
 	if err := encoder.Encode(resp); err != nil {
76
-		exit(err)
82
+		return fmt.Errorf("cannot dump access json: %w", err)
77
 	}
83
 	}
84
+
85
+	return nil
78
 }
86
 }
79
 
87
 
80
-func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
88
+func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
81
 	client := &http.Client{
89
 	client := &http.Client{
82
 		Timeout: ntw.HTTP.Timeout,
90
 		Timeout: ntw.HTTP.Timeout,
83
 		Transport: &http.Transport{
91
 		Transport: &http.Transport{
106
 	return net.ParseIP(strings.TrimSpace(string(data)))
114
 	return net.ParseIP(strings.TrimSpace(string(data)))
107
 }
115
 }
108
 
116
 
109
-func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
117
+func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
110
 	if ip == nil {
118
 	if ip == nil {
111
 		return nil
119
 		return nil
112
 	}
120
 	}
139
 		}).String(),
147
 		}).String(),
140
 	}
148
 	}
141
 
149
 
142
-	rv.TgQrCode = runMakeAccessResponseURLsQRCode(rv.TgURL)
143
-	rv.TmeQrCode = runMakeAccessResponseURLsQRCode(rv.TmeURL)
150
+	rv.TgQrCode = c.makeResponseQRCode(rv.TgURL)
151
+	rv.TmeQrCode = c.makeResponseQRCode(rv.TmeURL)
144
 
152
 
145
 	return rv
153
 	return rv
146
 }
154
 }
147
 
155
 
148
-func runMakeAccessResponseURLsQRCode(data string) string {
156
+func (c *cliCommandAccess) makeResponseQRCode(data string) string {
149
 	values := url.Values{}
157
 	values := url.Values{}
150
 
158
 
151
 	values.Set("qzone", "4")
159
 	values.Set("qzone", "4")

+ 8
- 1
cli_generate_secret.go View File

6
 	"github.com/9seconds/mtg/v2/mtglib"
6
 	"github.com/9seconds/mtg/v2/mtglib"
7
 )
7
 )
8
 
8
 
9
-func runGenerateSecret(cli *CLI) {
9
+type cliCommandGenerateSecret struct {
10
+	HostName string `arg optional help:"Hostname to use for domain fronting. Default is '${domain_front}'." name:"hostname" default:"${domain_front}"` // nolint: lll, govet
11
+	Hex      bool   `help:"Print secret in hex encoding."`
12
+}
13
+
14
+func (c *cliCommandGenerateSecret) Run(cli *CLI) error { // nolint: unparam
10
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
15
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
11
 
16
 
12
 	if cli.GenerateSecret.Hex {
17
 	if cli.GenerateSecret.Hex {
14
 	} else {
19
 	} else {
15
 		fmt.Println(secret.Base64()) // nolint: forbidigo
20
 		fmt.Println(secret.Base64()) // nolint: forbidigo
16
 	}
21
 	}
22
+
23
+	return nil
17
 }
24
 }

+ 4
- 23
main.go View File

10
 var version = "dev" // has to be set by ldflags
10
 var version = "dev" // has to be set by ldflags
11
 
11
 
12
 type CLI struct {
12
 type CLI struct {
13
-	GenerateSecret struct { // nolint: govet
14
-		HostName string `arg optional help:"Hostname to use for domain fronting. Default is '${domain_front}'." name:"hostname" default:"${domain_front}"` // nolint: lll, govet
15
-		Hex      bool   `help:"Print secret in hex encoding."`
16
-	} `cmd help:"Generate new proxy secret."`
17
-	Access struct { // nolint: govet
18
-		ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
19
-		Hex        bool   `help:"Print secret in hex encoding."`
20
-	} `cmd help:"Print access information."`
21
-	Run struct { // nolint: govet
22
-		ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
23
-	} `cmd help:"Run proxy."`
24
-	Version kong.VersionFlag `help:"Print version."`
13
+	GenerateSecret cliCommandGenerateSecret `cmd help:"Generate new proxy secret"` // nolint: govet
14
+	Access         cliCommandAccess         `cmd help:"Print access information."` // nolint: govet
15
+	Version        kong.VersionFlag         `help:"Print version."`
25
 }
16
 }
26
 
17
 
27
 func main() {
18
 func main() {
30
 	cli := &CLI{}
21
 	cli := &CLI{}
31
 	ctx := kong.Parse(cli, kong.Vars{
22
 	ctx := kong.Parse(cli, kong.Vars{
32
 		"domain_front": "amazonaws.com",
23
 		"domain_front": "amazonaws.com",
33
-		"config_path":  "/etc/mtg.toml",
34
 		"version":      version,
24
 		"version":      version,
35
 	})
25
 	})
36
 
26
 
37
-	switch ctx.Command() {
38
-	case "generate-secret":
39
-		runGenerateSecret(cli)
40
-	case "access <config-path>":
41
-		runAccess(cli)
42
-	case "run <config-path>":
43
-		panic("not implemented yet")
44
-	default:
45
-		panic(ctx.Command())
46
-	}
27
+	ctx.FatalIfErrorf(ctx.Run(cli))
47
 }
28
 }

+ 0
- 6
utils.go View File

7
 	"net"
7
 	"net"
8
 	"net/http"
8
 	"net/http"
9
 	"net/url"
9
 	"net/url"
10
-	"os"
11
 
10
 
12
 	"github.com/9seconds/mtg/v2/mtglib/network"
11
 	"github.com/9seconds/mtg/v2/mtglib/network"
13
 )
12
 )
14
 
13
 
15
-func exit(err error) {
16
-	fmt.Fprintln(os.Stderr, err.Error())
17
-	os.Exit(1)
18
-}
19
-
20
 func makeNetwork(conf *config) (*network.Network, error) {
14
 func makeNetwork(conf *config) (*network.Network, error) {
21
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
15
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
22
 	httpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultHTTPTimeout)
16
 	httpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultHTTPTimeout)

Loading…
Cancel
Save