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 822e722
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 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 @@ -22,16 +23,45 @@ import (
"github.com/spf13/viper"
)

// Migrate old ~/.vt.* configuration files.
func migrateConfig(configDir string) {
home, err := os.UserHomeDir()
if err != nil {
return
}

for _, ext := range viper.SupportedExts {
oldPath := path.Join(home, ".vt."+ext)

f, err := os.Open(oldPath)
if f != nil {
f.Close()
}
if err != nil {
continue
}

newPath := path.Join(configDir, path.Base(oldPath))
err = os.Rename(oldPath, newPath)
if err != nil {
fmt.Printf("Migrated %s to %s\n", oldPath, newPath)
} else {
fmt.Printf("Failed to migrate %s to %s: %v\n", oldPath, newPath, err)
}
}
}

// 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")
migrateConfig(configDir)

// Search config in home directory and current directory
viper.AddConfigPath(configDir)
}
// 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 822e722

Please sign in to comment.