Skip to content

Commit

Permalink
Migrate old ~/.vt.* configuration files to UserConfigDir
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Jan 15, 2025
1 parent 6c02129 commit acef272
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions vt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"fmt"
"os"
"path"

Expand All @@ -24,14 +25,34 @@ import (

// initConfig reads in config file and ENV variables if set.
func initConfig() {

// Find config directory.
configDir, err := os.UserConfigDir()
if err == nil {
viper.AddConfigPath(path.Join(configDir, "vt-cli"))
}
configDir = path.Join(configDir, "vt-cli")
viper.AddConfigPath(configDir)

// Migrate old ~/.vt.* configuration files.
home, err := os.UserHomeDir()
if err == nil {
for _, ext := range viper.SupportedExts {
oldPath := path.Join(home, ".vt."+ext)

// Search config in home directory and current directory
f, err := os.Open(oldPath)
if f != nil {
f.Close()
}
if err != nil {
continue
}

newPath := path.Join(configDir, path.Base(oldPath))
os.Rename(oldPath, newPath)
fmt.Printf("Migrated %s to %s\n", oldPath, newPath)
}
}

}
// Search config in current directory
viper.AddConfigPath(".")
// Config file must be named vt + format extension (.toml, .json, etc)
viper.SetConfigName("vt")
Expand Down

0 comments on commit acef272

Please sign in to comment.