-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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() | ||
}, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 mappingstruct.id
to the_id
json property.