mirror of
https://github.com/foomo/gocontentful.git
synced 2025-10-16 12:25:39 +00:00
Better file naming and documentation update
This commit is contained in:
parent
a3a0dbb013
commit
a42e8d8403
63
README.md
63
README.md
@ -1,16 +1,16 @@
|
||||
# gocontentful
|
||||
|
||||
A Contentful Entry-Reference Mapping code generator for Go. Initial features:
|
||||
A Contentful API code generator for Go. Initial features:
|
||||
|
||||
- Creation of Value Objects from Contentful content model
|
||||
- CDA, CPA, CMA support for CRUD operations
|
||||
- Automatic management/resolution of references
|
||||
- Automatic management/resolution of references and parents
|
||||
- Many utility functions, like converting to/from RichText to HTML and easy access to Assets
|
||||
|
||||
Rationale
|
||||
-----------------------------------------
|
||||
|
||||
While an unofficial/experimental Go client by Contentful has been available for a long time (see credits at the end of this document), working with the response of a JSON REST API is far from optimal.
|
||||
While an unofficial/experimental Go client by Contentful Labs has been available for a long time (see credits at the end of this document), working with the response of a JSON REST API is far from optimal.
|
||||
|
||||
Loading and converting data into (and from) Go value objects can quickly become a nightmare and the amount of code to access each field of each content type of a complex space can literally explode and if you're not careful how and when you interact with an API over the Internet, performance can be impacted dramatically. Not to mention what happens to your code if you need to make a significant change the space data model.
|
||||
|
||||
@ -23,33 +23,60 @@ How much code is that? As an example of a real-world scenario, a space content m
|
||||
Quickstart
|
||||
-----------------------------------------
|
||||
|
||||
Case: you want to generate a package named "people" and manipulate entries of content types with ID "person" and "pet". From the main directory run this:
|
||||
### Installation
|
||||
|
||||
<pre><code>export SPACEID=YOUR_SPACE_ID
|
||||
export CMAKEY=YOUR_CMA_KEY
|
||||
export PACKAGE=people
|
||||
export CONTENTTYPES=person,pet
|
||||
Prerequisite: you need Go 1.16+. Upgrade if you still haven't, then run:
|
||||
|
||||
make</code></pre>
|
||||
> go get github.com/foomo/gocontentful
|
||||
|
||||
Note: the Makefile requires _sed_ and _gofmt_ to be available in your shell path. This will generally be the case for Go developers and makes the generated files nice and tidy.
|
||||
Test the installation (make sure $GOPATH/bin is in your $PATH):
|
||||
|
||||
The script will scan the space, download locales and content types and generate some files in a directory named after the package inside the "generated" directory:
|
||||
>gocontentful
|
||||
|
||||
<pre><code>generated/people
|
||||
|-contentful_vo_base.go
|
||||
|-contentful_vo_lib_person.go // One file for each content type
|
||||
|-contentful_vo_lib_pet.go // One file for each content type
|
||||
|-contentful_vo_lib.go
|
||||
|-contentful_vo.go
|
||||
<pre><code>Contentful API Generator starting...
|
||||
|
||||
ERROR: Please specify the Contentful space ID and access Key
|
||||
|
||||
SYNOPSIS
|
||||
gocontentful -spaceid SpaceID -cmakey CMAKey [-contenttypes firsttype,secondtype...lasttype] path/to/target/package
|
||||
|
||||
Usage of /var/folders/hy/xggk1mb1409g93q1c_r14ltw0000gn/T/go-build1029582693/b001/exe/main:
|
||||
-cmakey string
|
||||
Contentful CMA key
|
||||
-contenttypes string
|
||||
[Optional] Content type IDs to parse, comma separated
|
||||
-spaceid string
|
||||
Contentful space ID
|
||||
|
||||
Note: The last segment of the path/to/target/package will be used as package name
|
||||
</code></pre>
|
||||
|
||||
Copy these files into a subdirectory of your project and import the "people" package.
|
||||
###Use case
|
||||
|
||||
- You want to generate a package named "people" and manipulate entries of content types with ID "person" and "pet".
|
||||
|
||||
Run the following:
|
||||
|
||||
>gocontentful -spaceid YOUR_SPACE_ID -cmakey YOUR_CMA_API_TOKEN -contenttypes person,pet path/to/your/go/project/folder/people
|
||||
|
||||
The -contenttypes parameter is optional. If not specified, an API for all content types of the space will be generated.
|
||||
|
||||
The script will scan the space, download locales and content types and generate the Go API files in the target path:
|
||||
|
||||
<pre><code>path/to/your/go/project/folder/people
|
||||
|-gocontentfulvobase.go
|
||||
|-gocontentfulvolib_person.go // One file for each content type
|
||||
|-gocontentfulvolib_pet.go // One file for each content type
|
||||
|-gocontentfulvolib.go
|
||||
|-gocontentfulvo.go
|
||||
</code></pre>
|
||||
|
||||
_Note: Do NOT modify these files! If you change the content model in Contentful you will need to run the generator again and overwrite the files._
|
||||
|
||||
### Get a client
|
||||
|
||||
The generated files will be in the "people" subdirectory of your project. Your go program can get a Contentful client from them:
|
||||
|
||||
`cc, err := people.NewContentfulClient(YOUR_SPACE_ID, people.ClientModeCDA, YOUR_API_KEY, 1000, contentfulLogger, people.LogDebug,false)`
|
||||
|
||||
The parameters to pass to NewContentfulClient are:
|
||||
|
||||
@ -52,9 +52,9 @@ func generate(filename string, tpl []byte, conf spaceConf) error {
|
||||
// generateCode generates API to and value objects for the space
|
||||
func generateCode(conf spaceConf) (err error) {
|
||||
for file, tpl := range map[string][]byte{
|
||||
filepath.Join(conf.PackageDir, "contentfulvobase"+goExt): templates.TemplateVoBase,
|
||||
filepath.Join(conf.PackageDir, "contentfulvo"+goExt): templates.TemplateVo,
|
||||
filepath.Join(conf.PackageDir, "contentfulvolib"+goExt): templates.TemplateVoLib,
|
||||
filepath.Join(conf.PackageDir, "gocontentfulvobase"+goExt): templates.TemplateVoBase,
|
||||
filepath.Join(conf.PackageDir, "gocontentfulvo"+goExt): templates.TemplateVo,
|
||||
filepath.Join(conf.PackageDir, "gocontentfulvolib"+goExt): templates.TemplateVoLib,
|
||||
} {
|
||||
errGenerate := generate(file, tpl, conf)
|
||||
if errGenerate != nil {
|
||||
@ -64,7 +64,7 @@ func generateCode(conf spaceConf) (err error) {
|
||||
for _, contentType := range conf.ContentTypes {
|
||||
conf.ContentType = contentType
|
||||
errGenerate := generate(
|
||||
filepath.Join(conf.PackageDir, "contentfulvolib"+strings.ToLower(contentType.Sys.ID)+goExt),
|
||||
filepath.Join(conf.PackageDir, "gocontentfulvolib"+strings.ToLower(contentType.Sys.ID)+goExt),
|
||||
templates.TemplateVoLibContentType,
|
||||
conf,
|
||||
)
|
||||
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/contentful"
|
||||
)
|
||||
@ -90,7 +91,11 @@ func getData(spaceID, cmaKey string, flagContentTypes []string) (finalContentTyp
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Filtered Content types:", len(finalContentTypes), finalContentTypes)
|
||||
var finalContentTypesString []string
|
||||
for _, finalContentType := range finalContentTypes {
|
||||
finalContentTypesString = append(finalContentTypesString, finalContentType.Name)
|
||||
}
|
||||
fmt.Println("Filtered Content types:", len(finalContentTypes), strings.Join(finalContentTypesString, ", "))
|
||||
return finalContentTypes, locales, nil
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/contentful-erm
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/gocontentful
|
||||
{{ $cfg := . }}{{ $contentTypes := .ContentTypes }}package {{ .PackageName }}
|
||||
|
||||
import "github.com/foomo/contentful"
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/contentful-erm
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/gocontentful
|
||||
{{ $cfg := . }}package {{ .PackageName }}
|
||||
|
||||
import "github.com/foomo/contentful"
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/contentful-erm
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/gocontentful
|
||||
{{ $cfg := . }}{{ $contentTypes := .ContentTypes }}{{ $locales := .Locales }}package {{ .PackageName }}
|
||||
|
||||
import (
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/contentful-erm
|
||||
// Package {{ .PackageName }} - DO NOT EDIT THIS FILE: Auto-generated code by https://github.com/foomo/gocontentful
|
||||
{{ $cfg := . }}{{ $contentTypes := .ContentTypes }}{{ $contentType := .ContentType }}package {{ .PackageName }}
|
||||
|
||||
import (
|
||||
|
||||
9
main.go
9
main.go
@ -12,8 +12,11 @@ import (
|
||||
)
|
||||
|
||||
func usageError(comment string) {
|
||||
fmt.Println(comment)
|
||||
fmt.Println("ERROR:", comment)
|
||||
fmt.Printf("\nSYNOPSIS\n")
|
||||
fmt.Printf(" gocontentful -spaceid SpaceID -cmakey CMAKey [-contenttypes firsttype,secondtype...lasttype] path/to/target/package\n\n")
|
||||
flag.Usage()
|
||||
fmt.Printf("\nNote: The last segment of the path/to/target/package will be used as package name\n\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -23,7 +26,7 @@ func fatal(infos ...interface{}) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Contentful Entry-Reference Mapping Generator starting...")
|
||||
fmt.Printf("Contentful API Generator starting...\n\n")
|
||||
// Get parameters from cmd line flags
|
||||
flagSpaceID := flag.String("spaceid", "", "Contentful space ID")
|
||||
flagCMAKey := flag.String("cmakey", "", "Contentful CMA key")
|
||||
@ -36,7 +39,7 @@ func main() {
|
||||
}
|
||||
|
||||
if len(flag.Args()) != 1 {
|
||||
usageError("missing arg path/to/target/package")
|
||||
usageError("Missing arg path/to/target/package")
|
||||
}
|
||||
|
||||
path := flag.Arg(0)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user