Skip to content

Commit

Permalink
Merge pull request #294 from vasubabu/fix-issue-292
Browse files Browse the repository at this point in the history
Added support for --include, --exclude, and --filter command-line flags
  • Loading branch information
ctreatma authored Jun 28, 2023
2 parents 1777458 + b96b150 commit 9aef362
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
71 changes: 71 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,77 @@ func (c *Client) NewCommand() *cobra.Command {
return c.rootCmd
}

func (c *Client) Includes(defaultIncludes []string) (incl []string) {
var inc []string

inc = defaultIncludes

if c.rootCmd.Flags().Changed("include") {
inc = *c.includes
}

return inc
}

func (c *Client) Excludes(defaultExcludes []string) (excl []string) {
var exc []string

exc = defaultExcludes

if c.rootCmd.Flags().Changed("exclude") {
exc = *c.excludes
}

return exc
}

func (c *Client) Search() (sea string) {
var search string

if c.rootCmd.Flags().Changed("search") {
search = c.search
}

return search
}

func (c *Client) SortBy() (sBy string) {

var sortBy string

if c.rootCmd.Flags().Changed("sort-by") {
sortBy = c.sortBy
}

return sortBy
}

func (c *Client) SortDirection() (sDir string) {
var sortDir string

if c.rootCmd.Flags().Changed("sort-dir") {
sortDir = c.sortDir
}

return sortDir
}

func (c *Client) Filters() map[string]string {
mapFilt := make(map[string]string)
if c.rootCmd.Flags().Changed("filter") {
for _, kv := range *c.filters {
var k, v string
tokens := strings.SplitN(kv, "=", 2)
k = strings.TrimSpace(tokens[0])
if len(tokens) != 1 {
v = strings.TrimSpace(tokens[1])
}
mapFilt[k] = v
}
}
return mapFilt
}

// ListOptions creates a packngo.ListOptions using the includes and excludes persistent
// flags. When not defined, the defaults given will be supplied.
func (c *Client) ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions {
Expand Down
3 changes: 3 additions & 0 deletions internal/plans/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (c *Client) NewCommand() *cobra.Command {

type Servicer interface {
MetalAPI(*cobra.Command) *metal.APIClient
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down
15 changes: 14 additions & 1 deletion internal/plans/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
plansList, _, err := c.Service.FindPlans(context.Background()).Execute()

request := c.Service.FindPlans(context.Background()).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil))
filters := c.Servicer.Filters()

if filters["type"] != "" {
request = request.Type_(filters["type"])
}

if filters["slug"] != "" {
request = request.Slug(filters["slug"])
}

plansList, _, err := request.Execute()
if err != nil {
return fmt.Errorf("could not list plans: %w", err)
}

plans := plansList.GetPlans()
data := make([][]string, len(plans))

Expand Down

0 comments on commit 9aef362

Please sign in to comment.