refactor: migrate db and config packages to use struct-based API
Removed global variables in favor of instance variables. This makes the code more maintainable and making it easier to write unit tests without relying on global state. Marked the old functions with global state as obsolete, redirecting them to use a new API based on struct in order to rewrite the code using these functions gradually.
This commit is contained in:
@ -21,59 +21,108 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"go.elara.ws/logger/log"
|
||||
"plemya-x.ru/alr/internal/types"
|
||||
"plemya-x.ru/alr/pkg/loggerctx"
|
||||
)
|
||||
|
||||
var defaultConfig = &types.Config{
|
||||
RootCmd: "sudo",
|
||||
PagerStyle: "native",
|
||||
IgnorePkgUpdates: []string{},
|
||||
Repos: []types.Repo{
|
||||
{
|
||||
Name: "default",
|
||||
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
|
||||
},
|
||||
},
|
||||
type ALRConfig struct {
|
||||
cfg *types.Config
|
||||
paths *Paths
|
||||
|
||||
pathsOnce sync.Once
|
||||
}
|
||||
|
||||
var (
|
||||
configMtx sync.Mutex
|
||||
config *types.Config
|
||||
)
|
||||
func New() *ALRConfig {
|
||||
return &ALRConfig{}
|
||||
}
|
||||
|
||||
// Config returns a ALR configuration struct.
|
||||
// The first time it's called, it'll load the config from a file.
|
||||
// Subsequent calls will just return the same value.
|
||||
func Config(ctx context.Context) *types.Config {
|
||||
configMtx.Lock()
|
||||
defer configMtx.Unlock()
|
||||
func (c *ALRConfig) Load(ctx context.Context) {
|
||||
cfgFl, err := os.Open(c.GetPaths(ctx).ConfigPath)
|
||||
if err != nil {
|
||||
log.Warn("Error opening config file, using defaults").Err(err).Send()
|
||||
c.cfg = defaultConfig
|
||||
return
|
||||
}
|
||||
defer cfgFl.Close()
|
||||
|
||||
// Copy the default configuration into config
|
||||
defCopy := *defaultConfig
|
||||
config := &defCopy
|
||||
config.Repos = nil
|
||||
|
||||
err = toml.NewDecoder(cfgFl).Decode(config)
|
||||
if err != nil {
|
||||
log.Warn("Error decoding config file, using defaults").Err(err).Send()
|
||||
c.cfg = defaultConfig
|
||||
return
|
||||
}
|
||||
c.cfg = config
|
||||
}
|
||||
|
||||
func (c *ALRConfig) initPaths(ctx context.Context) {
|
||||
log := loggerctx.From(ctx)
|
||||
paths := &Paths{}
|
||||
|
||||
if config == nil {
|
||||
cfgFl, err := os.Open(GetPaths(ctx).ConfigPath)
|
||||
if err != nil {
|
||||
log.Warn("Error opening config file, using defaults").Err(err).Send()
|
||||
return defaultConfig
|
||||
}
|
||||
defer cfgFl.Close()
|
||||
|
||||
// Copy the default configuration into config
|
||||
defCopy := *defaultConfig
|
||||
config = &defCopy
|
||||
config.Repos = nil
|
||||
|
||||
err = toml.NewDecoder(cfgFl).Decode(config)
|
||||
if err != nil {
|
||||
log.Warn("Error decoding config file, using defaults").Err(err).Send()
|
||||
// Set config back to nil so that we try again next time
|
||||
config = nil
|
||||
return defaultConfig
|
||||
}
|
||||
cfgDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to detect user config directory").Err(err).Send()
|
||||
}
|
||||
|
||||
return config
|
||||
paths.ConfigDir = filepath.Join(cfgDir, "alr")
|
||||
|
||||
err = os.MkdirAll(paths.ConfigDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create ALR config directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.ConfigPath = filepath.Join(paths.ConfigDir, "alr.toml")
|
||||
|
||||
if _, err := os.Stat(paths.ConfigPath); err != nil {
|
||||
cfgFl, err := os.Create(paths.ConfigPath)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create ALR config file").Err(err).Send()
|
||||
}
|
||||
|
||||
err = toml.NewEncoder(cfgFl).Encode(&defaultConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Error encoding default configuration").Err(err).Send()
|
||||
}
|
||||
|
||||
cfgFl.Close()
|
||||
}
|
||||
|
||||
cacheDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to detect cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.CacheDir = filepath.Join(cacheDir, "alr")
|
||||
paths.RepoDir = filepath.Join(paths.CacheDir, "repo")
|
||||
paths.PkgsDir = filepath.Join(paths.CacheDir, "pkgs")
|
||||
|
||||
err = os.MkdirAll(paths.RepoDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create repo cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
err = os.MkdirAll(paths.PkgsDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create package cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.DBPath = filepath.Join(paths.CacheDir, "db")
|
||||
|
||||
c.paths = paths
|
||||
}
|
||||
|
||||
func (c *ALRConfig) GetPaths(ctx context.Context) *Paths {
|
||||
c.pathsOnce.Do(func() {
|
||||
c.initPaths(ctx)
|
||||
})
|
||||
return c.paths
|
||||
}
|
||||
|
65
internal/config/config_legacy.go
Normal file
65
internal/config/config_legacy.go
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* ALR - Any Linux Repository
|
||||
* Copyright (C) 2024 Евгений Храмов
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"plemya-x.ru/alr/internal/types"
|
||||
)
|
||||
|
||||
var defaultConfig = &types.Config{
|
||||
RootCmd: "sudo",
|
||||
PagerStyle: "native",
|
||||
IgnorePkgUpdates: []string{},
|
||||
Repos: []types.Repo{
|
||||
{
|
||||
Name: "default",
|
||||
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Config returns a ALR configuration struct.
|
||||
// The first time it's called, it'll load the config from a file.
|
||||
// Subsequent calls will just return the same value.
|
||||
//
|
||||
// Deprecated: use struct method
|
||||
func Config(ctx context.Context) *types.Config {
|
||||
return GetInstance(ctx).cfg
|
||||
}
|
||||
|
||||
// =======================
|
||||
// FOR LEGACY ONLY
|
||||
// =======================
|
||||
|
||||
var (
|
||||
alrConfig *ALRConfig
|
||||
alrConfigOnce sync.Once
|
||||
)
|
||||
|
||||
func GetInstance(ctx context.Context) *ALRConfig {
|
||||
alrConfigOnce.Do(func() {
|
||||
alrConfig = New()
|
||||
alrConfig.Load(ctx)
|
||||
})
|
||||
|
||||
return alrConfig
|
||||
}
|
@ -20,12 +20,6 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"plemya-x.ru/alr/pkg/loggerctx"
|
||||
)
|
||||
|
||||
// Paths contains various paths used by ALR
|
||||
@ -38,71 +32,13 @@ type Paths struct {
|
||||
DBPath string
|
||||
}
|
||||
|
||||
var (
|
||||
pathsMtx sync.Mutex
|
||||
paths *Paths
|
||||
)
|
||||
|
||||
// GetPaths returns a Paths struct.
|
||||
// The first time it's called, it'll generate the struct
|
||||
// using information from the system.
|
||||
// Subsequent calls will return the same value.
|
||||
//
|
||||
// Depreacted: use struct API
|
||||
func GetPaths(ctx context.Context) *Paths {
|
||||
pathsMtx.Lock()
|
||||
defer pathsMtx.Unlock()
|
||||
|
||||
log := loggerctx.From(ctx)
|
||||
if paths == nil {
|
||||
paths = &Paths{}
|
||||
|
||||
cfgDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to detect user config directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.ConfigDir = filepath.Join(cfgDir, "alr")
|
||||
|
||||
err = os.MkdirAll(paths.ConfigDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create ALR config directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.ConfigPath = filepath.Join(paths.ConfigDir, "alr.toml")
|
||||
|
||||
if _, err := os.Stat(paths.ConfigPath); err != nil {
|
||||
cfgFl, err := os.Create(paths.ConfigPath)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create ALR config file").Err(err).Send()
|
||||
}
|
||||
|
||||
err = toml.NewEncoder(cfgFl).Encode(&defaultConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Error encoding default configuration").Err(err).Send()
|
||||
}
|
||||
|
||||
cfgFl.Close()
|
||||
}
|
||||
|
||||
cacheDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to detect cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.CacheDir = filepath.Join(cacheDir, "alr")
|
||||
paths.RepoDir = filepath.Join(paths.CacheDir, "repo")
|
||||
paths.PkgsDir = filepath.Join(paths.CacheDir, "pkgs")
|
||||
|
||||
err = os.MkdirAll(paths.RepoDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create repo cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
err = os.MkdirAll(paths.PkgsDir, 0o755)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create package cache directory").Err(err).Send()
|
||||
}
|
||||
|
||||
paths.DBPath = filepath.Join(paths.CacheDir, "db")
|
||||
}
|
||||
return paths
|
||||
alrConfig := GetInstance(ctx)
|
||||
return alrConfig.GetPaths(ctx)
|
||||
}
|
||||
|
Reference in New Issue
Block a user