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