ssh-

Форк
0
/
config.go 
59 строк · 1.1 Кб
1
package config
2

3
import (
4
	"os/user"
5

6
	"ssh+/app/file"
7
	"ssh+/app/output"
8

9
	"github.com/spf13/viper"
10
)
11

12
func getHomeDir() string {
13
	usr, err := user.Current()
14
	if err != nil {
15
		output.GetOutError("Error retrieving user data")
16
	}
17

18
	return usr.HomeDir + DirectionApp
19
}
20

21
func existOrCreateConfig(configPath string) {
22
	err := viper.ReadInConfig()
23
	if err != nil {
24
		file.CreateFile(configPath)
25

26
		err = viper.ReadInConfig()
27
		if err != nil {
28
			output.GetOutError("File creation error")
29
		}
30
	}
31
}
32

33
func setConfigVariable() {
34
	viper.Set("NameFileConnects", NameFileConnects)
35
	viper.Set("NameFileCryptKey", NameFileCryptKey)
36
	viper.Set("FullPathConfig", getHomeDir())
37
	viper.Set("Separator", Separator)
38
	viper.Set("Space", Space)
39

40
	err := viper.WriteConfig()
41
	if err != nil {
42
		output.GetOutError("Error writing to configuration file")
43
	}
44
}
45

46
func Generate() {
47
	viper.New()
48

49
	viper.SetConfigName(NameFileConfig)
50
	viper.SetConfigType(TypeFileConfig)
51
	viper.AddConfigPath(getHomeDir())
52

53
	err := viper.ReadInConfig()
54
	if err != nil {
55
		configPath := getHomeDir() + FullNameFileConfig
56
		existOrCreateConfig(configPath)
57
		setConfigVariable()
58
	}
59
}
60

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

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

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

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