fix(lint): 修复所有 lint 错误

- 为导出的函数添加注释 (revive)
- 检查 os.Stdout.Write 返回值 (errcheck)
- 重命名 err 变量避免 shadow declaration (govet)
- 使用 tagged switch 替代 if-else (staticcheck QF1003)
- 用 append 展开替换循环 (staticcheck S1011)
- 添加常量消除重复字符串 (goconst)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-27 10:20:01 +08:00
parent 13a2a3d8b2
commit 1f672d1a7a
4 changed files with 34 additions and 24 deletions

View File

@ -36,16 +36,19 @@ type App struct {
listeners []net.Listener
}
// NewApp creates a new App instance with the given config path.
func NewApp(cfgPath string) *App {
return &App{
cfgPath: cfgPath,
}
}
// SetPidFile sets the path to the PID file for the app.
func (a *App) SetPidFile(path string) {
a.pidFile = path
}
// SetLogFile sets the path to the log file for the app.
func (a *App) SetLogFile(path string) {
a.logFile = path
}

View File

@ -79,8 +79,8 @@ func importNginxConfig(path, outputPath string) error {
fmt.Fprintf(os.Stderr, "warning: %s:line %d: %s\n", w.File, w.Line, w.Message)
}
if err := config.Validate(result.Config); err != nil {
return fmt.Errorf("转换后配置验证失败: %w", err)
if validateErr := config.Validate(result.Config); validateErr != nil {
return fmt.Errorf("转换后配置验证失败: %w", validateErr)
}
yamlData, err := yaml.Marshal(result.Config)
@ -89,7 +89,9 @@ func importNginxConfig(path, outputPath string) error {
}
if outputPath == "" {
os.Stdout.Write(yamlData)
if _, err := os.Stdout.Write(yamlData); err != nil {
return fmt.Errorf("写入标准输出失败: %w", err)
}
} else {
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
return fmt.Errorf("创建输出目录失败: %w", err)

View File

@ -10,6 +10,12 @@ import (
"rua.plus/lolly/internal/config"
)
const (
gzipType = "gzip"
offValue = "off"
redirectType = "redirect"
)
// Warning represents a conversion warning for unsupported or partially supported directives.
type Warning struct {
Directive string
@ -44,18 +50,18 @@ type locationClassification struct {
// unsupportedDirectives are known nginx directives that have no lolly equivalent.
var unsupportedDirectives = map[string]string{
"if": "the 'if' directive is not supported; consider using map or rewrite",
"map": "the 'map' directive is not supported; use variables config instead",
"set": "the 'set' directive is not supported; use variables config instead",
"limit_req": "the 'limit_req' directive is not supported; use rate_limit config instead",
"limit_conn": "the 'limit_conn' directive is not supported",
"add_header": "the 'add_header' directive is not supported; use security.headers config instead",
"more_set_headers": "the 'more_set_headers' directive is not supported; use security.headers config instead",
"auth_request": "the 'auth_request' directive is not supported; use security.auth_request config instead",
"split_clients": "the 'split_clients' directive is not supported",
"geo": "the 'geo' directive is not supported; use access.geoip config instead",
"range": "the 'range' directive is not supported",
"return": "the 'return' directive is not supported for non-redirect status codes; only 301/302 are supported",
"if": "the 'if' directive is not supported; consider using map or rewrite",
"map": "the 'map' directive is not supported; use variables config instead",
"set": "the 'set' directive is not supported; use variables config instead",
"limit_req": "the 'limit_req' directive is not supported; use rate_limit config instead",
"limit_conn": "the 'limit_conn' directive is not supported",
"add_header": "the 'add_header' directive is not supported; use security.headers config instead",
"more_set_headers": "the 'more_set_headers' directive is not supported; use security.headers config instead",
"auth_request": "the 'auth_request' directive is not supported; use security.auth_request config instead",
"split_clients": "the 'split_clients' directive is not supported",
"geo": "the 'geo' directive is not supported; use access.geoip config instead",
"range": "the 'range' directive is not supported",
"return": "the 'return' directive is not supported for non-redirect status codes; only 301/302 are supported",
}
// Convert converts a parsed nginx configuration to a lolly configuration.
@ -91,7 +97,8 @@ func Convert(nginxCfg *NginxConfig) (*ConvertResult, error) {
var serverBlocks []Directive
for i := range nginxCfg.Directives {
d := &nginxCfg.Directives[i]
if d.Name == "http" {
switch d.Name {
case "http":
// Check for unsupported directives at the http level.
for j := range d.Block {
bd := &d.Block[j]
@ -106,7 +113,7 @@ func Convert(nginxCfg *NginxConfig) (*ConvertResult, error) {
})
}
}
} else if d.Name == "server" {
case "server":
serverBlocks = append(serverBlocks, *d)
}
}
@ -207,7 +214,7 @@ func convertServerBlock(d *Directive, upstreams map[string]*upstreamInfo, result
if len(bd.Args) > 0 {
server.SSL.Key = bd.Args[0]
}
case "gzip":
case gzipType:
parseGzip(bd, &server)
case "gzip_types":
server.Compression.Types = bd.Args
@ -223,7 +230,7 @@ func convertServerBlock(d *Directive, upstreams map[string]*upstreamInfo, result
}
case "server_tokens":
if len(bd.Args) > 0 {
server.ServerTokens = bd.Args[0] != "off"
server.ServerTokens = bd.Args[0] != offValue
}
case "access_log":
parseAccessLog(bd, result)
@ -441,7 +448,7 @@ func parseErrorPage(d *Directive, server *config.ServerConfig) {
// parseAuthBasic parses an auth_basic directive.
func parseAuthBasic(d *Directive, server *config.ServerConfig) {
if len(d.Args) > 0 {
if d.Args[0] != "off" {
if d.Args[0] != offValue {
server.Security.Auth.Type = "basic"
server.Security.Auth.Realm = d.Args[0]
}
@ -508,7 +515,7 @@ func classifyLocation(d *Directive, result *ConvertResult) locationClassificatio
case hasRootOrAlias:
class.LocType = "static"
case hasRedirect:
class.LocType = "redirect"
class.LocType = redirectType
default:
class.LocType = "unsupported"
}

View File

@ -127,9 +127,7 @@ func (p *parser) parseDirectives() ([]Directive, error) {
directives = append(directives, *d)
// Drain any extra directives injected by include expansion.
for _, extra := range p.extraDirectives {
directives = append(directives, extra)
}
directives = append(directives, p.extraDirectives...)
p.extraDirectives = nil
}
return directives, nil