14
14
package main
15
15
16
16
import (
17
+ "fmt"
17
18
"os"
18
19
"path"
19
20
@@ -22,16 +23,45 @@ import (
22
23
"github.com/spf13/viper"
23
24
)
24
25
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
+
25
54
// initConfig reads in config file and ENV variables if set.
26
55
func initConfig () {
27
-
28
56
// Find config directory.
29
57
configDir , err := os .UserConfigDir ()
30
58
if err == nil {
31
- viper . AddConfigPath ( path .Join (configDir , "vt-cli" ) )
32
- }
59
+ configDir = path .Join (configDir , "vt-cli" )
60
+ migrateConfig ( configDir )
33
61
34
- // Search config in home directory and current directory
62
+ viper .AddConfigPath (configDir )
63
+ }
64
+ // Search config in current directory
35
65
viper .AddConfigPath ("." )
36
66
// Config file must be named vt + format extension (.toml, .json, etc)
37
67
viper .SetConfigName ("vt" )
0 commit comments