Skip to content

Commit

Permalink
Add CacheDir function to config package (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffalor committed Aug 10, 2023
1 parent dadb47a commit baae867
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
"time"

"github.com/cli/go-gh/v2/pkg/asciisanitizer"
"github.com/cli/go-gh/v2/pkg/config"
"github.com/cli/go-gh/v2/pkg/term"
"github.com/henvic/httpretty"
"github.com/thlib/go-timezone-local/tzlocal"
Expand Down Expand Up @@ -68,7 +68,7 @@ func NewHTTPClient(opts ClientOptions) (*http.Client, error) {
transport = newSanitizerRoundTripper(transport)

if opts.CacheDir == "" {
opts.CacheDir = filepath.Join(os.TempDir(), "gh-cli-cache")
opts.CacheDir = config.CacheDir()
}
if opts.EnableCache && opts.CacheTTL == 0 {
opts.CacheTTL = time.Hour * 24
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ func DataDir() string {
return path
}

// CacheDir returns the default path for gh cli cache.
func CacheDir() string {
return filepath.Join(os.TempDir(), "gh-cli-cache")
}

func readFile(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ func TestDataDir(t *testing.T) {
}
}

func TestCacheDir(t *testing.T) {
expected := filepath.Join(os.TempDir(), "gh-cli-cache")
actual := CacheDir()
assert.Equal(t, expected, actual)
}

func TestLoad(t *testing.T) {
tempDir := t.TempDir()
globalFilePath := filepath.Join(tempDir, "config.yml")
Expand Down

0 comments on commit baae867

Please sign in to comment.