|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" |
| 8 | + "github.com/metal-stack/cli/cmd/config" |
| 9 | + "github.com/metal-stack/metal-lib/pkg/genericcli" |
| 10 | + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" |
| 11 | + "github.com/metal-stack/metal-lib/pkg/pointer" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/viper" |
| 14 | +) |
| 15 | + |
| 16 | +type task struct { |
| 17 | + c *config.Config |
| 18 | +} |
| 19 | + |
| 20 | +func newTaskCmd(c *config.Config) *cobra.Command { |
| 21 | + w := &task{ |
| 22 | + c: c, |
| 23 | + } |
| 24 | + |
| 25 | + cmdsConfig := &genericcli.CmdsConfig[any, any, *adminv2.TaskInfo]{ |
| 26 | + BinaryName: config.BinaryName, |
| 27 | + GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs), |
| 28 | + Singular: "task", |
| 29 | + Plural: "tasks", |
| 30 | + Description: "get task insights", |
| 31 | + DescribePrinter: func() printers.Printer { return c.DescribePrinter }, |
| 32 | + ListPrinter: func() printers.Printer { return c.ListPrinter }, |
| 33 | + DescribeCmdMutateFn: func(cmd *cobra.Command) { |
| 34 | + cmd.Flags().String("queue", "default", "the queue for which tasks should be described") |
| 35 | + }, |
| 36 | + ListCmdMutateFn: func(cmd *cobra.Command) { |
| 37 | + cmd.Flags().String("queue", "", "the queue for which tasks should be listed") |
| 38 | + }, |
| 39 | + DeleteCmdMutateFn: func(cmd *cobra.Command) { |
| 40 | + cmd.Flags().String("queue", "default", "the queue of the task which should be delete") |
| 41 | + }, |
| 42 | + OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.DescribeCmd, genericcli.DeleteCmd), |
| 43 | + } |
| 44 | + |
| 45 | + queueCmd := &cobra.Command{ |
| 46 | + Use: "queues", |
| 47 | + Short: "list all queues", |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + return w.queues() |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + return genericcli.NewCmds(cmdsConfig, queueCmd) |
| 54 | +} |
| 55 | + |
| 56 | +func (t *task) queues() error { |
| 57 | + ctx, cancel := t.c.NewRequestContext() |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + req := &adminv2.TaskServiceQueuesRequest{} |
| 61 | + |
| 62 | + resp, err := t.c.Client.Adminv2().Task().Queues(ctx, req) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to get task queues: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + _, _ = fmt.Fprint(t.c.Out, strings.Join(resp.Queues, "\n")) |
| 68 | + _, _ = fmt.Fprint(t.c.Out, "\n") |
| 69 | + return err |
| 70 | +} |
| 71 | + |
| 72 | +func (t *task) Get(id string) (*adminv2.TaskInfo, error) { |
| 73 | + ctx, cancel := t.c.NewRequestContext() |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + req := &adminv2.TaskServiceGetRequest{TaskId: id, Queue: viper.GetString("queue")} |
| 77 | + |
| 78 | + resp, err := t.c.Client.Adminv2().Task().Get(ctx, req) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("failed to get task: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + return resp.Task, nil |
| 84 | +} |
| 85 | +func (t *task) List() ([]*adminv2.TaskInfo, error) { |
| 86 | + ctx, cancel := t.c.NewRequestContext() |
| 87 | + defer cancel() |
| 88 | + |
| 89 | + req := &adminv2.TaskServiceListRequest{} |
| 90 | + if viper.IsSet("queue") { |
| 91 | + req.Queue = pointer.Pointer(viper.GetString("queue")) |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := t.c.Client.Adminv2().Task().List(ctx, req) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("failed to list tasks: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + return resp.Tasks, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (t *task) Create(rq any) (*adminv2.TaskInfo, error) { |
| 103 | + panic("unimplemented") |
| 104 | +} |
| 105 | + |
| 106 | +func (t *task) Delete(id string) (*adminv2.TaskInfo, error) { |
| 107 | + ctx, cancel := t.c.NewRequestContext() |
| 108 | + defer cancel() |
| 109 | + |
| 110 | + req := &adminv2.TaskServiceDeleteRequest{TaskId: id, Queue: viper.GetString("queue")} |
| 111 | + |
| 112 | + _, err := t.c.Client.Adminv2().Task().Delete(ctx, req) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("failed to get task: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + return nil, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (t *task) Convert(r *adminv2.TaskInfo) (string, any, any, error) { |
| 121 | + panic("unimplemented") |
| 122 | +} |
| 123 | + |
| 124 | +func (t *task) Update(rq any) (*adminv2.TaskInfo, error) { |
| 125 | + panic("unimplemented") |
| 126 | +} |
0 commit comments