|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/joinflux/webflowctl/internal" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + sitesCmd.AddCommand(getSitesCmd) |
| 16 | + sitesCmd.AddCommand(listDomainsCmd) |
| 17 | + sitesCmd.AddCommand(listSitesCmd) |
| 18 | + sitesCmd.AddCommand(publishSitesCmd) |
| 19 | + rootCmd.AddCommand(sitesCmd) |
| 20 | +} |
| 21 | + |
| 22 | +// sitesCmd represents the sites command |
| 23 | +var sitesCmd = &cobra.Command{ |
| 24 | + Use: "sites", |
| 25 | + Short: "Manage sites", |
| 26 | + Long: `List, create, delete and manage sites.`, |
| 27 | +} |
| 28 | + |
| 29 | +type Site struct { |
| 30 | + CreatedOn string |
| 31 | + Id string `json:"_id"` |
| 32 | + LastPublished string |
| 33 | + Name string |
| 34 | + PreviewUrl string |
| 35 | + ShortName string |
| 36 | + Timezone string |
| 37 | +} |
| 38 | + |
| 39 | +// ListSitesResponse represents a response to the list sites request in Webflow. |
| 40 | +// See: https://developers.webflow.com/reference/list-sites |
| 41 | +type ListSitesResponse []Site |
| 42 | + |
| 43 | +// listSitesCmd represents the command to list sites in Webflow. |
| 44 | +var listSitesCmd = &cobra.Command{ |
| 45 | + Use: "list", |
| 46 | + Short: "list sites", |
| 47 | + Run: func(cmd *cobra.Command, args []string) { |
| 48 | + c := internal.NewClient(ApiToken) |
| 49 | + body, err := c.Get([]string{"sites"}) |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("Request failed: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + var response ListSitesResponse |
| 55 | + err = json.Unmarshal(body, &response) |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("Failed to unmarshal response body: %v", err) |
| 58 | + } |
| 59 | + for _, site := range response { |
| 60 | + fmt.Printf("%s\t%s\t%s\t%s\n", site.Name, site.Id, site.LastPublished, site.PreviewUrl) |
| 61 | + } |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +// GetSitesResponse represents a response to the get site request in Webflow. |
| 66 | +// See: https://developers.webflow.com/reference/get-site |
| 67 | +type GetSitesResponse Site |
| 68 | + |
| 69 | +// getSitesCmd represents the command to get a site in Webflow. |
| 70 | +var getSitesCmd = &cobra.Command{ |
| 71 | + Use: "get [site_id]", |
| 72 | + Short: "get a site", |
| 73 | + Args: cobra.ExactArgs(1), |
| 74 | + Run: func(cmd *cobra.Command, args []string) { |
| 75 | + siteId := args[0] |
| 76 | + c := internal.NewClient(ApiToken) |
| 77 | + body, err := c.Get([]string{"sites", siteId}) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("Request failed: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + var response GetSitesResponse |
| 83 | + err = json.Unmarshal(body, &response) |
| 84 | + if err != nil { |
| 85 | + log.Fatalf("Failed to unmarshal response body: %v", err) |
| 86 | + } |
| 87 | + w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) |
| 88 | + fmt.Fprintf(w, "name:\t%s\n", response.Name) |
| 89 | + fmt.Fprintf(w, "id:\t%s\n", response.Id) |
| 90 | + fmt.Fprintf(w, "created on:\t%s\n", response.CreatedOn) |
| 91 | + fmt.Fprintf(w, "last published:\t%s\n", response.LastPublished) |
| 92 | + fmt.Fprintf(w, "preview url:\t%s\n", response.PreviewUrl) |
| 93 | + fmt.Fprintf(w, "short name:\t%s\n", response.ShortName) |
| 94 | + fmt.Fprintf(w, "timezone:\t%s\n", response.Timezone) |
| 95 | + w.Flush() |
| 96 | + }, |
| 97 | +} |
| 98 | + |
| 99 | +// PublishSiteResponse represents a response to the publish site request in Webflow. |
| 100 | +// See: https://developers.webflow.com/reference/publish-site |
| 101 | +type PublishSiteResponse struct { |
| 102 | + Queued bool |
| 103 | +} |
| 104 | + |
| 105 | +// publishSitesCmd represents the command to publish a site in Webflow. |
| 106 | +var publishSitesCmd = &cobra.Command{ |
| 107 | + Use: "publish [site_id]", |
| 108 | + Short: "publish a site", |
| 109 | + Args: cobra.ExactArgs(1), |
| 110 | + Run: func(cmd *cobra.Command, args []string) { |
| 111 | + siteId := args[0] |
| 112 | + c := internal.NewClient(ApiToken) |
| 113 | + body, err := c.Get([]string{"sites", siteId, "publish"}) |
| 114 | + if err != nil { |
| 115 | + log.Fatalf("Request failed: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + var response PublishSiteResponse |
| 119 | + err = json.Unmarshal(body, &response) |
| 120 | + if err != nil { |
| 121 | + log.Fatalf("Failed to unmarshal response body: %v", err) |
| 122 | + } |
| 123 | + }, |
| 124 | +} |
| 125 | + |
| 126 | +type Domain struct { |
| 127 | + Id string `json:"_id"` |
| 128 | + Name string |
| 129 | +} |
| 130 | + |
| 131 | +// ListDomainsResponse represents a response to the list domains request in Webflow. |
| 132 | +// See: https://developers.webflow.com/reference/list-domains |
| 133 | +type ListDomainsResponse []Domain |
| 134 | + |
| 135 | +// listDomainsCmd represents the command to list the domains for a site. |
| 136 | +var listDomainsCmd = &cobra.Command{ |
| 137 | + Use: "domains [site_id]", |
| 138 | + Short: "list a site's domains", |
| 139 | + Args: cobra.ExactArgs(1), |
| 140 | + Run: func(cmd *cobra.Command, args []string) { |
| 141 | + siteId := args[0] |
| 142 | + c := internal.NewClient(ApiToken) |
| 143 | + body, err := c.Get([]string{"sites", siteId, "domains"}) |
| 144 | + if err != nil { |
| 145 | + log.Fatalf("Request failed: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + var response ListDomainsResponse |
| 149 | + err = json.Unmarshal(body, &response) |
| 150 | + if err != nil { |
| 151 | + log.Fatalf("Failed to unmarshal response body: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + for _, domain := range response { |
| 155 | + fmt.Println(domain.Name) |
| 156 | + } |
| 157 | + }, |
| 158 | +} |
0 commit comments