|
| 1 | +package cron_jobs |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "log" |
| 8 | + |
| 9 | + "github.com/hostinger/api-cli/api" |
| 10 | + "github.com/hostinger/api-cli/output" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var CreatePlanWebsiteCmd = &cobra.Command{ |
| 15 | + Use: "create-plan-website <website_uid>", |
| 16 | + Short: "Create Agency Plan website cron job", |
| 17 | + Long: "Creates a cron job for an Agency Plan website from a schedule expression and a command.\n\nReturns the created cron job, including its uuid, which is required to delete the cron job.", |
| 18 | + Args: cobra.MatchAll(cobra.ExactArgs(1)), |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + payload, err := json.Marshal(createPlanWebsiteBody(cmd)) |
| 21 | + if err != nil { |
| 22 | + log.Fatal(err) |
| 23 | + } |
| 24 | + r, err := api.Request().AgencyHostingCreateAgencyPlanWebsiteCronJobV1WithBodyWithResponse(context.TODO(), args[0], "application/json", bytes.NewReader(payload)) |
| 25 | + if err != nil { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +func init() { |
| 34 | + CreatePlanWebsiteCmd.Flags().StringP("command", "", "", "Command to run on the schedule. Must not contain pipe (|) or redirection (<, >) characters.") |
| 35 | + CreatePlanWebsiteCmd.Flags().StringP("time", "", "", "Cron schedule expression (standard 5-field crontab syntax).") |
| 36 | + CreatePlanWebsiteCmd.MarkFlagRequired("command") |
| 37 | + CreatePlanWebsiteCmd.MarkFlagRequired("time") |
| 38 | +} |
| 39 | + |
| 40 | +func createPlanWebsiteBody(cmd *cobra.Command) map[string]any { |
| 41 | + body := map[string]any{} |
| 42 | + commandVal, _ := cmd.Flags().GetString("command") |
| 43 | + body["command"] = commandVal |
| 44 | + timeVal, _ := cmd.Flags().GetString("time") |
| 45 | + body["time"] = timeVal |
| 46 | + return body |
| 47 | +} |
0 commit comments