|
| 1 | +package themes |
| 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 InstallCmd = &cobra.Command{ |
| 16 | + Use: "install <username> <software>", |
| 17 | + Short: "Install WordPress theme", |
| 18 | + Long: "Install a theme on an existing WordPress installation.\n\nProvide the WordPress installation (software) identifier in the path. It can\nbe obtained from GET /api/hosting/v1/wordpress/installations (the `id`\nfield).\n\nWhen the theme is one of the Hostinger themes (hostinger-blog,\nhostinger-affiliate-theme, hostinger-ai-theme), the optional `palette`,\n`layout`, and `font` fields are forwarded to the custom installer (defaults:\npalette1, layout1, default). For any other theme they are ignored.\n\nThis operation is asynchronous: a successful response only means the install\njob has been queued, not that the theme is ready.", |
| 19 | + Args: cobra.MatchAll(cobra.ExactArgs(2)), |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + utils.EnumCheck(cmd, "font", []string{"professional", "modern", "elegant", "creative", "dynamic", "default"}) |
| 22 | + payload, err := json.Marshal(installBody(cmd)) |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + r, err := api.Request().HostingInstallWordPressThemeV1WithBodyWithResponse(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 | + InstallCmd.Flags().StringP("font", "", "default", "Font identifier. Only applied when the theme is a Hostinger theme; the default is used when omitted. (one of: professional, modern, elegant, creative, dynamic, default)") |
| 37 | + InstallCmd.Flags().StringP("layout", "", "layout1", "Layout identifier. Only applied when the theme is a Hostinger theme; the default is used when omitted.") |
| 38 | + InstallCmd.Flags().StringP("palette", "", "palette1", "Palette identifier. Only applied when the theme is a Hostinger theme; the default is used when omitted.") |
| 39 | + InstallCmd.Flags().StringP("theme", "", "", "Slug of the theme to install. Hostinger theme slugs (hostinger-blog, hostinger-affiliate-theme, hostinger-ai-theme) trigger the custom installer and forward the optional palette/layout/font fields; any other WordPress theme slug uses the standard installer and ignores those fields.") |
| 40 | + InstallCmd.MarkFlagRequired("theme") |
| 41 | +} |
| 42 | + |
| 43 | +func installBody(cmd *cobra.Command) map[string]any { |
| 44 | + body := map[string]any{} |
| 45 | + if cmd.Flags().Changed("font") { |
| 46 | + v, _ := cmd.Flags().GetString("font") |
| 47 | + body["font"] = v |
| 48 | + } |
| 49 | + if cmd.Flags().Changed("layout") { |
| 50 | + v, _ := cmd.Flags().GetString("layout") |
| 51 | + body["layout"] = v |
| 52 | + } |
| 53 | + if cmd.Flags().Changed("palette") { |
| 54 | + v, _ := cmd.Flags().GetString("palette") |
| 55 | + body["palette"] = v |
| 56 | + } |
| 57 | + themeVal, _ := cmd.Flags().GetString("theme") |
| 58 | + body["theme"] = themeVal |
| 59 | + return body |
| 60 | +} |
0 commit comments