2020-09-30 07:11:33 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 03:40:02 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 07:11:33 +02:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-09-08 03:40:02 +02:00
|
|
|
"os"
|
2020-09-30 07:11:33 +02:00
|
|
|
"path/filepath"
|
2020-12-12 14:28:37 +01:00
|
|
|
"sync"
|
2020-09-30 07:11:33 +02:00
|
|
|
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
2020-10-06 15:06:47 +02:00
|
|
|
"github.com/adrg/xdg"
|
2023-09-10 05:47:43 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-09-30 07:11:33 +02:00
|
|
|
)
|
|
|
|
|
2022-03-29 01:34:14 +02:00
|
|
|
// FlagDefaults defines all flags that can be overridden with a default value
|
|
|
|
// via the config file
|
|
|
|
type FlagDefaults struct {
|
|
|
|
// Prefer a specific git remote to use for selecting a repository on gitea,
|
|
|
|
// instead of relying on the remote associated with main/master/trunk branch.
|
|
|
|
// The --remote flag still has precedence over this value.
|
|
|
|
Remote string `yaml:"remote"`
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:36:08 +02:00
|
|
|
// Preferences that are stored in and read from the config file
|
|
|
|
type Preferences struct {
|
|
|
|
// Prefer using an external text editor over inline multiline prompts
|
2022-03-29 01:34:14 +02:00
|
|
|
Editor bool `yaml:"editor"`
|
|
|
|
FlagDefaults FlagDefaults `yaml:"flag_defaults"`
|
2021-10-14 16:36:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 07:11:33 +02:00
|
|
|
// LocalConfig represents local configurations
|
|
|
|
type LocalConfig struct {
|
2021-10-14 16:36:08 +02:00
|
|
|
Logins []Login `yaml:"logins"`
|
|
|
|
Prefs Preferences `yaml:"preferences"`
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-12-12 14:28:37 +01:00
|
|
|
// config contain if loaded local tea config
|
|
|
|
config LocalConfig
|
|
|
|
loadConfigOnce sync.Once
|
2020-09-30 07:11:33 +02:00
|
|
|
)
|
|
|
|
|
2020-10-06 15:06:47 +02:00
|
|
|
// GetConfigPath return path to tea config file
|
|
|
|
func GetConfigPath() string {
|
|
|
|
configFilePath, err := xdg.ConfigFile("tea/config.yml")
|
|
|
|
|
|
|
|
var exists bool
|
2020-09-30 07:11:33 +02:00
|
|
|
if err != nil {
|
2020-10-06 15:06:47 +02:00
|
|
|
exists = false
|
|
|
|
} else {
|
|
|
|
exists, _ = utils.PathExists(configFilePath)
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:06:47 +02:00
|
|
|
// fallback to old config if no new one exists
|
|
|
|
if !exists {
|
|
|
|
file := filepath.Join(xdg.Home, ".tea", "tea.yml")
|
|
|
|
exists, _ = utils.PathExists(file)
|
|
|
|
if exists {
|
|
|
|
return file
|
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:06:47 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unable to get or create config file")
|
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
|
2020-10-06 15:06:47 +02:00
|
|
|
return configFilePath
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 16:36:08 +02:00
|
|
|
// GetPreferences returns preferences based on the config file
|
|
|
|
func GetPreferences() Preferences {
|
2023-04-30 05:43:26 +02:00
|
|
|
_ = loadConfig()
|
2021-10-14 16:36:08 +02:00
|
|
|
return config.Prefs
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// loadConfig load config from file
|
|
|
|
func loadConfig() (err error) {
|
|
|
|
loadConfigOnce.Do(func() {
|
|
|
|
ymlPath := GetConfigPath()
|
|
|
|
exist, _ := utils.FileExist(ymlPath)
|
|
|
|
if exist {
|
2023-09-08 03:40:02 +02:00
|
|
|
bs, err := os.ReadFile(ymlPath)
|
2020-12-12 14:28:37 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Failed to read config file: %s", ymlPath)
|
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
err = yaml.Unmarshal(bs, &config)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Failed to parse contents of config file: %s", ymlPath)
|
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
2020-12-12 14:28:37 +01:00
|
|
|
})
|
|
|
|
return
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// saveConfig save config to file
|
|
|
|
func saveConfig() error {
|
2020-09-30 07:11:33 +02:00
|
|
|
ymlPath := GetConfigPath()
|
2020-12-12 14:28:37 +01:00
|
|
|
bs, err := yaml.Marshal(config)
|
2020-09-30 07:11:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-08 03:40:02 +02:00
|
|
|
return os.WriteFile(ymlPath, bs, 0o660)
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|