From 05118ae56882a5f73da8b1a69462062f3290f40b Mon Sep 17 00:00:00 2001 From: Harshil Gupta Date: Fri, 18 Oct 2024 14:22:14 +0530 Subject: [PATCH 1/3] Adding filter by status flag --- cmd/challenge/catalog.go | 11 ++++++++++- internal/api/challenges.go | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/challenge/catalog.go b/cmd/challenge/catalog.go index abe4801..289369b 100644 --- a/cmd/challenge/catalog.go +++ b/cmd/challenge/catalog.go @@ -13,13 +13,14 @@ import ( type catalogOptions struct { category string + status string } func newCatalogCommand(cli labcli.CLI) *cobra.Command { var opts catalogOptions cmd := &cobra.Command{ - Use: "catalog [--category ]", + Use: "catalog [--category ] --status ", Aliases: []string{"catalog"}, Short: "List challenges from the catalog, optionally filtered by category", Args: cobra.NoArgs, @@ -37,6 +38,13 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { `Category to filter by - one of linux, containers, kubernetes, ... (an empty string means all)`, ) + flags.StringVar( + &opts.status, + "status", + "", + `status to filter by - one of todo, attempted, solved ... (empty means all))`, + ) + return cmd } @@ -54,6 +62,7 @@ type catalogItem struct { func runCatalogChallenges(ctx context.Context, cli labcli.CLI, opts *catalogOptions) error { challenges, err := cli.Client().ListChallenges(ctx, &api.ListChallengesOptions{ Category: opts.category, + Status: opts.status, }) if err != nil { return fmt.Errorf("cannot list challenges: %w", err) diff --git a/internal/api/challenges.go b/internal/api/challenges.go index 39d4ff3..ea567f6 100644 --- a/internal/api/challenges.go +++ b/internal/api/challenges.go @@ -110,6 +110,7 @@ func (c *Client) GetChallenge(ctx context.Context, name string) (*Challenge, err type ListChallengesOptions struct { Category string + Status string } func (c *Client) ListChallenges(ctx context.Context, opts *ListChallengesOptions) ([]*Challenge, error) { @@ -118,6 +119,9 @@ func (c *Client) ListChallenges(ctx context.Context, opts *ListChallengesOptions if opts.Category != "" { query.Set("category", opts.Category) } + if opts.Status != "" { + query.Set("status", opts.Status) + } return challenges, c.GetInto(ctx, "/challenges", query, nil, &challenges) } From 3b5999d5c9cf9c784501609e5723c6407d444e93 Mon Sep 17 00:00:00 2001 From: Harshil Gupta Date: Fri, 18 Oct 2024 16:37:13 +0530 Subject: [PATCH 2/3] adding support for multiple category or statuses --- cmd/challenge/catalog.go | 18 +++++++++--------- internal/api/challenges.go | 17 +++++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/cmd/challenge/catalog.go b/cmd/challenge/catalog.go index 289369b..3a0c206 100644 --- a/cmd/challenge/catalog.go +++ b/cmd/challenge/catalog.go @@ -12,15 +12,15 @@ import ( ) type catalogOptions struct { - category string - status string + category []string + status []string } func newCatalogCommand(cli labcli.CLI) *cobra.Command { var opts catalogOptions cmd := &cobra.Command{ - Use: "catalog [--category ] --status ", + Use: "catalog [--category ] --status ", Aliases: []string{"catalog"}, Short: "List challenges from the catalog, optionally filtered by category", Args: cobra.NoArgs, @@ -31,18 +31,18 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { flags := cmd.Flags() - flags.StringVar( + flags.StringSliceVar( &opts.category, "category", - "", - `Category to filter by - one of linux, containers, kubernetes, ... (an empty string means all)`, + []string{}, + `Category to filter by - one or multiple categories like linux, containers, kubernetes, ... (an empty string means all)`, ) - flags.StringVar( + flags.StringSliceVar( &opts.status, "status", - "", - `status to filter by - one of todo, attempted, solved ... (empty means all))`, + []string{}, + `status to filter by - one or multiple status like todo, attempted, solved ... (empty means all))`, ) return cmd diff --git a/internal/api/challenges.go b/internal/api/challenges.go index ea567f6..752811c 100644 --- a/internal/api/challenges.go +++ b/internal/api/challenges.go @@ -109,19 +109,24 @@ func (c *Client) GetChallenge(ctx context.Context, name string) (*Challenge, err } type ListChallengesOptions struct { - Category string - Status string + Category []string + Status []string } func (c *Client) ListChallenges(ctx context.Context, opts *ListChallengesOptions) ([]*Challenge, error) { var challenges []*Challenge query := url.Values{} - if opts.Category != "" { - query.Set("category", opts.Category) + if opts.Category != nil { + for _, category := range opts.Category { + query.Add("category",category) + } } - if opts.Status != "" { - query.Set("status", opts.Status) + if opts.Status != nil { + for _, status := range opts.Status { + query.Add("status",status) + } } + return challenges, c.GetInto(ctx, "/challenges", query, nil, &challenges) } From 00999022c707f21f08a6d980220c9c294b4dc49a Mon Sep 17 00:00:00 2001 From: Harshil Gupta Date: Mon, 21 Oct 2024 11:23:32 +0530 Subject: [PATCH 3/3] addressing review comments --- cmd/challenge/catalog.go | 4 ++-- internal/api/challenges.go | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cmd/challenge/catalog.go b/cmd/challenge/catalog.go index 3a0c206..fcbd29b 100644 --- a/cmd/challenge/catalog.go +++ b/cmd/challenge/catalog.go @@ -35,14 +35,14 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { &opts.category, "category", []string{}, - `Category to filter by - one or multiple categories like linux, containers, kubernetes, ... (an empty string means all)`, + `Category to filter by - one or multiple categories like linux, containers, kubernetes`, ) flags.StringSliceVar( &opts.status, "status", []string{}, - `status to filter by - one or multiple status like todo, attempted, solved ... (empty means all))`, + `status to filter by - one or multiple status like todo, attempted, solved)`, ) return cmd diff --git a/internal/api/challenges.go b/internal/api/challenges.go index 752811c..8e17506 100644 --- a/internal/api/challenges.go +++ b/internal/api/challenges.go @@ -116,15 +116,13 @@ type ListChallengesOptions struct { func (c *Client) ListChallenges(ctx context.Context, opts *ListChallengesOptions) ([]*Challenge, error) { var challenges []*Challenge query := url.Values{} - if opts.Category != nil { - for _, category := range opts.Category { - query.Add("category",category) - } + + for _, category := range opts.Category { + query.Add("category", category) } - if opts.Status != nil { - for _, status := range opts.Status { - query.Add("status",status) - } + + for _, status := range opts.Status { + query.Add("status", status) } return challenges, c.GetInto(ctx, "/challenges", query, nil, &challenges)