Have one place manage the config dir location

This commit is contained in:
Corey Johnson 2019-12-13 16:16:46 -08:00
parent e93ab66107
commit 9becb5f790
3 changed files with 14 additions and 14 deletions

View file

@ -8,7 +8,6 @@ import (
"os"
"path"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
)
@ -36,11 +35,9 @@ func parseConfigFile(fn string) (*configEntry, error) {
return parseConfig(f)
}
// ParseDefaultConfig reads the configuration from ~/.config/gh
// ParseDefaultConfig reads the configuration file
func ParseDefaultConfig() (*configEntry, error) {
// FIXME: this duplicates fsContext.configFile
fn, _ := homedir.Expand("~/.config/gh")
return parseConfigFile(fn)
return parseConfigFile(configFile())
}
func parseConfig(r io.Reader) (*configEntry, error) {
@ -75,7 +72,7 @@ func parseConfig(r io.Reader) (*configEntry, error) {
// If ~/.config/gh is a file, convert it to a directory and place the file
// into ~/.config/gh/config.yml
func migrateConfigFile() {
p, _ := homedir.Expand("~/.config/gh")
p := ConfigDir()
fi, err := os.Stat(p)
if err != nil { // This means the file doesn't exist, and that is fine.
return

View file

@ -1,6 +1,7 @@
package context
import (
"path"
"strings"
"github.com/github/gh-cli/git"
@ -39,14 +40,18 @@ type fsContext struct {
authToken string
}
func (c *fsContext) configFile() string {
dir, _ := homedir.Expand("~/.config/gh/config.yml")
func ConfigDir() string {
dir, _ := homedir.Expand("~/.config/gh")
return dir
}
func configFile() string {
return path.Join(ConfigDir(), "config.yml")
}
func (c *fsContext) getConfig() (*configEntry, error) {
if c.config == nil {
entry, err := parseOrSetupConfigFile(c.configFile())
entry, err := parseOrSetupConfigFile(configFile())
if err != nil {
return nil, err
}

View file

@ -3,14 +3,15 @@ package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/github/gh-cli/command"
"github.com/github/gh-cli/context"
"github.com/github/gh-cli/update"
"github.com/github/gh-cli/utils"
"github.com/mattn/go-isatty"
"github.com/mgutz/ansi"
"github.com/mitchellh/go-homedir"
)
var updaterEnabled = ""
@ -61,9 +62,6 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
}
repo := updaterEnabled
stateFilePath, err := homedir.Expand("~/.config/gh/state.yml")
if err != nil {
return nil, err
}
stateFilePath := path.Join(context.ConfigDir(), "state.yml")
return update.CheckForUpdate(client, stateFilePath, repo, currentVersion)
}