|
| 1 | +package websites |
| 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 StartSetupCmd = &cobra.Command{ |
| 16 | + Use: "start-setup <order_id>", |
| 17 | + Short: "Start website setup", |
| 18 | + Long: "Starts a website setup on a Web or Cloud hosting order and returns the created setup\nright away; the website itself is provisioned asynchronously. Poll the list website\nsetups endpoint with the `domain` filter every 10 to 15 seconds and wait for\n`status: completed` before uploading files, deploying or creating databases.\n\nOmit `type` for an empty website. `type: wordpress` installs WordPress in the website\nroot with the admin user, email and password from `wordpress`, the domain as the site\ntitle, and `en_US` when `wordpress.language` is omitted. The headless types\n(`headless_wordpress`, `headless_ecommerce`, `headless_pocketbase`) create a headless\nwebsite; `headless_wordpress` additionally installs WordPress into the `cms` directory\nof the website root with generated credentials.\n\nOmit `domain` to set the website up on a generated temporary free subdomain.\n\nThe order must already have a hosting account: to create the first website on a new\nhosting plan use the create website endpoint, which takes the `datacenter_code`.\nReturns 404 when the order does not exist or is not accessible to the authenticated\nclient, and 409 with a `Retry-After` header while a setup for the same domain is still\nrunning.", |
| 19 | + Args: cobra.MatchAll(cobra.ExactArgs(1)), |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + utils.EnumCheck(cmd, "type", []string{"wordpress", "headless_wordpress", "headless_ecommerce", "headless_pocketbase"}) |
| 22 | + payload, err := json.Marshal(startSetupBody(cmd)) |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + r, err := api.Request().HostingStartWebsiteSetupV1WithBodyWithResponse(context.TODO(), utils.StringToInt(args[0]), "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 | + StartSetupCmd.Flags().StringP("domain", "", "", "Customer-owned domain. Cannot start with \"www.\". Omit or `null` to set the website up on a generated temporary free subdomain.") |
| 37 | + StartSetupCmd.Flags().StringP("type", "", "", "Website type. Omit or `null` for an empty website. `wordpress` installs WordPress in the website root and requires `wordpress`. The headless types (`headless_wordpress`, `headless_ecommerce`, `headless_pocketbase`) create a headless website; `headless_wordpress` additionally installs WordPress into the `cms` directory with generated credentials. (one of: wordpress, headless_wordpress, headless_ecommerce, headless_pocketbase)") |
| 38 | + StartSetupCmd.Flags().StringP("wordpress", "", "", "WordPress install settings. Required when `type` is `wordpress`, not allowed otherwise. The site title is the domain. (JSON)") |
| 39 | +} |
| 40 | + |
| 41 | +func startSetupBody(cmd *cobra.Command) map[string]any { |
| 42 | + body := map[string]any{} |
| 43 | + if cmd.Flags().Changed("domain") { |
| 44 | + v, _ := cmd.Flags().GetString("domain") |
| 45 | + body["domain"] = v |
| 46 | + } |
| 47 | + if cmd.Flags().Changed("type") { |
| 48 | + v, _ := cmd.Flags().GetString("type") |
| 49 | + body["type"] = v |
| 50 | + } |
| 51 | + if cmd.Flags().Changed("wordpress") { |
| 52 | + v, _ := cmd.Flags().GetString("wordpress") |
| 53 | + body["wordpress"] = utils.JSONValue(v, "wordpress") |
| 54 | + } |
| 55 | + return body |
| 56 | +} |
0 commit comments