2024-01-22 10:36:06 +00:00
|
|
|
/*
|
2024-05-05 10:32:08 +00:00
|
|
|
* ALR - Any Linux Repository
|
|
|
|
* Copyright (C) 2024 Евгений Храмов
|
2024-01-22 10:36:06 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
2024-05-05 10:32:08 +00:00
|
|
|
"plemya-x.ru/alr/internal/types"
|
|
|
|
"plemya-x.ru/alr/pkg/loggerctx"
|
2024-01-22 10:36:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultConfig = &types.Config{
|
|
|
|
RootCmd: "sudo",
|
|
|
|
PagerStyle: "native",
|
|
|
|
IgnorePkgUpdates: []string{},
|
|
|
|
Repos: []types.Repo{
|
|
|
|
{
|
|
|
|
Name: "default",
|
2024-08-24 09:55:17 +00:00
|
|
|
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
|
2024-01-22 10:36:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
configMtx sync.Mutex
|
|
|
|
config *types.Config
|
|
|
|
)
|
|
|
|
|
2024-05-05 10:32:08 +00:00
|
|
|
// Config returns a ALR configuration struct.
|
2024-01-22 10:36:06 +00:00
|
|
|
// 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()
|
|
|
|
log := loggerctx.From(ctx)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|