Pārlūkot izejas kodu

Use custom cli classes

tags/v2.0.0-rc1
9seconds 5 gadus atpakaļ
vecāks
revīzija
94084674c0
4 mainītis faili ar 34 papildinājumiem un 44 dzēšanām
  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 Parādīt failu

@@ -3,6 +3,7 @@ package main
3 3
 import (
4 4
 	"context"
5 5
 	"encoding/json"
6
+	"fmt"
6 7
 	"io/ioutil"
7 8
 	"net"
8 9
 	"net/http"
@@ -31,38 +32,43 @@ type runAccessResponseURLs struct {
31 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 41
 	filefp, err := os.Open(cli.Access.ConfigPath)
36 42
 	if err != nil {
37
-		exit(err)
43
+		return fmt.Errorf("cannot open config file: %w", err)
38 44
 	}
39 45
 
40 46
 	defer filefp.Close()
41 47
 
42 48
 	conf, err := parseConfig(filefp)
43 49
 	if err != nil {
44
-		exit(err)
50
+		return fmt.Errorf("cannot parse config: %w", err)
45 51
 	}
46 52
 
47 53
 	ntw, err := makeNetwork(conf)
48 54
 	if err != nil {
49
-		exit(err)
55
+		return fmt.Errorf("cannot build a network: %w", err)
50 56
 	}
51 57
 
52 58
 	ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
53 59
 	ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
54 60
 
55 61
 	if ipv4 == nil {
56
-		ipv4 = runAccessGetIP(ntw, "tcp4")
62
+		ipv4 = c.getIP(ntw, "tcp4")
57 63
 	}
58 64
 
59 65
 	if ipv6 == nil {
60
-		ipv6 = runAccessGetIP(ntw, "tcp6")
66
+		ipv6 = c.getIP(ntw, "tcp6")
61 67
 	}
62 68
 
63 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 73
 	resp.Secret.Base64 = conf.Secret.Base64()
68 74
 	resp.Secret.Hex = conf.Secret.Hex()
@@ -73,11 +79,13 @@ func runAccess(cli *CLI) {
73 79
 	encoder.SetIndent("", "  ")
74 80
 
75 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 89
 	client := &http.Client{
82 90
 		Timeout: ntw.HTTP.Timeout,
83 91
 		Transport: &http.Transport{
@@ -106,7 +114,7 @@ func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
106 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 118
 	if ip == nil {
111 119
 		return nil
112 120
 	}
@@ -139,13 +147,13 @@ func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResp
139 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 153
 	return rv
146 154
 }
147 155
 
148
-func runMakeAccessResponseURLsQRCode(data string) string {
156
+func (c *cliCommandAccess) makeResponseQRCode(data string) string {
149 157
 	values := url.Values{}
150 158
 
151 159
 	values.Set("qzone", "4")

+ 8
- 1
cli_generate_secret.go Parādīt failu

@@ -6,7 +6,12 @@ import (
6 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 15
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
11 16
 
12 17
 	if cli.GenerateSecret.Hex {
@@ -14,4 +19,6 @@ func runGenerateSecret(cli *CLI) {
14 19
 	} else {
15 20
 		fmt.Println(secret.Base64()) // nolint: forbidigo
16 21
 	}
22
+
23
+	return nil
17 24
 }

+ 4
- 23
main.go Parādīt failu

@@ -10,18 +10,9 @@ import (
10 10
 var version = "dev" // has to be set by ldflags
11 11
 
12 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 18
 func main() {
@@ -30,18 +21,8 @@ func main() {
30 21
 	cli := &CLI{}
31 22
 	ctx := kong.Parse(cli, kong.Vars{
32 23
 		"domain_front": "amazonaws.com",
33
-		"config_path":  "/etc/mtg.toml",
34 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 Parādīt failu

@@ -7,16 +7,10 @@ import (
7 7
 	"net"
8 8
 	"net/http"
9 9
 	"net/url"
10
-	"os"
11 10
 
12 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 14
 func makeNetwork(conf *config) (*network.Network, error) {
21 15
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
22 16
 	httpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultHTTPTimeout)

Notiek ielāde…
Atcelt
Saglabāt