|
| 1 | +package forms |
| 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/hostinger/api-cli/utils" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var UpdateCmd = &cobra.Command{ |
| 16 | + Use: "update <profile-uuid> <form-uuid>", |
| 17 | + Short: "Update form", |
| 18 | + Long: "Rename a form and/or change its status.\n\nSetting a `draft` or `paused` form to `active` takes it live: its latest saved design is published\nand visitors can submit it. `paused` and `draft` stop the form from accepting submissions. A form is\nnot reachable by visitors until it is taken live. Design changes saved while the form is already\n`active` stay unpublished until the form is paused and set to `active` again. Renaming does not rename\nthe automatic `form:{name}` tag.", |
| 19 | + Args: cobra.MatchAll(cobra.ExactArgs(2)), |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + utils.EnumCheck(cmd, "status", []string{"active", "paused", "draft"}) |
| 22 | + payload, err := json.Marshal(updateBody(cmd)) |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + r, err := api.Request().ReachUpdateFormV1WithBodyWithResponse(context.TODO(), args[0], args[1], "application/json", bytes.NewReader(payload)) |
| 27 | + if err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +func init() { |
| 36 | + UpdateCmd.Flags().StringP("name", "", "", "Form name. Letters, digits, spaces and - . ' _ only.") |
| 37 | + UpdateCmd.Flags().StringP("status", "", "", "`active` takes the form live, `paused` and `draft` stop it accepting submissions. (one of: active, paused, draft)") |
| 38 | +} |
| 39 | + |
| 40 | +func updateBody(cmd *cobra.Command) map[string]any { |
| 41 | + body := map[string]any{} |
| 42 | + if cmd.Flags().Changed("name") { |
| 43 | + v, _ := cmd.Flags().GetString("name") |
| 44 | + body["name"] = v |
| 45 | + } |
| 46 | + if cmd.Flags().Changed("status") { |
| 47 | + v, _ := cmd.Flags().GetString("status") |
| 48 | + body["status"] = v |
| 49 | + } |
| 50 | + return body |
| 51 | +} |
0 commit comments