From 1bcfa7518c6e7c537a8f6425af9553dc67f7cd1a Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Fri, 24 Oct 2025 12:15:39 +0200 Subject: [PATCH] refactor: stop using `go-homedir` library and use the builtin functions. Also, the relationships cache file is not stored in the cache directory returned by `os.UserCacheDir`. If the cache is already in the home directory it is moved to the new location when the application is executed. --- cmd/init.go | 16 +++++++++++----- cmd/relationship.go | 22 +++++++++++++++------- go.mod | 5 ++--- go.sum | 10 ++++------ vt/main.go | 8 +++----- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 0fd4657..9a62780 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -20,7 +20,6 @@ import ( "path" vt "github.com/VirusTotal/vt-go" - "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" ) @@ -68,13 +67,19 @@ func NewInitCmd() *cobra.Command { os.Exit(1) } - dir, err := homedir.Dir() + homeDir, err := os.UserHomeDir() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - relCacheFile, err := os.Create(path.Join(dir, ".vt.relationships.cache")) + cacheDir, err := os.UserCacheDir() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + relCacheFile, err := os.Create(path.Join(cacheDir, ".vt.relationships.cache")) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -88,7 +93,7 @@ func NewInitCmd() *cobra.Command { os.Exit(1) } - configFilePath := path.Join(dir, ".vt.toml") + configFilePath := path.Join(homeDir, ".vt.toml") configFile, err := os.OpenFile(configFilePath, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { fmt.Fprintln(os.Stderr, err) @@ -102,7 +107,8 @@ func NewInitCmd() *cobra.Command { os.Exit(1) } - fmt.Printf("Your API key has been written to config file %s\n", configFilePath) + fmt.Printf("API key written to config file: %s\n", configFilePath) + fmt.Printf("Relationships cache written to: %s\n", relCacheFile.Name()) }, } } diff --git a/cmd/relationship.go b/cmd/relationship.go index 75c8ef0..bc3db09 100644 --- a/cmd/relationship.go +++ b/cmd/relationship.go @@ -24,7 +24,6 @@ import ( "github.com/VirusTotal/vt-cli/utils" vt "github.com/VirusTotal/vt-go" - homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -32,12 +31,21 @@ import ( var objectRelationshipsMap map[string][]vt.RelationshipMeta func init() { - home, _ := homedir.Dir() - f, err := os.Open(path.Join(home, ".vt.relationships.cache")) - if err == nil { - defer f.Close() - dec := gob.NewDecoder(f) - dec.Decode(&objectRelationshipsMap) + if cacheDir, err := os.UserCacheDir(); err == nil { + cacheFile := path.Join(cacheDir, ".vt.relationships.cache") + // We used to store the cache file in user's home directory. Let's + // move it to the cache directory. + if homeDir, err := os.UserHomeDir(); err == nil { + os.Rename(path.Join(homeDir, ".vt.relationships.cache"), cacheFile) + } + f, err := os.Open(cacheFile) + if err == nil { + defer f.Close() + dec := gob.NewDecoder(f) + dec.Decode(&objectRelationshipsMap) + } else { + + } } } diff --git a/go.mod b/go.mod index bd19f96..003343e 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/gobwas/glob v0.2.3 github.com/gosuri/uitable v0.0.4 github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 - github.com/mitchellh/go-homedir v1.1.0 github.com/plusvic/go-ansi v0.0.0-20180516115420-9879244c4340 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 @@ -21,6 +20,7 @@ require ( ) require ( + github.com/clipperhouse/uax29/v2 v2.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect @@ -29,11 +29,10 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/mattn/go-runewidth v0.0.19 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/rivo/uniseg v0.2.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/go.sum b/go.sum index 76f6527..bda95cf 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Pt github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM= github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4= github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4= +github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY= +github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -41,10 +43,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= +github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= @@ -54,8 +54,6 @@ github.com/plusvic/go-ansi v0.0.0-20180516115420-9879244c4340/go.mod h1:eYI1gLV8 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= diff --git a/vt/main.go b/vt/main.go index 89e6a44..f0449d2 100644 --- a/vt/main.go +++ b/vt/main.go @@ -18,23 +18,21 @@ import ( "os" "github.com/VirusTotal/vt-cli/cmd" - homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" ) // initConfig reads in config file and ENV variables if set. func initConfig() { - - // Find home directory. - home, err := homedir.Dir() + // Find homeDir directory. + homeDir, err := os.UserHomeDir() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // Search config in home directory and current directory - viper.AddConfigPath(home) + viper.AddConfigPath(homeDir) viper.AddConfigPath(".") // Config file must be named .vt + format extension (.toml, .json, etc) viper.SetConfigName(".vt")