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

@@ -25,13 +25,3 @@ 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
}

78
types/options.go Normal file
View File

@@ -0,0 +1,78 @@
package types
import (
"fmt"
"net/url"
"strconv"
"strings"
)
var SORT_INPUT_TO_QUERY_MAP = map[string]string{
"created-at": "created_at",
"last-used": "last_accessed_at",
"size": "size_in_bytes",
}
type BaseOptions struct {
Repo string
Branch string
Key string
}
type ListOptions struct {
BaseOptions
Limit int
Order string
Sort string
}
type DeleteOptions struct {
BaseOptions
Confirm bool
}
func (o *ListOptions) Validate() error {
if o.Order != "" && o.Order != "asc" && o.Order != "desc" {
return fmt.Errorf(fmt.Sprintf("%s is not a valid value for order flag. Allowed values: asc/desc", o.Order))
}
if o.Sort != "" && o.Sort != "last-used" && o.Sort != "size" && o.Sort != "created-at" {
return fmt.Errorf(fmt.Sprintf("%s is not a valid value for sort flag. Allowed values: last-used/size/created-at", o.Sort))
}
if o.Limit < 1 || o.Limit > 100 {
return fmt.Errorf(fmt.Sprintf("%d is not a valid value for limit flag. Allowed values: 1-100", o.Limit))
}
return nil
}
func (o *BaseOptions) GenerateBaseQueryParams(query url.Values) {
if o.Branch != "" {
if strings.HasPrefix(o.Branch, "refs/") {
query.Add("ref", o.Branch)
} else {
query.Add("ref", fmt.Sprintf("refs/heads/%s", o.Branch))
}
}
if o.Key != "" {
query.Add("key", o.Key)
}
}
func (o *ListOptions) GenerateQueryParams(query url.Values) {
if o.Limit != 30 {
query.Add("per_page", strconv.Itoa(o.Limit))
}
if o.Order != "" {
query.Add("direction", o.Order)
}
if o.Sort != "" {
query.Add("sort", SORT_INPUT_TO_QUERY_MAP[o.Sort])
}
o.GenerateBaseQueryParams(query)
}