Skip to content

Commit

Permalink
Merge pull request cli#8667 from toshimaru/add-cache-key-optoion-to-c…
Browse files Browse the repository at this point in the history
…ache-list

feat: Add cache key option to `gh cache list`
  • Loading branch information
williammartin committed Feb 12, 2024
2 parents ae2a361 + 960c666 commit 8948ee8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/cmd/cache/list/list.go
Expand Up @@ -27,6 +27,7 @@ type ListOptions struct {
Limit int
Order string
Sort string
Key string
}

func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
Expand All @@ -47,6 +48,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
# List caches sorted by least recently accessed
$ gh cache list --sort last_accessed_at --order asc
# List caches that have keys matching a prefix (or that match exactly)
$ gh cache list --key key-prefix
`),
Aliases: []string{"ls"},
Args: cobra.NoArgs,
Expand All @@ -69,6 +73,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 30, "Maximum number of caches to fetch")
cmdutil.StringEnumFlag(cmd, &opts.Order, "order", "O", "desc", []string{"asc", "desc"}, "Order of caches returned")
cmdutil.StringEnumFlag(cmd, &opts.Sort, "sort", "S", "last_accessed_at", []string{"created_at", "last_accessed_at", "size_in_bytes"}, "Sort fetched caches")
cmd.Flags().StringVarP(&opts.Key, "key", "k", "", "Filter by cache key prefix")
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.CacheFields)

return cmd
Expand All @@ -87,7 +92,7 @@ func listRun(opts *ListOptions) error {
client := api.NewClientFromHTTP(httpClient)

opts.IO.StartProgressIndicator()
result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order})
result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order, Key: opts.Key})
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("%s Failed to get caches: %w", opts.IO.ColorScheme().FailureIcon(), err)
Expand Down
35 changes: 35 additions & 0 deletions pkg/cmd/cache/list/list_test.go
Expand Up @@ -30,6 +30,7 @@ func TestNewCmdList(t *testing.T) {
Limit: 30,
Order: "desc",
Sort: "last_accessed_at",
Key: "",
},
},
{
Expand All @@ -39,6 +40,7 @@ func TestNewCmdList(t *testing.T) {
Limit: 100,
Order: "desc",
Sort: "last_accessed_at",
Key: "",
},
},
{
Expand All @@ -53,6 +55,7 @@ func TestNewCmdList(t *testing.T) {
Limit: 30,
Order: "desc",
Sort: "created_at",
Key: "",
},
},
{
Expand All @@ -62,6 +65,17 @@ func TestNewCmdList(t *testing.T) {
Limit: 30,
Order: "asc",
Sort: "last_accessed_at",
Key: "",
},
},
{
name: "with key",
input: "--key cache-key-prefix-",
wants: ListOptions{
Limit: 30,
Order: "desc",
Sort: "last_accessed_at",
Key: "cache-key-prefix-",
},
},
}
Expand Down Expand Up @@ -90,6 +104,7 @@ func TestNewCmdList(t *testing.T) {
assert.Equal(t, tt.wants.Limit, gotOpts.Limit)
assert.Equal(t, tt.wants.Sort, gotOpts.Sort)
assert.Equal(t, tt.wants.Order, gotOpts.Order)
assert.Equal(t, tt.wants.Key, gotOpts.Key)
})
}
}
Expand Down Expand Up @@ -171,6 +186,26 @@ ID KEY SIZE CREATED ACCESSED
},
wantStdout: "1\tfoo\t100 B\t2021-01-01T01:01:01Z\t2022-01-01T01:01:01Z\n2\tbar\t1.00 KiB\t2021-01-01T01:01:01Z\t2022-01-01T01:01:01Z\n",
},
{
name: "only requests caches with the provided key prefix",
opts: ListOptions{
Key: "test-key",
},
stubs: func(reg *httpmock.Registry) {
reg.Register(
func(req *http.Request) bool {
return req.URL.Query().Get("key") == "test-key"
},
httpmock.JSONResponse(shared.CachePayload{
ActionsCaches: []shared.Cache{},
TotalCount: 0,
}))
},
// We could put anything here, we're really asserting that the key is passed
// to the API.
wantErr: true,
wantErrMsg: "No caches found in OWNER/REPO",
},
{
name: "displays no results",
stubs: func(reg *httpmock.Registry) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/cache/shared/shared.go
Expand Up @@ -2,6 +2,7 @@ package shared

import (
"fmt"
"net/url"
"time"

"github.com/cli/cli/v2/api"
Expand Down Expand Up @@ -38,6 +39,7 @@ type GetCachesOptions struct {
Limit int
Order string
Sort string
Key string
}

// Return a list of caches for a repository. Pass a negative limit to request
Expand All @@ -57,6 +59,9 @@ func GetCaches(client *api.Client, repo ghrepo.Interface, opts GetCachesOptions)
if opts.Order != "" {
path += fmt.Sprintf("&direction=%s", opts.Order)
}
if opts.Key != "" {
path += fmt.Sprintf("&key=%s", url.QueryEscape(opts.Key))
}

var result *CachePayload
pagination:
Expand Down

0 comments on commit 8948ee8

Please sign in to comment.