refactor(config): eliminate ErupeConfig global variable

Replace the mutable global `_config.ErupeConfig` with dependency
injection across 79 files. Config is now threaded through existing
paths: `ClientContext.RealClientMode` for packet encoding, `s.server.
erupeConfig` for channel handlers, and explicit parameters for utility
functions. This removes hidden coupling, enables test parallelism
without global save/restore, and prevents low-level packages from
reaching up to the config layer.

Key changes:
- Enrich ClientContext with RealClientMode for packet files
- Add mode parameter to CryptConn, mhfitem, mhfcourse functions
- Convert handlers_commands init() to lazy sync.Once initialization
- Delete global var, init(), and helper functions from config.go
- Update all tests to pass config explicitly
This commit is contained in:
Houmgaor
2026-02-20 17:07:42 +01:00
parent 8c7e95ce18
commit 5f3c843082
79 changed files with 509 additions and 723 deletions

View File

@@ -1,12 +1,9 @@
package _config
import (
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/spf13/viper"
)
@@ -308,36 +305,6 @@ func (c *EntranceChannelInfo) IsEnabled() bool {
return *c.Enabled
}
var ErupeConfig *Config
func init() {
var err error
ErupeConfig, err = LoadConfig()
if err != nil {
// In test environments or when config.toml is missing, use defaults
ErupeConfig = &Config{
ClientMode: "ZZ",
RealClientMode: ZZ,
}
// Only call preventClose if it's not a test environment
if !isTestEnvironment() {
preventClose(fmt.Sprintf("Failed to load config: %s", err.Error()))
}
}
}
func isTestEnvironment() bool {
// Check if we're running under test
for _, arg := range os.Args {
if arg == "-test.v" || arg == "-test.run" || arg == "-test.timeout" {
return true
}
if strings.Contains(arg, "test") {
return true
}
}
return false
}
// getOutboundIP4 gets the preferred outbound ip4 of this machine
// From https://stackoverflow.com/a/37382208
@@ -399,19 +366,3 @@ func LoadConfig() (*Config, error) {
return c, nil
}
func preventClose(text string) {
if ErupeConfig != nil && ErupeConfig.DisableSoftCrash {
os.Exit(0)
}
fmt.Println("\nFailed to start Erupe:\n" + text)
go wait()
fmt.Println("\nPress Enter/Return to exit...")
_, _ = fmt.Scanln()
os.Exit(0)
}
func wait() {
for {
time.Sleep(time.Millisecond * 100)
}
}

View File

@@ -94,14 +94,6 @@ func TestModeConstants(t *testing.T) {
}
}
// TestIsTestEnvironment tests the isTestEnvironment function
func TestIsTestEnvironment(t *testing.T) {
result := isTestEnvironment()
if !result {
t.Error("isTestEnvironment() should return true when running tests")
}
}
// TestVersionStringsLength verifies versionStrings has correct length
func TestVersionStringsLength(t *testing.T) {
expectedCount := 41 // S1 through ZZ = 41 versions
@@ -708,10 +700,3 @@ func BenchmarkGetOutboundIP4(b *testing.B) {
}
}
// BenchmarkIsTestEnvironment benchmarks test environment detection
func BenchmarkIsTestEnvironment(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = isTestEnvironment()
}
}