Skip to content

Commit f99e197

Browse files
authored
refactor: stop using go-homedir library and use the builtin functions. (#119)
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.
1 parent aa944f0 commit f99e197

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

cmd/init.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"path"
2121

2222
vt "github.com/VirusTotal/vt-go"
23-
"github.com/mitchellh/go-homedir"
2423
"github.com/spf13/cobra"
2524
)
2625

@@ -68,13 +67,19 @@ func NewInitCmd() *cobra.Command {
6867
os.Exit(1)
6968
}
7069

71-
dir, err := homedir.Dir()
70+
homeDir, err := os.UserHomeDir()
7271
if err != nil {
7372
fmt.Fprintln(os.Stderr, err)
7473
os.Exit(1)
7574
}
7675

77-
relCacheFile, err := os.Create(path.Join(dir, ".vt.relationships.cache"))
76+
cacheDir, err := os.UserCacheDir()
77+
if err != nil {
78+
fmt.Fprintln(os.Stderr, err)
79+
os.Exit(1)
80+
}
81+
82+
relCacheFile, err := os.Create(path.Join(cacheDir, ".vt.relationships.cache"))
7883
if err != nil {
7984
fmt.Fprintln(os.Stderr, err)
8085
os.Exit(1)
@@ -88,7 +93,7 @@ func NewInitCmd() *cobra.Command {
8893
os.Exit(1)
8994
}
9095

91-
configFilePath := path.Join(dir, ".vt.toml")
96+
configFilePath := path.Join(homeDir, ".vt.toml")
9297
configFile, err := os.OpenFile(configFilePath, os.O_CREATE|os.O_WRONLY, 0600)
9398
if err != nil {
9499
fmt.Fprintln(os.Stderr, err)
@@ -102,7 +107,8 @@ func NewInitCmd() *cobra.Command {
102107
os.Exit(1)
103108
}
104109

105-
fmt.Printf("Your API key has been written to config file %s\n", configFilePath)
110+
fmt.Printf("API key written to config file: %s\n", configFilePath)
111+
fmt.Printf("Relationships cache written to: %s\n", relCacheFile.Name())
106112
},
107113
}
108114
}

cmd/relationship.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ import (
2424
"github.com/VirusTotal/vt-cli/utils"
2525

2626
vt "github.com/VirusTotal/vt-go"
27-
homedir "github.com/mitchellh/go-homedir"
2827
"github.com/spf13/cobra"
2928
"github.com/spf13/viper"
3029
)
3130

3231
var objectRelationshipsMap map[string][]vt.RelationshipMeta
3332

3433
func init() {
35-
home, _ := homedir.Dir()
36-
f, err := os.Open(path.Join(home, ".vt.relationships.cache"))
37-
if err == nil {
38-
defer f.Close()
39-
dec := gob.NewDecoder(f)
40-
dec.Decode(&objectRelationshipsMap)
34+
if cacheDir, err := os.UserCacheDir(); err == nil {
35+
cacheFile := path.Join(cacheDir, ".vt.relationships.cache")
36+
// We used to store the cache file in user's home directory. Let's
37+
// move it to the cache directory.
38+
if homeDir, err := os.UserHomeDir(); err == nil {
39+
os.Rename(path.Join(homeDir, ".vt.relationships.cache"), cacheFile)
40+
}
41+
f, err := os.Open(cacheFile)
42+
if err == nil {
43+
defer f.Close()
44+
dec := gob.NewDecoder(f)
45+
dec.Decode(&objectRelationshipsMap)
46+
} else {
47+
48+
}
4149
}
4250
}
4351

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/gobwas/glob v0.2.3
1212
github.com/gosuri/uitable v0.0.4
1313
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
14-
github.com/mitchellh/go-homedir v1.1.0
1514
github.com/plusvic/go-ansi v0.0.0-20180516115420-9879244c4340
1615
github.com/spf13/cobra v1.8.1
1716
github.com/spf13/pflag v1.0.5
@@ -21,6 +20,7 @@ require (
2120
)
2221

2322
require (
23+
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
2424
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
2525
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2626
github.com/fsnotify/fsnotify v1.7.0 // indirect
@@ -29,11 +29,10 @@ require (
2929
github.com/magiconair/properties v1.8.7 // indirect
3030
github.com/mattn/go-colorable v0.1.13 // indirect
3131
github.com/mattn/go-isatty v0.0.20 // indirect
32-
github.com/mattn/go-runewidth v0.0.16 // indirect
32+
github.com/mattn/go-runewidth v0.0.19 // indirect
3333
github.com/mitchellh/mapstructure v1.5.0 // indirect
3434
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
3535
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
36-
github.com/rivo/uniseg v0.2.0 // indirect
3736
github.com/russross/blackfriday/v2 v2.1.0 // indirect
3837
github.com/sagikazarmark/locafero v0.4.0 // indirect
3938
github.com/sagikazarmark/slog-shim v0.1.0 // indirect

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Pt
44
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
55
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
66
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
7+
github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY=
8+
github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
79
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
810
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
911
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
4143
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
4244
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
4345
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
44-
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
45-
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
46-
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
47-
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
46+
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
47+
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
4848
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4949
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
5050
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
5454
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5555
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
5656
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57-
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
58-
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5957
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
6058
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
6159
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=

vt/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ import (
1818
"os"
1919

2020
"github.com/VirusTotal/vt-cli/cmd"
21-
homedir "github.com/mitchellh/go-homedir"
2221
"github.com/spf13/cobra"
2322
"github.com/spf13/viper"
2423
)
2524

2625
// initConfig reads in config file and ENV variables if set.
2726
func initConfig() {
28-
29-
// Find home directory.
30-
home, err := homedir.Dir()
27+
// Find homeDir directory.
28+
homeDir, err := os.UserHomeDir()
3129
if err != nil {
3230
fmt.Fprintln(os.Stderr, err)
3331
os.Exit(1)
3432
}
3533

3634
// Search config in home directory and current directory
37-
viper.AddConfigPath(home)
35+
viper.AddConfigPath(homeDir)
3836
viper.AddConfigPath(".")
3937
// Config file must be named .vt + format extension (.toml, .json, etc)
4038
viper.SetConfigName(".vt")

0 commit comments

Comments
 (0)