Skip to content

Commit

Permalink
tidy up challenge list output
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Aug 29, 2024
1 parent fb74744 commit e0ae68f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cmd/challenge/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func newListCommand(cli labcli.CLI) *cobra.Command {
return cmd
}

type challengeListItem struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Categories []string `json:"categories" yaml:"categories"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
URL string `json:"url" yaml:"url"`
Attempted int `json:"attempted" yaml:"attempted"`
Completed int `json:"completed" yaml:"completed"`
}

func runListChallenges(ctx context.Context, cli labcli.CLI, opts *listOptions) error {
challenges, err := cli.Client().ListChallenges(ctx, &api.ListChallengesOptions{
Category: opts.category,
Expand All @@ -48,7 +59,21 @@ func runListChallenges(ctx context.Context, cli labcli.CLI, opts *listOptions) e
return fmt.Errorf("cannot list challenges: %w", err)
}

if err := yaml.NewEncoder(cli.OutputStream()).Encode(challenges); err != nil {
var items []challengeListItem
for _, ch := range challenges {
items = append(items, challengeListItem{
Name: ch.Name,
Title: ch.Title,
Description: ch.Description,
Categories: ch.Categories,
Tags: ch.Tags,
URL: ch.PageURL,
Attempted: ch.AttemptCount,
Completed: ch.CompletionCount,
})
}

if err := yaml.NewEncoder(cli.OutputStream()).Encode(items); err != nil {
return err
}

Expand Down

0 comments on commit e0ae68f

Please sign in to comment.