Skip to content

Commit e5c8040

Browse files
authored
Merge pull request #3 from joinflux/feature/collections
Add collections related subcommands
2 parents 944cbce + e776505 commit e5c8040

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ Usage:
1616
webflowctl [command]
1717
1818
Available Commands:
19+
collections Manage collections
1920
completion Generate the autocompletion script for the specified shell
2021
help Help about any command
22+
sites Manage sites
2123
webhooks Manage webhooks
2224
2325
Flags:
@@ -39,6 +41,8 @@ Use "webflowctl [command] --help" for more information about a command.
3941
- [x] Get site
4042
- [x] Publish site
4143
- [x] List site domains
44+
- [x] List collections
45+
- [x] Get a collection
4246

4347
## Development
4448

Diff for: cmd/collections.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"os"
8+
"reflect"
9+
"text/tabwriter"
10+
11+
"github.com/joinflux/webflowctl/internal"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func init() {
16+
collectionsCmd.AddCommand(listCollectionsCmd)
17+
collectionsCmd.AddCommand(getCollectionCmd)
18+
rootCmd.AddCommand(collectionsCmd)
19+
}
20+
21+
// collectionsCmd represents the webhooks command
22+
var collectionsCmd = &cobra.Command{
23+
Use: "collections",
24+
Short: "Manage collections",
25+
Long: "List and manage collections",
26+
}
27+
28+
type Collection struct {
29+
Id string `json:"_id"`
30+
LastUpdated string
31+
CreatedOn string
32+
Name string
33+
Slug string
34+
SingularName string
35+
}
36+
37+
// ListCollectionsResponse represents a response to the list collections request in Webflow.
38+
// See: https://developers.webflow.com/reference/list-collections
39+
type ListCollectionsResponse []Collection
40+
41+
// listCollectionsCmd represents the command to list all collections for a site in Webflow.
42+
var listCollectionsCmd = &cobra.Command{
43+
Use: "list [site_id]",
44+
Short: "list collections for a site",
45+
Args: cobra.ExactArgs(1),
46+
Run: func(cmd *cobra.Command, args []string) {
47+
siteId := args[0]
48+
49+
c := internal.NewClient(ApiToken)
50+
51+
body, err := c.Get([]string{"sites", siteId, "collections"})
52+
if err != nil {
53+
log.Fatalf("Request failed: %v", err)
54+
}
55+
56+
var response ListCollectionsResponse
57+
err = json.Unmarshal(body, &response)
58+
if err != nil {
59+
log.Fatalf("Failed to unmarshal response body: %v", err)
60+
}
61+
62+
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
63+
for _, collection := range response {
64+
v := reflect.ValueOf(collection)
65+
for i := 0; i < v.NumField(); i++ {
66+
name := v.Type().Field(i).Name
67+
value := v.Field(i).Interface()
68+
fmt.Fprintf(w, "%s:\t%s\n", name, value)
69+
}
70+
fmt.Fprint(w, "\n")
71+
}
72+
w.Flush()
73+
},
74+
}
75+
76+
// GetCollectionResponse represents a response to the get collection request in Webflow.
77+
// See: https://developers.webflow.com/reference/get-collection
78+
type GetCollectionResponse struct {
79+
*Collection
80+
Fields []struct {
81+
Id string
82+
Slug string
83+
Name string
84+
Archived bool `json:"_archived"`
85+
Draft bool `json:"_draft"`
86+
Editable bool
87+
Required bool
88+
}
89+
}
90+
91+
// getCollectionCmd represents the command to get detailed info on a collection in Webflow
92+
var getCollectionCmd = &cobra.Command{
93+
Use: "get [collection_id]",
94+
Short: "get detailed information for a collection",
95+
Args: cobra.ExactArgs(1),
96+
Run: func(cmd *cobra.Command, args []string) {
97+
collectionId := args[0]
98+
99+
c := internal.NewClient(ApiToken)
100+
101+
body, err := c.Get([]string{"collections", collectionId})
102+
if err != nil {
103+
log.Fatalf("Request failed: %v", err)
104+
}
105+
106+
var response GetCollectionResponse
107+
err = json.Unmarshal(body, &response)
108+
if err != nil {
109+
log.Fatalf("Failed to unmarshal response body: %v", err)
110+
}
111+
112+
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
113+
fmt.Fprintf(w, "id:\t%s\n", response.Id)
114+
fmt.Fprintf(w, "name:\t%s\n", response.Name)
115+
fmt.Fprintf(w, "slug:\t%s\n", response.Slug)
116+
fmt.Fprintf(w, "singular name:\t%v\n", response.SingularName)
117+
fmt.Fprintf(w, "crated on:\t%v\n", response.CreatedOn)
118+
fmt.Fprintf(w, "last updated:\t%v\n", response.LastUpdated)
119+
fmt.Fprint(w, "\n\nFields:\n")
120+
for _, val := range response.Fields {
121+
fmt.Fprintf(w, "id:\t%s\n", val.Id)
122+
fmt.Fprintf(w, "name:\t%s\n", val.Name)
123+
fmt.Fprintf(w, "slug:\t%s\n", val.Slug)
124+
fmt.Fprintf(w, "draft:\t%v\n", val.Draft)
125+
fmt.Fprintf(w, "archived:\t%v\n", val.Archived)
126+
fmt.Fprintf(w, "editable:\t%v\n", val.Editable)
127+
fmt.Fprintf(w, "required:\t%v\n", val.Required)
128+
fmt.Fprint(w, "\n")
129+
}
130+
fmt.Fprint(w, "\n")
131+
w.Flush()
132+
},
133+
}

0 commit comments

Comments
 (0)