mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-02 09:58:29 +02:00
[Refactor] unexport config.Config var & move login tasks to task module (#288)
Unexport generateToken() move CreateLogin into task Create func config.SetDefaultLogin() Unexport loadConfig() & saveConfig unexport config var make SetDefaultLogin() case insensitive update func descriptions move FindSSHKey to task module Reviewed-on: https://gitea.com/gitea/tea/pulls/288 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Co-Authored-By: 6543 <6543@obermui.de> Co-Committed-By: 6543 <6543@obermui.de>
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
|
||||
@ -22,8 +23,9 @@ type LocalConfig struct {
|
||||
}
|
||||
|
||||
var (
|
||||
// Config contain if loaded local tea config
|
||||
Config LocalConfig
|
||||
// config contain if loaded local tea config
|
||||
config LocalConfig
|
||||
loadConfigOnce sync.Once
|
||||
)
|
||||
|
||||
// GetConfigPath return path to tea config file
|
||||
@ -53,29 +55,30 @@ func GetConfigPath() string {
|
||||
return configFilePath
|
||||
}
|
||||
|
||||
// LoadConfig load config into global Config var
|
||||
func LoadConfig() error {
|
||||
ymlPath := GetConfigPath()
|
||||
exist, _ := utils.FileExist(ymlPath)
|
||||
if exist {
|
||||
bs, err := ioutil.ReadFile(ymlPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read config file: %s", ymlPath)
|
||||
}
|
||||
// loadConfig load config from file
|
||||
func loadConfig() (err error) {
|
||||
loadConfigOnce.Do(func() {
|
||||
ymlPath := GetConfigPath()
|
||||
exist, _ := utils.FileExist(ymlPath)
|
||||
if exist {
|
||||
bs, err := ioutil.ReadFile(ymlPath)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Failed to read config file: %s", ymlPath)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(bs, &Config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse contents of config file: %s", ymlPath)
|
||||
err = yaml.Unmarshal(bs, &config)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Failed to parse contents of config file: %s", ymlPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// SaveConfig save config from global Config var into config file
|
||||
func SaveConfig() error {
|
||||
// saveConfig save config to file
|
||||
func saveConfig() error {
|
||||
ymlPath := GetConfigPath()
|
||||
bs, err := yaml.Marshal(Config)
|
||||
bs, err := yaml.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user