Refactor input params as Options (#9)

* some minor code refactor

* unsaved changes

* Minor cleanup

Co-authored-by: t-dedah <t-dedah@github.com>
This commit is contained in:
Bishal Prasad
2022-07-01 00:22:03 +05:30
committed by GitHub
parent c98ebfe90b
commit 47c4b91fe4
6 changed files with 115 additions and 80 deletions

View File

@@ -14,7 +14,7 @@ import (
func NewCmdDelete() *cobra.Command {
COMMAND = "delete"
f := types.InputFlags{}
f := types.DeleteOptions{}
var deleteCmd = &cobra.Command{
Use: "delete",
@@ -24,7 +24,8 @@ func NewCmdDelete() *cobra.Command {
fmt.Printf("accepts 1 arg(s), received %d\n", len(args))
return
}
key := args[0]
f.Key = args[0]
repo, err := internal.GetRepo(f.Repo)
if err != nil {
@@ -32,13 +33,14 @@ func NewCmdDelete() *cobra.Command {
return
}
artifactCache := service.NewArtifactCache(repo, COMMAND, VERSION)
queryParams := internal.GenerateQueryParams(f.Branch, 100, key, "", "", 1)
queryParams := url.Values{}
f.GenerateBaseQueryParams(queryParams)
if !f.Confirm {
var matchedCaches = getCacheListWithExactMatch(queryParams, key, artifactCache)
var matchedCaches = getCacheListWithExactMatch(f, artifactCache)
matchedCachesLen := len(matchedCaches)
if matchedCachesLen == 0 {
fmt.Printf("Cache with input key '%s' does not exist\n", key)
fmt.Printf("Cache with input key '%s' does not exist\n", f.Key)
return
}
fmt.Printf("You're going to delete %s", internal.PrintSingularOrPlural(matchedCachesLen, "cache entry\n\n", "cache entries\n\n"))
@@ -59,9 +61,9 @@ func NewCmdDelete() *cobra.Command {
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)
fmt.Printf("%s Deleted %s with key '%s'\n", internal.RedTick(), internal.PrintSingularOrPlural(cachesDeleted, "cache entry", "cache entries"), f.Key)
} else {
fmt.Printf("Cache with input key '%s' does not exist\n", key)
fmt.Printf("Cache with input key '%s' does not exist\n", f.Key)
}
}
},
@@ -97,11 +99,16 @@ EXAMPLES:
`
}
func getCacheListWithExactMatch(queryParams url.Values, key string, artifactCache service.ArtifactCacheService) []types.ActionsCache {
caches := artifactCache.ListAllCaches(queryParams, key)
func getCacheListWithExactMatch(f types.DeleteOptions, artifactCache service.ArtifactCacheService) []types.ActionsCache {
listOption := types.ListOptions{BaseOptions: types.BaseOptions{Repo: f.Repo, Branch: f.Branch, Key: f.Key}, Limit: 100, Order: "", Sort: ""}
queryParams := url.Values{}
listOption.GenerateBaseQueryParams(queryParams)
caches := artifactCache.ListAllCaches(queryParams, f.Key)
var exactMatchedKeys []types.ActionsCache
for _, cache := range caches {
if strings.EqualFold(key, cache.Key) {
if strings.EqualFold(f.Key, cache.Key) {
exactMatchedKeys = append(exactMatchedKeys, cache)
}
}

View File

@@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"net/url"
"github.com/actions/gh-actions-cache/internal"
"github.com/actions/gh-actions-cache/service"
@@ -13,7 +14,7 @@ import (
func NewCmdList() *cobra.Command {
COMMAND = "list"
f := types.InputFlags{}
f := types.ListOptions{}
var listCmd = &cobra.Command{
Use: "list",
@@ -30,7 +31,10 @@ func NewCmdList() *cobra.Command {
log.Fatal(err)
}
validateInputs(f)
err = f.Validate()
if err != nil {
log.Fatal(err)
}
artifactCache := service.NewArtifactCache(repo, COMMAND, VERSION)
@@ -39,7 +43,8 @@ func NewCmdList() *cobra.Command {
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 := url.Values{}
f.GenerateQueryParams(queryParams)
listCacheResponse := artifactCache.ListCaches(queryParams)
totalCaches := listCacheResponse.TotalCount
@@ -68,20 +73,6 @@ func displayedEntriesCount(totalCaches int, limit int) int {
return limit
}
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.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)))
}
}
func getListHelp() string {
return `
gh-actions-cache: Works with GitHub Actions Cache.