Skip to content

Commit

Permalink
Merge pull request #150 from r4um/config-dir-cache
Browse files Browse the repository at this point in the history
Allow specifying config and cache directory via environment variables
  • Loading branch information
danielgtaylor authored Dec 8, 2022
2 parents b5765fb + 1376209 commit e152134
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func cacheAPI(name string, api *API) {
if err != nil {
LogError("Could not marshal API cache %s", err)
}
filename := path.Join(viper.GetString("config-directory"), name+".cbor")
filename := path.Join(getCacheDir(), name+".cbor")
if err := os.WriteFile(filename, b, 0o600); err != nil {
LogError("Could not write API cache %s", err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func Load(entrypoint string, root *cobra.Command) (API, error) {
expires := Cache.GetTime(name + ".expires")
if !viper.GetBool("rsh-no-cache") && !expires.IsZero() && expires.After(time.Now()) {
var cached API
filename := path.Join(viper.GetString("config-directory"), name+".cbor")
filename := path.Join(getCacheDir(), name+".cbor")
if data, err := os.ReadFile(filename); err == nil {
if err := cbor.Unmarshal(data, &cached); err == nil {
if cached.RestishVersion == root.Version {
Expand Down
35 changes: 30 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,33 @@ func userHomeDir() string {
return os.Getenv("HOME")
}

func cacheDir() string {
return path.Join(userHomeDir(), "."+viper.GetString("app-name"))
func getConfigDir(appName string) string {
configDirEnv := strings.ToUpper(appName) + "_CONFIG_DIR"

configDir := os.Getenv(configDirEnv)

if configDir == "" {
configDir = path.Join(userHomeDir(), "."+appName)
}
return configDir
}

func getCacheDir() string {
appName := viper.GetString("app-name")
cacheDirEnv := strings.ToUpper(appName) + "_CACHE_DIR"

cacheDir := os.Getenv(cacheDirEnv)

if cacheDir == "" {
cacheDir = path.Join(userHomeDir(), "."+appName)
}
return cacheDir
}

func initConfig(appName, envPrefix string) {
// One-time setup to ensure the path exists so we can write files into it
// later as needed.
configDir := path.Join(userHomeDir(), "."+appName)
configDir := getConfigDir(appName)
if err := os.MkdirAll(configDir, 0700); err != nil {
panic(err)
}
Expand All @@ -603,11 +622,17 @@ func initConfig(appName, envPrefix string) {
func initCache(appName string) {
Cache = viper.New()
Cache.SetConfigName("cache")
Cache.AddConfigPath(viper.GetString("config-directory"))

cacheDir := getCacheDir()
if err := os.MkdirAll(cacheDir, 0700); err != nil {
panic(err)
}

Cache.AddConfigPath(cacheDir)

// Write a blank cache if no file is already there. Later you can use
// cli.Cache.SaveConfig() to write new values.
filename := path.Join(viper.GetString("config-directory"), "cache.json")
filename := path.Join(cacheDir, "cache.json")
if _, err := os.Stat(filename); os.IsNotExist(err) {
if err := os.WriteFile(filename, []byte("{}"), 0600); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion cli/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func shouldCache(resp *http.Response) bool {

// CachedTransport returns an HTTP transport with caching abilities.
func CachedTransport() *httpcache.Transport {
t := httpcache.NewTransport(diskcache.New(path.Join(cacheDir(), "responses")))
t := httpcache.NewTransport(diskcache.New(path.Join(getCacheDir(), "responses")))
t.MarkCachedResponses = false
return t
}
Expand Down

0 comments on commit e152134

Please sign in to comment.