Skip to content

Commit 4011e57

Browse files
committed
fix: ensure cache directory exists for relationships cache
This change ensures that the cache directory is created if it doesn't already exist, both when `vt init` creates the cache file and when migrating an old cache file from the user's home directory. It also adds a check to verify the existence of the old cache file before attempting to move it. Closes #136.
1 parent e671a5d commit 4011e57

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎cmd/init.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func NewInitCmd() *cobra.Command {
7979
os.Exit(1)
8080
}
8181

82+
if err := os.MkdirAll(cacheDir, 0755); err != nil {
83+
fmt.Fprintln(os.Stderr, err)
84+
os.Exit(1)
85+
}
86+
8287
relCacheFile, err := os.Create(path.Join(cacheDir, ".vt.relationships.cache"))
8388
if err != nil {
8489
fmt.Fprintln(os.Stderr, err)

‎cmd/relationship.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ func init() {
3636
// We used to store the cache file in user's home directory. Let's
3737
// move it to the cache directory.
3838
if homeDir, err := os.UserHomeDir(); err == nil {
39-
os.Rename(path.Join(homeDir, ".vt.relationships.cache"), cacheFile)
39+
oldCacheFile := path.Join(homeDir, ".vt.relationships.cache")
40+
if _, err := os.Stat(oldCacheFile); err == nil {
41+
if err := os.MkdirAll(cacheDir, 0755); err == nil {
42+
os.Rename(oldCacheFile, cacheFile)
43+
}
44+
}
4045
}
4146
f, err := os.Open(cacheFile)
4247
if err == nil {

0 commit comments

Comments
 (0)