Skip to content

Commit

Permalink
feat: load run config from YAML file
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Vidmar committed Jul 6, 2023
1 parent 8d426d6 commit 2dcd1a3
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 20 deletions.
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

import (
"os"

"gopkg.in/yaml.v3"
)

func LoadConfigFromYAML(filename string) (*Config, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
config := &Config{}
err = yaml.Unmarshal(bytes, config)
if err != nil {
return nil, err
}
return config, nil
}
16 changes: 16 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestConfigFromYAML(t *testing.T) {
config, err := LoadConfigFromYAML("./sampleconfig.yaml")
require.NoError(t, err)
require.Equal(t, "abc123", config.SpaceID)
require.Equal(t, "dev", config.Environment)
require.Equal(t, "v1.0.19", config.RequireVersion)
require.Equal(t, 2, len(config.ContentTypes))
}
7 changes: 7 additions & 0 deletions config/sampleconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spaceId: "abc123"
environment: "dev"
exportFile: "foo.json"
requireVersion: v1.0.19
contentTypes:
- person
- pet
10 changes: 10 additions & 0 deletions config/vo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

type Config struct {
SpaceID string `yaml:"spaceId,omitempty"`
Environment string `yaml:"environment,omitempty"`
ExportFile string `yaml:"exportFile,omitempty"`
ContentTypes []string `yaml:"contentTypes,omitempty"`
PathTargetPackage string `yaml:"pathTargetPackage,omitempty"`
RequireVersion string `yaml:"requireVersion,omitempty"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ require (
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
golang.org/x/tools v0.1.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
moul.io/http2curl v1.0.0 // indirect
)
55 changes: 41 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"regexp"
"strings"

"github.com/foomo/gocontentful/config"
"github.com/foomo/gocontentful/erm"
)

var VERSION = "v1.0.18"
var VERSION = "v1.0.19"

type contentfulRc struct {
ManagementToken string `json:"managementToken"`
Expand Down Expand Up @@ -59,6 +60,7 @@ func getCmaKeyFromRcFile() string {

func main() {
// Get parameters from cmd line flags
flagConfigFile := flag.String("configfile", "", "Full path to configuration file")
flagSpaceID := flag.String("spaceid", "", "Contentful space ID")
flagCMAKey := flag.String("cmakey", "", "[Optional] Contentful CMA key")
flagEnvironment := flag.String("environment", "", "[Optional] Contentful space environment")
Expand All @@ -77,37 +79,62 @@ func main() {
Usage()
os.Exit(0)
}
var conf *config.Config
var err error
if *flagConfigFile != "" {
conf, err = config.LoadConfigFromYAML(*flagConfigFile)
if err != nil {
fatal(err)
}
if conf.RequireVersion != "" && conf.RequireVersion != VERSION {
fatal("Required version mismatch. Want: " + conf.RequireVersion + " Have: " + VERSION)
}
} else {
conf = &config.Config{
SpaceID: *flagSpaceID,
Environment: *flagEnvironment,
ExportFile: *flagGenerateFromExport,
}
if *flagContentTypes != "" {
conf.ContentTypes = strings.Split(*flagContentTypes, ",")
}
}
cmaKey := *flagCMAKey
if cmaKey == "" && *flagGenerateFromExport == "" {
cmaKey = getCmaKeyFromRcFile()
}
if *flagGenerateFromExport == "" && (*flagSpaceID == "" || cmaKey == "") ||
*flagGenerateFromExport != "" && (*flagSpaceID != "" || cmaKey != "") {
usageError("Please specify either a Contentful Space ID and CMA access token or an export file name")
if conf.ExportFile == "" && (conf.SpaceID == "" || cmaKey == "") ||
conf.ExportFile != "" && (conf.SpaceID != "" || cmaKey != "") {
usageError("Please provide either a Contentful Space ID and CMA access token or an export file name")
}

if len(flag.Args()) != 1 {
var path string
if len(flag.Args()) != 1 && conf.PathTargetPackage == "" {
usageError("Missing arg path/to/target/package")
}

path := flag.Arg(0)
if conf.PathTargetPackage != "" {
path = conf.PathTargetPackage
} else {
path = flag.Arg(0)
}
packageName := filepath.Base(path)
fmt.Println("output path:", path)
fmt.Println("packageName:", packageName)

matched, err := regexp.MatchString(`[a-z].{2,}`, packageName)
if !matched {
if !matched || err != nil {
usageError("Please specify the package name correctly (only small caps letters)")
}

fmt.Printf("Contentful API Generator %s starting...\n\n", VERSION)

var flagContentTypesSlice []string
if *flagContentTypes != "" {
for _, contentType := range strings.Split(*flagContentTypes, ",") {
flagContentTypesSlice = append(flagContentTypesSlice, strings.TrimSpace(contentType))
var cleanContentTypes []string
if len(conf.ContentTypes) > 0 {
for _, contentType := range conf.ContentTypes {
cleanContentTypes = append(cleanContentTypes, strings.TrimSpace(contentType))
}
}

err = erm.GenerateAPI(filepath.Dir(path), packageName, *flagSpaceID, cmaKey, *flagEnvironment, *flagGenerateFromExport, flagContentTypesSlice, VERSION)
err = erm.GenerateAPI(filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION)
if err != nil {
fatal("Something went horribly wrong...", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvobase.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvolib.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/testapi/gocontentfulvolibbrand.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/testapi/gocontentfulvolibcategory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/testapi/gocontentfulvolibproduct.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2dcd1a3

Please sign in to comment.