From 6a3be467c5dc51cb0992d1570cd0acd2c4e3c628 Mon Sep 17 00:00:00 2001 From: Cristian Vidmar Date: Thu, 8 Jul 2021 13:16:06 +0200 Subject: [PATCH] cache all content types by default, CLI improvements --- erm/templates/contentful_vo_lib.gotmpl | 14 ++++++++----- main.go | 28 +++++++++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/erm/templates/contentful_vo_lib.gotmpl b/erm/templates/contentful_vo_lib.gotmpl index 357324b..6b5f8b5 100644 --- a/erm/templates/contentful_vo_lib.gotmpl +++ b/erm/templates/contentful_vo_lib.gotmpl @@ -362,16 +362,20 @@ func RichTextToHtml(rt interface{}, linkResolver LinkResolverFunc, entryLinkReso func (cc *ContentfulClient) UpdateCache(ctx context.Context, contentTypes []string, cacheAssets bool) error { start := time.Now() + if contentTypes == nil { + contentTypes = spaceContentTypes + } else { + for _, contentType := range contentTypes { + if !stringSliceContains(spaceContentTypes, contentType) { + return fmt.Errorf("UpdateCache: Content Type %q not available in this space", contentType) + } + } + } tempCache := &ContentfulCache{ contentTypes: contentTypes, idContentTypeMap: map[string]string{}, parentMap: map[string][]EntryReference{}, } - for _, contentType := range contentTypes { - if !stringSliceContains(spaceContentTypes, contentType) { - return fmt.Errorf("UpdateCache: Content Type %q not available in this space", contentType) - } - } if cacheAssets { contentTypes = append([]string{assetWorkerType}, contentTypes...) } diff --git a/main.go b/main.go index b1e8e3d..7d7e58a 100644 --- a/main.go +++ b/main.go @@ -11,12 +11,18 @@ import ( "github.com/foomo/gocontentful/erm" ) -func usageError(comment string) { - fmt.Println("ERROR:", comment) +const VERSION = "v0.9.5" + +var Usage = func() { fmt.Printf("\nSYNOPSIS\n") fmt.Printf(" gocontentful -spaceid SpaceID -cmakey CMAKey [-contenttypes firsttype,secondtype...lasttype] path/to/target/package\n\n") - flag.Usage() + flag.PrintDefaults() fmt.Printf("\nNote: The last segment of the path/to/target/package will be used as package name\n\n") +} + +func usageError(comment string) { + fmt.Println("ERROR:", comment) + Usage() os.Exit(1) } @@ -26,14 +32,24 @@ func fatal(infos ...interface{}) { } func main() { - 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") flagContentTypes := flag.String("contenttypes", "", "[Optional] Content type IDs to parse, comma separated") - + flagVersion := flag.Bool("version", false, "Print version and exit") + flagHelp := flag.Bool("help", false, "Print version and exit") flag.Parse() + if *flagVersion { + fmt.Println(VERSION) + os.Exit(0) + } + + if *flagHelp { + Usage() + os.Exit(0) + } + if *flagSpaceID == "" || *flagCMAKey == "" { usageError("Please specify the Contentful space ID and access Key") } @@ -50,6 +66,8 @@ func main() { usageError("Please specify the package name correctly (only small caps letters)") } + fmt.Printf("Contentful API Generator starting...\n\n") + var flagContentTypesSlice []string if *flagContentTypes != "" { for _, contentType := range strings.Split(*flagContentTypes, ",") {