Skip to content

Commit 822e722

Browse files
committed
Migrate old ~/.vt.* configuration files to UserConfigDir
1 parent 6c02129 commit 822e722

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

vt/main.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package main
1515

1616
import (
17+
"fmt"
1718
"os"
1819
"path"
1920

@@ -22,16 +23,45 @@ import (
2223
"github.com/spf13/viper"
2324
)
2425

26+
// Migrate old ~/.vt.* configuration files.
27+
func migrateConfig(configDir string) {
28+
home, err := os.UserHomeDir()
29+
if err != nil {
30+
return
31+
}
32+
33+
for _, ext := range viper.SupportedExts {
34+
oldPath := path.Join(home, ".vt."+ext)
35+
36+
f, err := os.Open(oldPath)
37+
if f != nil {
38+
f.Close()
39+
}
40+
if err != nil {
41+
continue
42+
}
43+
44+
newPath := path.Join(configDir, path.Base(oldPath))
45+
err = os.Rename(oldPath, newPath)
46+
if err != nil {
47+
fmt.Printf("Migrated %s to %s\n", oldPath, newPath)
48+
} else {
49+
fmt.Printf("Failed to migrate %s to %s: %v\n", oldPath, newPath, err)
50+
}
51+
}
52+
}
53+
2554
// initConfig reads in config file and ENV variables if set.
2655
func initConfig() {
27-
2856
// Find config directory.
2957
configDir, err := os.UserConfigDir()
3058
if err == nil {
31-
viper.AddConfigPath(path.Join(configDir, "vt-cli"))
32-
}
59+
configDir = path.Join(configDir, "vt-cli")
60+
migrateConfig(configDir)
3361

34-
// Search config in home directory and current directory
62+
viper.AddConfigPath(configDir)
63+
}
64+
// Search config in current directory
3565
viper.AddConfigPath(".")
3666
// Config file must be named vt + format extension (.toml, .json, etc)
3767
viper.SetConfigName("vt")

0 commit comments

Comments
 (0)