Address PR comments

This commit is contained in:
Sam Coe 2021-05-26 11:28:58 -04:00
parent 583e74d70c
commit 602167c0c7
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
2 changed files with 52 additions and 43 deletions

View file

@ -40,34 +40,18 @@ func ConfigDir() string {
// If the path does not exist try migrating config from default paths
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
autoMigrateConfigDir(path)
_ = autoMigrateConfigDir(path)
}
return path
}
// 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) {
path, err := os.UserHomeDir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExists(oldPath) {
migrateDir(oldPath, newPath)
return
}
path, err = homedir.Dir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExists(oldPath) {
migrateDir(oldPath, newPath)
}
}
func StateDir() string {
if path := os.Getenv(XDG_STATE_HOME); path != "" {
path = filepath.Join(path, "gh")
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
if !dirExists(path) {
_ = os.MkdirAll(path, 0755)
autoMigrateStateDir(path)
_ = autoMigrateStateDir(path)
}
return path
}
@ -75,49 +59,70 @@ func StateDir() string {
return ConfigDir()
}
// Check default paths (os.UserHomeDir, and homedir.Dir) for existing state file (state.yml)
// If state file exist then move it to newPath
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 autoMigrateStateDir(newPath string) {
func autoMigrateConfigDir(newPath string) error {
path, err := os.UserHomeDir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExists(oldPath) {
migrateFile(oldPath, newPath, "state.yml")
return
return migrateDir(oldPath, newPath)
}
path, err = homedir.Dir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExists(oldPath) {
migrateFile(oldPath, newPath, "state.yml")
return migrateDir(oldPath, newPath)
}
return errNotExist
}
func migrateFile(oldPath, newPath, file string) {
// 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 && dirExists(oldPath) {
return migrateFile(oldPath, newPath, "state.yml")
}
path, err = homedir.Dir()
if oldPath := filepath.Join(path, ".config", "gh"); err == nil && dirExists(oldPath) {
return migrateFile(oldPath, newPath, "state.yml")
}
return errNotExist
}
func migrateFile(oldPath, newPath, file string) error {
if oldPath == newPath {
return
return errSamePath
}
oldFile := filepath.Join(oldPath, file)
newFile := filepath.Join(newPath, file)
if !fileExists(oldFile) {
return
return errNotExist
}
_ = os.MkdirAll(filepath.Dir(newFile), 0755)
_ = os.Rename(oldFile, newFile)
return os.Rename(oldFile, newFile)
}
func migrateDir(oldPath, newPath string) {
func migrateDir(oldPath, newPath string) error {
if oldPath == newPath {
return
return errSamePath
}
if !dirExists(oldPath) {
return
return errNotExist
}
_ = os.MkdirAll(filepath.Dir(newPath), 0755)
_ = os.Rename(oldPath, newPath)
return os.Rename(oldPath, newPath)
}
func dirExists(path string) bool {

View file

@ -269,8 +269,7 @@ func Test_configFile_Write_toDisk(t *testing.T) {
}
}
func Test_autoMigrateConfigDir_noMigration(t *testing.T) {
// homeDir does not have any config files
func Test_autoMigrateConfigDir_noMigration_notExist(t *testing.T) {
homeDir := t.TempDir()
migrateDir := t.TempDir()
@ -282,7 +281,8 @@ func Test_autoMigrateConfigDir_noMigration(t *testing.T) {
os.Setenv(homeEnvVar, homeDir)
defer os.Setenv(homeEnvVar, old)
autoMigrateConfigDir(migrateDir)
err := autoMigrateConfigDir(migrateDir)
assert.Equal(t, errNotExist, err)
files, err := ioutil.ReadDir(migrateDir)
assert.NoError(t, err)
@ -303,7 +303,8 @@ func Test_autoMigrateConfigDir_noMigration_samePath(t *testing.T) {
os.Setenv(homeEnvVar, homeDir)
defer os.Setenv(homeEnvVar, old)
autoMigrateConfigDir(migrateDir)
err = autoMigrateConfigDir(migrateDir)
assert.Equal(t, errSamePath, err)
files, err := ioutil.ReadDir(migrateDir)
assert.NoError(t, err)
@ -330,7 +331,8 @@ func Test_autoMigrateConfigDir_migration(t *testing.T) {
assert.NoError(t, err)
f.Close()
autoMigrateConfigDir(migrateConfigDir)
err = autoMigrateConfigDir(migrateConfigDir)
assert.NoError(t, err)
_, err = ioutil.ReadDir(homeConfigDir)
assert.True(t, os.IsNotExist(err))
@ -395,8 +397,7 @@ func Test_StateDir(t *testing.T) {
}
}
func Test_autoMigrateStateDir_noMigration(t *testing.T) {
// homeDir does not have any config files
func Test_autoMigrateStateDir_noMigration_notExist(t *testing.T) {
homeDir := t.TempDir()
migrateDir := t.TempDir()
@ -408,7 +409,8 @@ func Test_autoMigrateStateDir_noMigration(t *testing.T) {
os.Setenv(homeEnvVar, homeDir)
defer os.Setenv(homeEnvVar, old)
autoMigrateStateDir(migrateDir)
err := autoMigrateStateDir(migrateDir)
assert.Equal(t, errNotExist, err)
files, err := ioutil.ReadDir(migrateDir)
assert.NoError(t, err)
@ -429,7 +431,8 @@ func Test_autoMigrateStateDir_noMigration_samePath(t *testing.T) {
os.Setenv(homeEnvVar, homeDir)
defer os.Setenv(homeEnvVar, old)
autoMigrateStateDir(migrateDir)
err = autoMigrateStateDir(migrateDir)
assert.Equal(t, errSamePath, err)
files, err := ioutil.ReadDir(migrateDir)
assert.NoError(t, err)
@ -455,7 +458,8 @@ func Test_autoMigrateStateDir_migration(t *testing.T) {
err = ioutil.WriteFile(filepath.Join(homeConfigDir, "state.yml"), nil, 0755)
assert.NoError(t, err)
autoMigrateStateDir(migrateConfigDir)
err = autoMigrateStateDir(migrateConfigDir)
assert.NoError(t, err)
files, err := ioutil.ReadDir(homeConfigDir)
assert.NoError(t, err)