1
0
mirror of https://gitea.com/gitea/tea.git synced 2024-06-23 06:35:38 +00:00
tea/modules/config/config.go
Norwin 7e191eb18b fix InitCommand() (#285)
split modules/config

login_tasks.go should probably be modules/task/login.go,
but i didn't do that, as it still depends on the global
`Config` variable from the config module, see
https://gitea.com/gitea/tea/issues/158

rework InitCommand()

- make it error tolerant if $PWD is not a git repo (#200)
- don't force default login when repo flag is set (#191)

remove InitCommandLoginOnly()

Merge branch 'master' into issue-200-initcommand

improve docs

Merge branch 'master' into issue-200-initcommand

move config func and config task func to right place

Co-authored-by: Norwin Roosen <git@nroo.de>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/285
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: khmarbaise <khmarbaise@noreply.gitea.io>
Co-Authored-By: Norwin <noerw@noreply.gitea.io>
Co-Committed-By: Norwin <noerw@noreply.gitea.io>
2020-12-11 17:07:29 +08:00

84 lines
1.7 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package config
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"code.gitea.io/tea/modules/utils"
"github.com/adrg/xdg"
"gopkg.in/yaml.v2"
)
// LocalConfig represents local configurations
type LocalConfig struct {
Logins []Login `yaml:"logins"`
}
var (
// Config contain if loaded local tea config
Config LocalConfig
)
// GetConfigPath return path to tea config file
func GetConfigPath() string {
configFilePath, err := xdg.ConfigFile("tea/config.yml")
var exists bool
if err != nil {
exists = false
} else {
exists, _ = utils.PathExists(configFilePath)
}
// 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
}
}
if err != nil {
log.Fatal("unable to get or create config file")
}
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)
}
err = yaml.Unmarshal(bs, &Config)
if err != nil {
return fmt.Errorf("Failed to parse contents of config file: %s", ymlPath)
}
}
return nil
}
// SaveConfig save config from global Config var into config file
func SaveConfig() error {
ymlPath := GetConfigPath()
bs, err := yaml.Marshal(Config)
if err != nil {
return err
}
return ioutil.WriteFile(ymlPath, bs, 0660)
}