ALR

Форк
1
/
config.go 
79 строк · 2.0 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package config
20

21
import (
22
	"context"
23
	"os"
24
	"sync"
25

26
	"github.com/pelletier/go-toml/v2"
27
	"plemya-x.ru/alr/internal/types"
28
	"plemya-x.ru/alr/pkg/loggerctx"
29
)
30

31
var defaultConfig = &types.Config{
32
	RootCmd:          "sudo",
33
	PagerStyle:       "native",
34
	IgnorePkgUpdates: []string{},
35
	Repos: []types.Repo{
36
		{
37
			Name: "default",
38
			URL:  "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
39
		},
40
	},
41
}
42

43
var (
44
	configMtx sync.Mutex
45
	config    *types.Config
46
)
47

48
// Config returns a ALR configuration struct.
49
// The first time it's called, it'll load the config from a file.
50
// Subsequent calls will just return the same value.
51
func Config(ctx context.Context) *types.Config {
52
	configMtx.Lock()
53
	defer configMtx.Unlock()
54
	log := loggerctx.From(ctx)
55

56
	if config == nil {
57
		cfgFl, err := os.Open(GetPaths(ctx).ConfigPath)
58
		if err != nil {
59
			log.Warn("Error opening config file, using defaults").Err(err).Send()
60
			return defaultConfig
61
		}
62
		defer cfgFl.Close()
63

64
		// Copy the default configuration into config
65
		defCopy := *defaultConfig
66
		config = &defCopy
67
		config.Repos = nil
68

69
		err = toml.NewDecoder(cfgFl).Decode(config)
70
		if err != nil {
71
			log.Warn("Error decoding config file, using defaults").Err(err).Send()
72
			// Set config back to nil so that we try again next time
73
			config = nil
74
			return defaultConfig
75
		}
76
	}
77

78
	return config
79
}
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.