"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "internal/config/config_file.go" between
gh-cli-1.10.3.tar.gz and gh-cli-1.11.0.tar.gz

About: GitHub CLI is GitHub’s official command line tool.

config_file.go  (gh-cli-1.10.3):config_file.go  (gh-cli-1.11.0)
package config package config
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"syscall" "syscall"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
const ( const (
GH_CONFIG_DIR = "GH_CONFIG_DIR" GH_CONFIG_DIR = "GH_CONFIG_DIR"
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
XDG_STATE_HOME = "XDG_STATE_HOME"
XDG_DATA_HOME = "XDG_DATA_HOME"
APP_DATA = "AppData"
LOCAL_APP_DATA = "LocalAppData"
) )
// Config path precedence
// 1. GH_CONFIG_DIR
// 2. XDG_CONFIG_HOME
// 3. AppData (windows only)
// 4. HOME
func ConfigDir() string { func ConfigDir() string {
if v := os.Getenv(GH_CONFIG_DIR); v != "" { var path string
return v if a := os.Getenv(GH_CONFIG_DIR); a != "" {
path = a
} else if b := os.Getenv(XDG_CONFIG_HOME); b != "" {
path = filepath.Join(b, "gh")
} else if c := os.Getenv(APP_DATA); runtime.GOOS == "windows" && c != ""
{
path = filepath.Join(c, "GitHub CLI")
} else {
d, _ := os.UserHomeDir()
path = filepath.Join(d, ".config", "gh")
} }
homeDir, _ := homeDirAutoMigrate() // If the path does not exist try migrating config from default paths
return homeDir if !dirExists(path) {
_ = autoMigrateConfigDir(path)
}
return path
}
// State path precedence
// 1. XDG_CONFIG_HOME
// 2. LocalAppData (windows only)
// 3. HOME
func StateDir() string {
var path string
if a := os.Getenv(XDG_STATE_HOME); a != "" {
path = filepath.Join(a, "gh")
} else if b := os.Getenv(LOCAL_APP_DATA); runtime.GOOS == "windows" && b
!= "" {
path = filepath.Join(b, "GitHub CLI")
} else {
c, _ := os.UserHomeDir()
path = filepath.Join(c, ".local", "state", "gh")
}
// If the path does not exist try migrating state from default paths
if !dirExists(path) {
_ = autoMigrateStateDir(path)
}
return path
}
// Data path precedence
// 1. XDG_DATA_HOME
// 2. LocalAppData (windows only)
// 3. HOME
func DataDir() string {
var path string
if a := os.Getenv(XDG_DATA_HOME); a != "" {
path = filepath.Join(a, "gh")
} else if b := os.Getenv(LOCAL_APP_DATA); runtime.GOOS == "windows" && b
!= "" {
path = filepath.Join(b, "GitHub CLI")
} else {
c, _ := os.UserHomeDir()
path = filepath.Join(c, ".local", "share", "gh")
}
return path
}
var errSamePath = errors.New("same path")
var errNotExist = errors.New("not exist")
// Check default paths (os.UserHomeDir, and homedir.Dir) for existing configs
// If configs exist then move them to newPath
// TODO: Remove support for homedir.Dir location in v2
func autoMigrateConfigDir(newPath string) error {
path, err := os.UserHomeDir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExis
ts(oldPath) {
return migrateDir(oldPath, newPath)
}
path, err = homedir.Dir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExis
ts(oldPath) {
return migrateDir(oldPath, newPath)
}
return errNotExist
}
// Check default paths (os.UserHomeDir, and homedir.Dir) for existing state file
(state.yml)
// If state file exist then move it to newPath
// TODO: Remove support for homedir.Dir location in v2
func autoMigrateStateDir(newPath string) error {
path, err := os.UserHomeDir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExis
ts(oldPath) {
return migrateFile(oldPath, newPath, "state.yml")
}
path, err = homedir.Dir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExis
ts(oldPath) {
return migrateFile(oldPath, newPath, "state.yml")
}
return errNotExist
}
func migrateFile(oldPath, newPath, file string) error {
if oldPath == newPath {
return errSamePath
}
oldFile := filepath.Join(oldPath, file)
newFile := filepath.Join(newPath, file)
if !fileExists(oldFile) {
return errNotExist
}
_ = os.MkdirAll(filepath.Dir(newFile), 0755)
return os.Rename(oldFile, newFile)
}
func migrateDir(oldPath, newPath string) error {
if oldPath == newPath {
return errSamePath
}
if !dirExists(oldPath) {
return errNotExist
}
_ = os.MkdirAll(filepath.Dir(newPath), 0755)
return os.Rename(oldPath, newPath)
}
func dirExists(path string) bool {
f, err := os.Stat(path)
return err == nil && f.IsDir()
}
func fileExists(path string) bool {
f, err := os.Stat(path)
return err == nil && !f.IsDir()
} }
func ConfigFile() string { func ConfigFile() string {
return filepath.Join(ConfigDir(), "config.yml") return filepath.Join(ConfigDir(), "config.yml")
} }
func HostsConfigFile() string { func HostsConfigFile() string {
return filepath.Join(ConfigDir(), "hosts.yml") return filepath.Join(ConfigDir(), "hosts.yml")
} }
skipping to change at line 66 skipping to change at line 206
if legacyDir, err := homedir.Dir(); err == nil { if legacyDir, err := homedir.Dir(); err == nil {
legacyPath := filepath.Join(legacyDir, subdir) legacyPath := filepath.Join(legacyDir, subdir)
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() { if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
return legacyPath, nil return legacyPath, nil
} }
} }
return newPath, nil return newPath, nil
} }
// Looks up the `~/.config/gh` directory with backwards-compatibility with go-ho
medir and auto-migration
// when an old homedir location was found.
func homeDirAutoMigrate() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
return filepath.Join(legacyDir, ".config", "gh"), nil
}
return "", err
}
newPath := filepath.Join(homeDir, ".config", "gh")
_, newPathErr := os.Stat(newPath)
if newPathErr == nil || !os.IsNotExist(err) {
return newPath, newPathErr
}
// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
legacyPath := filepath.Join(legacyDir, ".config", "gh")
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
_ = os.MkdirAll(filepath.Dir(newPath), 0755)
return newPath, os.Rename(legacyPath, newPath)
}
}
return newPath, nil
}
var ReadConfigFile = func(filename string) ([]byte, error) { var ReadConfigFile = func(filename string) ([]byte, error) {
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return nil, pathError(err) return nil, pathError(err)
} }
defer f.Close() defer f.Close()
data, err := ioutil.ReadAll(f) data, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return nil, err return nil, err
 End of changes. 6 change blocks. 
36 lines changed or deleted 153 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)