From c98ebfe90b7b86003b4dfe4e07c91ac144a4e597 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Tue, 28 Jun 2022 14:40:11 +0530 Subject: [PATCH] E2E delete command using list API (#4) * Completed List cmd and added API calls * Minor comments and add delete code to pass linting * Typo in descriptions * Added delete functionality using existing list API * Updated limit param for list api to default 30 * Minor refactoring * Linting fixes for survey output * Implemented review comments * Handling 404 response when confirm flag is passed * Added COMMAND back to delete CLI * Minor comments * Check http err statuscode for 404 * Validations * Validations-1 * improved branch flag validation * removed build * String match made case insensitive * Added TODO for error handling * Updated error message when args are not provided * Worked on review comments * Argument length check updated * Separated direct and indirect dependencies * Used SPrintF for formatting strings * Updated lastAccessed time logic * Removed extra variable userConfirmation * Removed unnecessary computations * Printing and formatting changes * Passed key from input in queryparams * Scan List API iteratively to get exact matches * Added pretty print for trimmed list * Update page number instead of re-generating params * Added listAllCaches method and moved it to utils * Moved redTick to utils * Update internal/utils.go Co-authored-by: Bishal Prasad * Limited scope of `sb` to `if` block * Fixed pretty print issue * Error type checked for httpError * Added PrintOneOrMore fn, moved listAll to service * Implemented `Goment` for last accessed time * Used percentage based on window size for printing * Removed stringbuilder and updated fn name * Made `ListAllCaches` member of `actions_cache.go` * Updated prettyPrint logic cover better content * Using PrettyPrint for List command as well. * Separated direct and indirect modules Co-authored-by: t-dedah Co-authored-by: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Co-authored-by: Bishal Prasad --- cmd/delete.go | 101 +++++++++++++++++++++++++++++---------- cmd/list.go | 52 ++++++++------------ cmd/root.go | 1 + go.mod | 16 +++++-- go.sum | 55 ++++++++++++++++++++- internal/utils.go | 68 ++++++++++++++++++++++++++ service/actions_cache.go | 27 ++++++++++- types/model.go | 10 ++++ 8 files changed, 269 insertions(+), 61 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 0de064d..9337a1d 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,35 +1,77 @@ package cmd import ( + "fmt" + "net/url" + "strings" + + "github.com/AlecAivazis/survey/v2" + "github.com/actions/gh-actions-cache/internal" + "github.com/actions/gh-actions-cache/service" + "github.com/actions/gh-actions-cache/types" "github.com/spf13/cobra" - // "github.com/actions/gh-actions-cache/internal" - // "github.com/actions/gh-actions-cache/client" ) -func init() { - rootCmd.AddCommand(deleteCmd) - deleteCmd.Flags().StringP("repo", "R", "", "Select another repository for finding actions cache.") - deleteCmd.Flags().StringP("branch", "B", "", "Filter by branch") +func NewCmdDelete() *cobra.Command { + COMMAND = "delete" + f := types.InputFlags{} + + var deleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete cache by key", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 1 { + fmt.Printf("accepts 1 arg(s), received %d\n", len(args)) + return + } + key := args[0] + + repo, err := internal.GetRepo(f.Repo) + if err != nil { + fmt.Println(err) + return + } + artifactCache := service.NewArtifactCache(repo, COMMAND, VERSION) + queryParams := internal.GenerateQueryParams(f.Branch, 100, key, "", "", 1) + + if !f.Confirm { + var matchedCaches = getCacheListWithExactMatch(queryParams, key, artifactCache) + matchedCachesLen := len(matchedCaches) + if matchedCachesLen == 0 { + fmt.Printf("Cache with input key '%s' does not exist\n", key) + return + } + fmt.Printf("You're going to delete %s", internal.PrintSingularOrPlural(matchedCachesLen, "cache entry\n\n", "cache entries\n\n")) + internal.PrettyPrintTrimmedCacheList(matchedCaches) + choice := "" + prompt := &survey.Select{ + Message: "Are you sure you want to delete the cache entries?", + Options: []string{"Delete", "Cancel"}, + } + err := survey.AskOne(prompt, &choice) + if err != nil { + fmt.Println("Error occured while taking input from user while trying to delete cache") + return + } + f.Confirm = choice == "Delete" + fmt.Println() + } + if f.Confirm { + cachesDeleted := artifactCache.DeleteCaches(queryParams) + if cachesDeleted > 0 { + fmt.Printf("%s Deleted %s with key '%s'\n", internal.RedTick(), internal.PrintSingularOrPlural(cachesDeleted, "cache entry", "cache entries"), key) + } else { + fmt.Printf("Cache with input key '%s' does not exist\n", key) + } + } + }, + } + deleteCmd.Flags().StringVarP(&f.Repo, "repo", "R", "", "Select another repository for finding actions cache.") + deleteCmd.Flags().StringVarP(&f.Branch, "branch", "B", "", "Filter by branch") + deleteCmd.Flags().BoolVar(&f.Confirm, "confirm", false, "Delete the cache without asking user for confirmation.") deleteCmd.SetHelpTemplate(getDeleteHelp()) -} -var deleteCmd = &cobra.Command{ - Use: "delete", - Short: "Delete cache by key", - Long: `Delete cache by key`, - Run: func(cmd *cobra.Command, args []string) { - COMMAND = "delete" - // r, _ := cmd.Flags().GetString("repo") - // branch, _ := cmd.Flags().GetString("branch") - - // repo, err := getRepo(r) - // if err != nil { - // log.Fatal(err) - // } - - // queryParams := generateQueryParams(branch, 30, "", "", "") - // deleteCaches(repo, queryParams) - }, + return deleteCmd } func getDeleteHelp() string { @@ -54,3 +96,14 @@ EXAMPLES: $ gh actions-cache delete Linux-node-f5dbf39c9d11eba80242ac13 ` } + +func getCacheListWithExactMatch(queryParams url.Values, key string, artifactCache service.ArtifactCacheService) []types.ActionsCache { + caches := artifactCache.ListAllCaches(queryParams, key) + var exactMatchedKeys []types.ActionsCache + for _, cache := range caches { + if strings.EqualFold(key, cache.Key) { + exactMatchedKeys = append(exactMatchedKeys, cache) + } + } + return exactMatchedKeys +} diff --git a/cmd/list.go b/cmd/list.go index ea729d6..25e3735 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -6,24 +6,16 @@ import ( "github.com/actions/gh-actions-cache/internal" "github.com/actions/gh-actions-cache/service" + "github.com/actions/gh-actions-cache/types" "github.com/spf13/cobra" ) -type InputFlags struct { - repo string - branch string - limit int - key string - order string - sort string -} - func NewCmdList() *cobra.Command { COMMAND = "list" - f := InputFlags{} + f := types.InputFlags{} - var listCmd = &cobra.Command { + var listCmd = &cobra.Command{ Use: "list", Short: "Lists the actions cache", Run: func(cmd *cobra.Command, args []string) { @@ -33,7 +25,7 @@ func NewCmdList() *cobra.Command { return } - repo, err := internal.GetRepo(f.repo) + repo, err := internal.GetRepo(f.Repo) if err != nil { log.Fatal(err) } @@ -42,30 +34,28 @@ func NewCmdList() *cobra.Command { artifactCache := service.NewArtifactCache(repo, COMMAND, VERSION) - if f.branch == "" && f.key == "" { + if f.Branch == "" && f.Key == "" { totalCacheSize := artifactCache.GetCacheUsage() fmt.Printf("Total caches size %s\n\n", internal.FormatCacheSize(totalCacheSize)) } - queryParams := internal.GenerateQueryParams(f.branch, f.limit, f.key, f.order, f.sort, 1) + queryParams := internal.GenerateQueryParams(f.Branch, f.Limit, f.Key, f.Order, f.Sort, 1) listCacheResponse := artifactCache.ListCaches(queryParams) totalCaches := listCacheResponse.TotalCount caches := listCacheResponse.ActionsCaches - fmt.Printf("Showing %d of %d cache entries in %s/%s\n\n", displayedEntriesCount(len(caches), f.limit), totalCaches, repo.Owner(), repo.Name()) - for _, cache := range caches { - fmt.Printf("%s\t [%s]\t %s\t %s\n", cache.Key, internal.FormatCacheSize(cache.SizeInBytes), cache.Ref, cache.LastAccessedAt) - } + fmt.Printf("Showing %d of %d cache entries in %s/%s\n\n", displayedEntriesCount(len(caches), f.Limit), totalCaches, repo.Owner(), repo.Name()) + internal.PrettyPrintCacheList(caches) }, } - listCmd.Flags().StringVarP(&f.repo, "repo", "R", "", "Select another repository for finding actions cache.") - listCmd.Flags().StringVarP(&f.branch, "branch", "B", "", "Filter by branch") - listCmd.Flags().IntVarP(&f.limit, "limit", "", 30, "Maximum number of items to fetch (default is 30, max limit is 100)") - listCmd.Flags().StringVarP(&f.key, "key", "", "", "Filter by key") - listCmd.Flags().StringVarP(&f.order, "order", "", "", "Order of caches returned (asc/desc)") - listCmd.Flags().StringVarP(&f.sort, "sort", "", "", "Sort fetched caches (last-used/size/created-at)") + listCmd.Flags().StringVarP(&f.Repo, "repo", "R", "", "Select another repository for finding actions cache.") + listCmd.Flags().StringVarP(&f.Branch, "branch", "B", "", "Filter by branch") + listCmd.Flags().IntVarP(&f.Limit, "limit", "", 30, "Maximum number of items to fetch (default is 30, max limit is 100)") + listCmd.Flags().StringVarP(&f.Key, "key", "", "", "Filter by key") + listCmd.Flags().StringVarP(&f.Order, "order", "", "", "Order of caches returned (asc/desc)") + listCmd.Flags().StringVarP(&f.Sort, "sort", "", "", "Sort fetched caches (last-used/size/created-at)") listCmd.SetHelpTemplate(getListHelp()) return listCmd @@ -78,17 +68,17 @@ func displayedEntriesCount(totalCaches int, limit int) int { return limit } -func validateInputs(input InputFlags) { - if input.order != "" && input.order != "asc" && input.order != "desc" { - log.Fatal(fmt.Errorf(fmt.Sprintf("%s is not a valid value for order flag. Allowed values: asc/desc", input.order))) +func validateInputs(input types.InputFlags) { + if input.Order != "" && input.Order != "asc" && input.Order != "desc" { + log.Fatal(fmt.Errorf(fmt.Sprintf("%s is not a valid value for order flag. Allowed values: asc/desc", input.Order))) } - if input.sort != "" && input.sort != "last-used" && input.sort != "size" && input.sort != "created-at" { - log.Fatal(fmt.Errorf(fmt.Sprintf("%s is not a valid value for sort flag. Allowed values: last-used/size/created-at", input.sort))) + if input.Sort != "" && input.Sort != "last-used" && input.Sort != "size" && input.Sort != "created-at" { + log.Fatal(fmt.Errorf(fmt.Sprintf("%s is not a valid value for sort flag. Allowed values: last-used/size/created-at", input.Sort))) } - if input.limit < 1 || input.limit > 100 { - log.Fatal(fmt.Errorf(fmt.Sprintf("%d is not a valid value for limit flag. Allowed values: 1-100", input.limit))) + if input.Limit < 1 || input.Limit > 100 { + log.Fatal(fmt.Errorf(fmt.Sprintf("%d is not a valid value for limit flag. Allowed values: 1-100", input.Limit))) } } diff --git a/cmd/root.go b/cmd/root.go index 4eddf0a..77c9cee 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,6 +29,7 @@ func init() { func addCommandsToRoot() { rootCmd.AddCommand(NewCmdList()) + rootCmd.AddCommand(NewCmdDelete()) } func getRootHelp() string { diff --git a/go.mod b/go.mod index e3f554f..f41a2d0 100644 --- a/go.mod +++ b/go.mod @@ -3,19 +3,29 @@ module github.com/actions/gh-actions-cache go 1.18 require ( + github.com/AlecAivazis/survey/v2 v2.3.5 + github.com/TwiN/go-color v1.1.0 github.com/cli/go-gh v0.0.3 + github.com/nleeper/goment v1.4.4 github.com/spf13/cobra v1.4.0 - github.com/stretchr/testify v1.7.0 ) require ( + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/cli/safeexec v1.0.0 // indirect github.com/cli/shurcooL-graphql v0.0.1 // indirect - github.com/davecgh/go-spew v1.1.0 // indirect github.com/henvic/httpretty v0.0.6 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect + github.com/mattn/go-colorable v0.1.2 // indirect + github.com/mattn/go-isatty v0.0.8 // indirect + github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect + github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 github.com/spf13/pflag v1.0.5 // indirect + github.com/tkuchiki/go-timezone v0.2.0 // indirect golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect + golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect + golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect + golang.org/x/text v0.3.6 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index a19d32a..9fb0992 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,13 @@ +github.com/AlecAivazis/survey/v2 v2.3.5 h1:A8cYupsAZkjaUmhtTYv3sSqc7LO5mp1XDfqe5E/9wRQ= +github.com/AlecAivazis/survey/v2 v2.3.5/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= +github.com/TwiN/go-color v1.1.0 h1:yhLAHgjp2iAxmNjDiVb6Z073NE65yoaPlcki1Q22yyQ= +github.com/TwiN/go-color v1.1.0/go.mod h1:aKVf4e1mD4ai2FtPifkDPP5iyoCwiK08YGzGwerjKo0= github.com/cli/go-gh v0.0.3 h1:GcVgUa7q0SeauIRbch3VSUXVij6+c49jtAHv7WuWj5c= github.com/cli/go-gh v0.0.3/go.mod h1:J1eNgrPJYAUy7TwPKj7GW1ibqI+WCiMndtyzrCyZIiQ= github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI= @@ -7,34 +15,77 @@ github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5 github.com/cli/shurcooL-graphql v0.0.1 h1:/9J3t9O6p1B8zdBBtQighq5g7DQRItBwuwGh3SocsKM= github.com/cli/shurcooL-graphql v0.0.1/go.mod h1:U7gCSuMZP/Qy7kbqkk5PrqXEeDgtfG5K+W+u8weorps= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs= github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc= +github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw= +github.com/nleeper/goment v1.4.4 h1:GlMTpxvhueljArSunzYjN9Ri4SOmpn0Vh2hg2z/IIl8= +github.com/nleeper/goment v1.4.4/go.mod h1:zDl5bAyDhqxwQKAvkSXMRLOdCowrdZz53ofRJc4VhTo= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tkuchiki/go-timezone v0.2.0 h1:yyZVHtQRVZ+wvlte5HXvSpBkR0dPYnPEIgq9qqAqltk= +github.com/tkuchiki/go-timezone v0.2.0/go.mod h1:b1Ean9v2UXtxSq4TZF0i/TU9NuoWa9hOzOKoGCV2zqY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w= +golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -42,3 +93,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= +gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/internal/utils.go b/internal/utils.go index e6769a0..0656a72 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -2,12 +2,19 @@ package internal import ( "fmt" + "math" "net/url" + "os" "strconv" "strings" + "unicode/utf8" + "github.com/TwiN/go-color" + "github.com/actions/gh-actions-cache/types" gh "github.com/cli/go-gh" ghRepo "github.com/cli/go-gh/pkg/repository" + "github.com/moby/term" + "github.com/nleeper/goment" ) const MB_IN_BYTES = 1024 * 1024 @@ -70,3 +77,64 @@ func FormatCacheSize(size_in_bytes float64) string { return fmt.Sprintf("%.2f GB", size_in_bytes/GB_IN_BYTES) } + +func PrettyPrintCacheList(caches []types.ActionsCache) { + fd := os.Stdin.Fd() + ws, _ := term.GetWinsize(fd) + width := math.Min(float64(ws.Width), 180) + keyWidth := int(math.Floor(0.30 * width)) + sizeWidth := int(math.Floor(0.12 * width)) + refWidth := int(math.Floor(0.20 * width)) + timeWidth := int(math.Floor(0.20 * width)) + for _, cache := range caches { + var formattedRow string = getFormattedCacheInfo(cache, keyWidth, sizeWidth, refWidth, timeWidth) + fmt.Println(formattedRow) + } +} +func PrettyPrintTrimmedCacheList(caches []types.ActionsCache) { + length := len(caches) + limit := 30 + if length > limit { + PrettyPrintCacheList(caches[:limit]) + fmt.Printf("... and %d more\n\n", length-limit) + } else { + PrettyPrintCacheList(caches[:length]) + } + fmt.Print("\n") +} + +func lastAccessedTime(lastAccessedAt string) string { + lastAccessed, _ := goment.New(lastAccessedAt) + return fmt.Sprintf("Used %s", lastAccessed.FromNow()) +} + +func trimOrPad(value string, maxSize int) string { + if len(value) > maxSize { + value = value[:maxSize-3] + "..." + } else { + value = value + strings.Repeat(" ", maxSize-len(value)) + } + return value +} + +func getFormattedCacheInfo(cache types.ActionsCache, keyWidth int, sizeWidth int, refWidth int, timeWidth int) string { + key := trimOrPad(cache.Key, keyWidth) + size := trimOrPad(fmt.Sprintf("[%s]", FormatCacheSize(cache.SizeInBytes)), sizeWidth) + ref := trimOrPad(cache.Ref, refWidth) + time := trimOrPad(lastAccessedTime(cache.LastAccessedAt), timeWidth) + return fmt.Sprintf(" %s %s %s %s", key, size, ref, time) +} + +func RedTick() string { + src := "\u2713" + tick, _ := utf8.DecodeRuneInString(src) + redTick := color.Colorize(color.Red, string(tick)) + return redTick +} + +func PrintSingularOrPlural(count int, singularStr string, pluralStr string) string { + if count == 1 { + return fmt.Sprintf("%d %s", count, singularStr) + } + return fmt.Sprintf("%d %s", count, pluralStr) +} diff --git a/service/actions_cache.go b/service/actions_cache.go index a6f3aed..2bf3616 100644 --- a/service/actions_cache.go +++ b/service/actions_cache.go @@ -1,9 +1,12 @@ package service import ( + "errors" "fmt" "log" + "math" "net/url" + "strconv" "github.com/actions/gh-actions-cache/types" gh "github.com/cli/go-gh" @@ -15,6 +18,7 @@ type ArtifactCacheService interface { GetCacheUsage() float64 ListCaches(queryParams url.Values) types.ListApiResponse DeleteCaches(queryParams url.Values) int + ListAllCaches(queryParams url.Values, key string) []types.ActionsCache } type ArtifactCache struct { @@ -61,8 +65,27 @@ func (a *ArtifactCache) DeleteCaches(queryParams url.Values) int { var apiResults types.DeleteApiResponse err := a.HttpClient.Delete(pathComponent+"?"+queryParams.Encode(), &apiResults) if err != nil { - log.Fatal(err) + var httpError api.HTTPError + if errors.As(err, &httpError) && httpError.StatusCode == 404 { + return 0 + } else { + log.Fatal(err) + } } - return apiResults.TotalCount } + +func (a *ArtifactCache) ListAllCaches(queryParams url.Values, key string) []types.ActionsCache { + var listApiResponse types.ListApiResponse + listApiResponse = a.ListCaches(queryParams) + caches := listApiResponse.ActionsCaches + totalCaches := listApiResponse.TotalCount + if totalCaches > 100 { + for page := 2; page <= int(math.Ceil(float64(listApiResponse.TotalCount)/100)); page++ { + queryParams.Set("page", strconv.Itoa(page)) + listApiResponse = a.ListCaches(queryParams) + caches = append(caches, listApiResponse.ActionsCaches...) + } + } + return caches +} diff --git a/types/model.go b/types/model.go index 653a8f5..1d30a66 100644 --- a/types/model.go +++ b/types/model.go @@ -25,3 +25,13 @@ type ActionsCache struct { CreatedAt string `json:"created_at"` SizeInBytes float64 `json:"size_in_bytes"` } + +type InputFlags struct { + Repo string + Branch string + Limit int + Key string + Order string + Sort string + Confirm bool +}