mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(config): copylocks and staticcheck findings (#1261)
refactor: copylocks and staticcheck findings Refactor mutex handling, optimize byte slice creation, and clean up code in accordance with go.staticcheck and copylocks guidelines. - Changed AppConfig's mutex to *sync.Mutex, preventing mutex copying and enhancing thread safety. - Simplified byte slice initialization in writeConfig for efficiency. - Removed redundant return in DynamicReload for better code clarity.
This commit is contained in:
parent
76d749b301
commit
d8c977b5ba
2 changed files with 5 additions and 7 deletions
|
@ -117,7 +117,7 @@ func (c *AppConfig) writeConfig(configPath string, configFile string) error {
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
} else if pd, _ := os.Open("/proc/1/cgroup"); pd != nil {
|
} else if pd, _ := os.Open("/proc/1/cgroup"); pd != nil {
|
||||||
defer pd.Close()
|
defer pd.Close()
|
||||||
b := make([]byte, 4096, 4096)
|
b := make([]byte, 4096)
|
||||||
pd.Read(b)
|
pd.Read(b)
|
||||||
if strings.Contains(string(b), "/docker") || strings.Contains(string(b), "/lxc") {
|
if strings.Contains(string(b), "/docker") || strings.Contains(string(b), "/lxc") {
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
|
@ -166,7 +166,7 @@ type Config interface {
|
||||||
|
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
Config *domain.Config
|
Config *domain.Config
|
||||||
m sync.Mutex
|
m *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(configPath string, version string) *AppConfig {
|
func New(configPath string, version string) *AppConfig {
|
||||||
|
@ -275,8 +275,6 @@ func (c *AppConfig) DynamicReload(log logger.Logger) {
|
||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
})
|
})
|
||||||
viper.WatchConfig()
|
viper.WatchConfig()
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AppConfig) UpdateConfig() error {
|
func (c *AppConfig) UpdateConfig() error {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
func TestAppConfig_processLines(t *testing.T) {
|
func TestAppConfig_processLines(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
Config *domain.Config
|
Config *domain.Config
|
||||||
m sync.Mutex
|
m *sync.Mutex
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
lines []string
|
lines []string
|
||||||
|
@ -30,7 +30,7 @@ func TestAppConfig_processLines(t *testing.T) {
|
||||||
name: "append missing",
|
name: "append missing",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
|
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
|
||||||
m: sync.Mutex{},
|
m: new(sync.Mutex),
|
||||||
},
|
},
|
||||||
args: args{[]string{}},
|
args: args{[]string{}},
|
||||||
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
|
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
|
||||||
|
@ -39,7 +39,7 @@ func TestAppConfig_processLines(t *testing.T) {
|
||||||
name: "update existing",
|
name: "update existing",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
|
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
|
||||||
m: sync.Mutex{},
|
m: new(sync.Mutex),
|
||||||
},
|
},
|
||||||
args: args{[]string{"# Check for updates", "#", "checkForUpdates = false", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""}},
|
args: args{[]string{"# Check for updates", "#", "checkForUpdates = false", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""}},
|
||||||
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
|
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue