Просмотр исходного кода

Add doctor command for deprecated config values

tags/v2.2.4^2
9seconds 1 месяц назад
Родитель
Сommit
63b147c287
2 измененных файлов: 106 добавлений и 0 удалений
  1. 1
    0
      internal/cli/cli.go
  2. 105
    0
      internal/cli/doctor.go

+ 1
- 0
internal/cli/cli.go Просмотреть файл

@@ -4,6 +4,7 @@ import "github.com/alecthomas/kong"
4 4
 
5 5
 type CLI struct {
6 6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
7
+	Doctor         Doctor           `kong:"cmd,help='Check that proxy can run correctly'"`
7 8
 	Access         Access           `kong:"cmd,help='Print access information.'"`
8 9
 	Run            Run              `kong:"cmd,help='Run proxy.'"`
9 10
 	SimpleRun      SimpleRun        `kong:"cmd,help='Run proxy without config file.'"`

+ 105
- 0
internal/cli/doctor.go Просмотреть файл

@@ -0,0 +1,105 @@
1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"os"
6
+	"strings"
7
+	"text/template"
8
+
9
+	"github.com/9seconds/mtg/v2/internal/config"
10
+	"github.com/9seconds/mtg/v2/internal/utils"
11
+)
12
+
13
+var (
14
+	tplWDeprecatedConfig = template.Must(
15
+		template.New("deprecated-config").
16
+			Parse(`  ⚠️ Option {{ .old | printf "%q" }}{{ if .old_section }} from section [{{ .old_section }}]{{ end }} is deprecated and will be removed in v{{ .when }}. Please use {{ .new | printf "%q" }}{{ if .new_section }} in [{{ .new_section }}] section{{ end }} instead.`),
17
+	)
18
+)
19
+
20
+type Doctor struct {
21
+	conf *config.Config
22
+
23
+	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
24
+}
25
+
26
+func (d *Doctor) Run(cli *CLI, version string) error {
27
+	conf, err := utils.ReadConfig(d.ConfigPath)
28
+	if err != nil {
29
+		return fmt.Errorf("cannot init config: %w", err)
30
+	}
31
+
32
+	d.conf = conf
33
+	everythingOK := true
34
+
35
+	fmt.Println("Deprecated options")
36
+	if errs := d.checkDeprecatedConfig(); len(errs) > 0 {
37
+		for _, err := range errs {
38
+			fmt.Println(err)
39
+			everythingOK = false
40
+		}
41
+	} else {
42
+		fmt.Println("  ✅ All good")
43
+	}
44
+
45
+	if !everythingOK {
46
+		os.Exit(1)
47
+	}
48
+
49
+	return nil
50
+}
51
+
52
+func (d *Doctor) checkDeprecatedConfig() []string {
53
+	errors := []string{}
54
+
55
+	if d.conf.DomainFrontingIP.Value != nil {
56
+		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
57
+			"when":        "2.3.0",
58
+			"old":         "domain-fronting-ip",
59
+			"old_section": "",
60
+			"new":         "ip",
61
+			"new_section": "domain-fronting",
62
+		})
63
+	}
64
+
65
+	if d.conf.DomainFrontingPort.Value != 0 {
66
+		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
67
+			"when":        "2.3.0",
68
+			"old":         "domain-fronting-port",
69
+			"old_section": "",
70
+			"new":         "port",
71
+			"new_section": "domain-fronting",
72
+		})
73
+	}
74
+
75
+	if d.conf.DomainFrontingProxyProtocol.Value {
76
+		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
77
+			"when":        "2.3.0",
78
+			"old":         "domain-fronting-proxy-protocol",
79
+			"old_section": "",
80
+			"new":         "proxy-protocol",
81
+			"new_section": "domain-fronting",
82
+		})
83
+	}
84
+
85
+	if d.conf.Network.DOHIP.Value != nil {
86
+		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
87
+			"when":        "2.3.0",
88
+			"old":         "doh-ip",
89
+			"old_section": "network",
90
+			"new":         "dns",
91
+			"new_section": "network",
92
+		})
93
+	}
94
+
95
+	return errors
96
+}
97
+
98
+func (d *Doctor) addError(messages []string, tpl *template.Template, context map[string]string) []string {
99
+	value := &strings.Builder{}
100
+	if err := tpl.Execute(value, context); err != nil {
101
+		panic(err)
102
+	}
103
+
104
+	return append(messages, value.String())
105
+}

Загрузка…
Отмена
Сохранить