Skip to content

Commit

Permalink
cli/cmd: add commands for viewing the cache location and clearing it (#…
Browse files Browse the repository at this point in the history
…384)

* cli/cmd: add commands for viewing the cache location and clearing it

* extract function

* extract cache paths
  • Loading branch information
calebdoxsey authored Jan 16, 2024
1 parent 42aa453 commit 012055f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
44 changes: 36 additions & 8 deletions cmd/pomerium-cli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,57 @@ import (
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"

"github.com/pomerium/cli/internal/cache"
)

func cachePath() (string, error) {
root, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(root, "pomerium-cli", "exec-credential"), nil
var cacheCmd = &cobra.Command{
Use: "cache",
Short: "commands for working with the cache",
}

var cacheClearCmd = &cobra.Command{
Use: "clear",
Short: "clear the cache",
RunE: func(_ *cobra.Command, _ []string) error {
return cache.Clear()
},
}

var cacheLocationCmd = &cobra.Command{
Use: "location",
Short: "print the cache location",
RunE: func(cmd *cobra.Command, args []string) error {
root, err := cache.RootPath()
if err != nil {
return err
}
fmt.Println(root)
return nil
},
}

func init() {
cacheCmd.AddCommand(cacheClearCmd)
cacheCmd.AddCommand(cacheLocationCmd)
rootCmd.AddCommand(cacheCmd)
}

func cachedCredentialPath(serverURL string) (string, error) {
h := sha256.New()
_, _ = h.Write([]byte(serverURL))
id := hex.EncodeToString(h.Sum(nil))
p, err := cachePath()
p, err := cache.ExecCredentialsPath()
if err != nil {
return "", err
}
return filepath.Join(p, id+".json"), nil
}

func clearAllCachedCredentials() error {
cache, err := cachePath()
cache, err := cache.ExecCredentialsPath()
if err != nil {
return err
}
Expand Down
43 changes: 43 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Package cache contains functions for working with caches.
package cache

import (
"os"
"path/filepath"
)

// Clear clears the cache.
func Clear() error {
root, err := RootPath()
if err != nil {
return err
}
return os.RemoveAll(root)
}

// RootPath returns the root cache path.
func RootPath() (string, error) {
root, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(root, "pomerium-cli"), nil
}

// ExecCredentialsPath returns the path to the exec credentials.
func ExecCredentialsPath() (string, error) {
root, err := RootPath()
if err != nil {
return "", err
}
return filepath.Join(root, "exec-credential"), nil
}

// JWTsPath returns the path to the jwts.
func JWTsPath() (string, error) {
root, err := RootPath()
if err != nil {
return "", err
}
return filepath.Join(root, "jwts"), nil
}
5 changes: 2 additions & 3 deletions jwt/jwtcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-jose/go-jose/v3"
"github.com/martinlindhe/base36"

"github.com/pomerium/cli/internal/cache"
"github.com/pomerium/pomerium/pkg/cryptutil"
)

Expand All @@ -36,13 +37,11 @@ type LocalJWTCache struct {

// NewLocalJWTCache creates a new LocalJWTCache.
func NewLocalJWTCache() (*LocalJWTCache, error) {
root, err := os.UserCacheDir()
dir, err := cache.JWTsPath()
if err != nil {
return nil, err
}

dir := filepath.Join(root, "pomerium-cli", "jwts")

err = os.MkdirAll(dir, 0o755)
if err != nil {
return nil, fmt.Errorf("error creating user cache directory: %w", err)
Expand Down

0 comments on commit 012055f

Please sign in to comment.