Files
opencode2api/internal/config/proxy.go
T
Jason Xu 46def7fedc refactor: organize Go packages and remove test files
Move the command to cmd/opencode2api and group production code into
configuration, gateway, protocol, model, admin, telemetry and helper packages.
Keep shared state private behind context, metrics and runtime interfaces.

Remove the requested test files and replace test-only CI jobs with vet/build
checks. Update Docker and release build paths, preserve version injection and
make the WebUI an explicit embedded asset package.

Add directory trees and source reading guides to both READMEs. Anchor binary
ignore rules so the command package is tracked.
2026-09-15 01:28:44 +08:00

91 lines
1.9 KiB
Go

package config
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
func resolveProxyFiles(configPath string, cfg *Config) error {
trimList(&cfg.Proxies)
effective := append([]string(nil), cfg.Proxies...)
if cfg.ProxyFile != "" {
resolved := cfg.ProxyFile
if !filepath.IsAbs(resolved) {
resolved = filepath.Join(filepath.Dir(configPath), resolved)
}
proxies, err := readProxyFile(resolved)
if err != nil {
return fmt.Errorf("load proxy file %s: %w", resolved, err)
}
effective = append(effective, proxies...)
}
effective = uniqueStrings(effective)
if len(effective) == 0 {
effective = []string{"direct"}
}
cfg.effectiveProxies = effective
return nil
}
func readProxyFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var proxies []string
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 4096), 1024*1024)
for scanner.Scan() {
value := strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "\ufeff"))
value = strings.TrimSpace(stripProxyLineComment(value))
if value != "" {
proxies = append(proxies, value)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return proxies, nil
}
func stripProxyLineComment(line string) string {
for i := 0; i < len(line); i++ {
if i > 0 && line[i-1] != ' ' && line[i-1] != '\t' {
continue
}
if line[i] == '#' || line[i] == ';' || (line[i] == '/' && i+1 < len(line) && line[i+1] == '/') {
return line[:i]
}
}
return line
}
func uniqueStrings(items []string) []string {
out := items[:0]
seen := make(map[string]struct{}, len(items))
for _, item := range items {
if _, exists := seen[item]; exists {
continue
}
seen[item] = struct{}{}
out = append(out, item)
}
return out
}
func trimList(items *[]string) {
out := (*items)[:0]
for _, item := range *items {
if value := strings.TrimSpace(item); value != "" {
out = append(out, value)
}
}
*items = out
}