Skip to content

Commit

Permalink
fix for panic when --cache and --verbose flags are supplied, tidy up …
Browse files Browse the repository at this point in the history
…verbose output for cache-releated stuff a bit
  • Loading branch information
danenania committed Mar 1, 2019
1 parent ad39d26 commit 276397e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ type Cache struct {
Done chan error
}

func DefaultPath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", err
}
return filepath.Join(home, ".envkey", "cache"), nil
}

func NewCache(dir string) (*Cache, error) {
var withDir string
var err error

if dir == "" {
home, err := homedir.Dir()
withDir, err = DefaultPath()
if err != nil {
return nil, err
}
withDir = filepath.Join(home, ".envkey", "cache")

} else {
withDir, err = homedir.Expand(dir)
Expand Down
12 changes: 9 additions & 3 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ func Fetch(envkey string, options FetchOptions) (string, error) {

if options.ShouldCache {
if options.VerboseOutput {
fmt.Fprintf(os.Stderr, "Initializing cache at %s", options.CacheDir)
var cachePath string
if options.CacheDir == "" {
cachePath, _ = cache.DefaultPath()
} else {
cachePath = options.CacheDir
}
fmt.Fprintf(os.Stderr, "Initializing cache at %s\n", cachePath)
}

// If initializing cache fails for some reason, ignore and let it be nil
fetchCache, cacheErr = cache.NewCache(options.CacheDir)

if options.VerboseOutput {
fmt.Fprintf(os.Stderr, "Error initializing cache: %s", cacheErr.Error())
if options.VerboseOutput && cacheErr != nil {
fmt.Fprintf(os.Stderr, "Error initializing cache: %s\n", cacheErr.Error())
}
}

Expand Down

0 comments on commit 276397e

Please sign in to comment.