Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collections related subcommands #3

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Usage:
webflowctl [command]

Available Commands:
collections Manage collections
completion Generate the autocompletion script for the specified shell
help Help about any command
sites Manage sites
webhooks Manage webhooks

Flags:
Expand All @@ -39,6 +41,8 @@ Use "webflowctl [command] --help" for more information about a command.
- [x] Get site
- [x] Publish site
- [x] List site domains
- [x] List collections
- [x] Get a collection

## Development

Expand Down
133 changes: 133 additions & 0 deletions cmd/collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package cmd

import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"text/tabwriter"

"github.com/joinflux/webflowctl/internal"
"github.com/spf13/cobra"
)

func init() {
collectionsCmd.AddCommand(listCollectionsCmd)
collectionsCmd.AddCommand(getCollectionCmd)
rootCmd.AddCommand(collectionsCmd)
}

// collectionsCmd represents the webhooks command
var collectionsCmd = &cobra.Command{
Use: "collections",
Short: "Manage collections",
Long: "List and manage collections",
}

type Collection struct {
Id string `json:"_id"`
LastUpdated string
CreatedOn string
Name string
Slug string
SingularName string
}

// ListCollectionsResponse represents a response to the list collections request in Webflow.
// See: https://developers.webflow.com/reference/list-collections
type ListCollectionsResponse []Collection

// listCollectionsCmd represents the command to list all collections for a site in Webflow.
var listCollectionsCmd = &cobra.Command{
Use: "list [site_id]",
Short: "list collections for a site",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
siteId := args[0]

c := internal.NewClient(ApiToken)

body, err := c.Get([]string{"sites", siteId, "collections"})
if err != nil {
log.Fatalf("Request failed: %v", err)
}

var response ListCollectionsResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatalf("Failed to unmarshal response body: %v", err)
}

w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
for _, collection := range response {
v := reflect.ValueOf(collection)
for i := 0; i < v.NumField(); i++ {
name := v.Type().Field(i).Name
value := v.Field(i).Interface()
fmt.Fprintf(w, "%s:\t%s\n", name, value)
}
fmt.Fprint(w, "\n")
}
w.Flush()
},
}

// GetCollectionResponse represents a response to the get collection request in Webflow.
// See: https://developers.webflow.com/reference/get-collection
type GetCollectionResponse struct {
*Collection
Fields []struct {
Id string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need the json:"_id" annotation?

Related q: what's the purpose of these annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need the annotation because the field is just called id. Go maps the struct property to the json field if they match but sometimes they don't (or can't like in this case) cause the field starts with _id and this can't be an exported struct property so the annotation in the other examples is mapping struct.id to the _id json property.

Slug string
Name string
Archived bool `json:"_archived"`
Draft bool `json:"_draft"`
Editable bool
Required bool
}
}

// getCollectionCmd represents the command to get detailed info on a collection in Webflow
var getCollectionCmd = &cobra.Command{
Use: "get [collection_id]",
Short: "get details information for a collection",
gugahoi marked this conversation as resolved.
Show resolved Hide resolved
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
collectionId := args[0]

c := internal.NewClient(ApiToken)

body, err := c.Get([]string{"collections", collectionId})
if err != nil {
log.Fatalf("Request failed: %v", err)
}

var response GetCollectionResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatalf("Failed to unmarshal response body: %v", err)
}

w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "id:\t%s\n", response.Id)
fmt.Fprintf(w, "name:\t%s\n", response.Name)
fmt.Fprintf(w, "slug:\t%s\n", response.Slug)
fmt.Fprintf(w, "singular name:\t%v\n", response.SingularName)
fmt.Fprintf(w, "crated on:\t%v\n", response.CreatedOn)
fmt.Fprintf(w, "last updated:\t%v\n", response.LastUpdated)
fmt.Fprint(w, "\n\nFields:\n")
for _, val := range response.Fields {
fmt.Fprintf(w, "id:\t%s\n", val.Id)
fmt.Fprintf(w, "name:\t%s\n", val.Name)
fmt.Fprintf(w, "slug:\t%s\n", val.Slug)
fmt.Fprintf(w, "draft:\t%v\n", val.Draft)
fmt.Fprintf(w, "archived:\t%v\n", val.Archived)
fmt.Fprintf(w, "editable:\t%v\n", val.Editable)
fmt.Fprintf(w, "required:\t%v\n", val.Required)
fmt.Fprint(w, "\n")
}
fmt.Fprint(w, "\n")
w.Flush()
},
}