2
0
mirror of https://github.com/hibiken/asynq.git synced 2026-06-10 13:21:30 +08:00
Files
asynq/tools/asynq/cmd/group.go
shiweikang f8d6677814 refactor(cli): migrate commands from Run to RunE and fix group error handling
Migrate all CLI command handlers from cobra's Run to RunE, replacing
fmt.Println(err) + os.Exit(1) patterns with idiomatic error returns.
This improves testability and lets cobra handle errors consistently
via the existing SilenceUsage/SilenceErrors configuration.

Also refactor getDuration() and getTime() helpers to return errors
instead of calling os.Exit(1).

Fix a bug in group.go where the error from inspector.Groups() was
never checked, causing silent failures (e.g. on Redis connection
errors) to be misreported as "No groups found".
2026-04-05 01:13:41 +08:00

55 lines
1.2 KiB
Go

// Copyright 2022 Kentaro Hibino. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package cmd
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(groupCmd)
groupCmd.AddCommand(groupListCmd)
groupListCmd.Flags().StringP("queue", "q", "", "queue to inspect")
groupListCmd.MarkFlagRequired("queue")
}
var groupCmd = &cobra.Command{
Use: "group <command> [flags]",
Short: "Manage groups",
Example: heredoc.Doc(`
$ asynq group list --queue=myqueue`),
}
var groupListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List groups",
Args: cobra.NoArgs,
RunE: groupLists,
}
func groupLists(cmd *cobra.Command, args []string) error {
qname, err := cmd.Flags().GetString("queue")
if err != nil {
return err
}
inspector := createInspector()
groups, err := inspector.Groups(qname)
if err != nil {
return fmt.Errorf("could not fetch groups: %v", err)
}
if len(groups) == 0 {
fmt.Printf("No groups found in queue %q\n", qname)
return nil
}
for _, g := range groups {
fmt.Println(g.Group)
}
return nil
}