|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package scheduler |
| 15 | + |
| 16 | +import ( |
| 17 | + "os" |
| 18 | + |
| 19 | + "github.com/gocarina/gocsv" |
| 20 | + "github.com/spf13/cobra" |
| 21 | + |
| 22 | + "github.com/dapr/cli/pkg/scheduler" |
| 23 | + "github.com/dapr/cli/utils" |
| 24 | + "github.com/dapr/kit/signals" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + getOutputFormat *string |
| 29 | +) |
| 30 | + |
| 31 | +var GetCmd = &cobra.Command{ |
| 32 | + Use: "get", |
| 33 | + Aliases: []string{"g", "ge"}, |
| 34 | + Short: `Get a scheduled app job or actor reminder in Scheduler. |
| 35 | +Job names are formatted by their type, app ID, then identifier. |
| 36 | +Actor reminders require the actor type, actor ID, then reminder name, separated by /. |
| 37 | +Workflow reminders require the app ID, instance ID, then reminder name, separated by /. |
| 38 | +Activity reminders require the app ID, activity ID, separated by /. |
| 39 | +Accepts multiple names. |
| 40 | +`, |
| 41 | + Args: cobra.MinimumNArgs(1), |
| 42 | + Example: ` |
| 43 | +dapr scheduler get app/my-app-id/my-job-name |
| 44 | +dapr scheduler get actor/my-actor-type/my-actor-id/my-reminder-name |
| 45 | +dapr scheduler get workflow/my-app-id/my-instance-id/my-workflow-reminder-name |
| 46 | +dapr scheduler get activity/my-app-id/xyz::0::1 |
| 47 | +`, |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + ctx := signals.Context() |
| 50 | + opts := scheduler.GetOptions{ |
| 51 | + SchedulerNamespace: schedulerNamespace, |
| 52 | + KubernetesMode: kubernetesMode, |
| 53 | + DaprNamespace: daprNamespace, |
| 54 | + } |
| 55 | + |
| 56 | + var list any |
| 57 | + var err error |
| 58 | + if *getOutputFormat == outputFormatShort { |
| 59 | + list, err = scheduler.Get(ctx, opts, args...) |
| 60 | + } else { |
| 61 | + list, err = scheduler.GetWide(ctx, opts, args...) |
| 62 | + } |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + switch *getOutputFormat { |
| 68 | + case outputFormatYAML: |
| 69 | + err = utils.PrintDetail(os.Stdout, "yaml", list) |
| 70 | + case outputFormatJSON: |
| 71 | + err = utils.PrintDetail(os.Stdout, "json", list) |
| 72 | + default: |
| 73 | + var table string |
| 74 | + table, err = gocsv.MarshalString(list) |
| 75 | + if err != nil { |
| 76 | + break |
| 77 | + } |
| 78 | + |
| 79 | + utils.PrintTable(table) |
| 80 | + } |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +func init() { |
| 91 | + getOutputFormat = outputFunc(GetCmd) |
| 92 | + SchedulerCmd.AddCommand(GetCmd) |
| 93 | +} |
0 commit comments