Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ts, err := ackgenerate.APIs(g, optTemplatesDir)
ts, err := ackgenerate.APIs(g, optTemplateDirs)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func generateController(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ts, err := ackgenerate.Controller(g, optTemplatesDir)
ts, err := ackgenerate.Controller(g, optTemplateDirs)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/ack-generate/command/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
if err := ensureSDKRepo(optCacheDir); err != nil {
return err
}
optTemplatesDir = filepath.Join(optTemplatesDir, "crossplane")
svcAlias := strings.ToLower(args[0])
sdkHelper := model.NewSDKHelper(sdkDir)
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
Expand Down Expand Up @@ -83,7 +82,7 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
return err
}

ts, err := cpgenerate.Crossplane(g, optTemplatesDir)
ts, err := cpgenerate.Crossplane(g, optTemplateDirs)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
}

// generate templates
ts, err := olmgenerate.BundleAssets(g, commonMeta, svcConf, version, optTemplatesDir)
ts, err := olmgenerate.BundleAssets(g, commonMeta, svcConf, version, optTemplateDirs)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func generateRelease(cmd *cobra.Command, args []string) error {
return err
}
ts, err := ackgenerate.Release(
g, optTemplatesDir,
g, optTemplateDirs,
releaseVersion, optImageRepository, optServiceAccountName,
)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/ack-generate/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var (
optCacheDir string
optRefreshCache bool
optAWSSDKGoVersion string
defaultTemplatesDir string
optTemplatesDir string
defaultTemplateDirs []string
optTemplateDirs []string
defaultServicesDir string
optServicesDir string
optDryRun bool
Expand Down Expand Up @@ -80,7 +80,7 @@ func init() {
for _, tryPath := range tryPaths {
if fi, err := os.Stat(tryPath); err == nil {
if fi.IsDir() {
defaultTemplatesDir = tryPath
defaultTemplateDirs = append(defaultTemplateDirs, tryPath)
break
}
}
Expand All @@ -100,8 +100,8 @@ func init() {
rootCmd.PersistentFlags().BoolVar(
&optDryRun, "dry-run", false, "If true, outputs all files to stdout",
)
rootCmd.PersistentFlags().StringVar(
&optTemplatesDir, "templates-dir", defaultTemplatesDir, "Path to directory with templates to use in code generation",
rootCmd.PersistentFlags().StringSliceVar(
&optTemplateDirs, "template-dirs", defaultTemplateDirs, "Paths to directories with templates to use in code generation. Note that the order in which directories is specified will be used to provide override functionality.",
)
rootCmd.PersistentFlags().StringVar(
&optServicesDir, "services-dir", defaultServicesDir, "Path to directory to output service-specific code",
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/ack/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
// generating ACK service controller's apis/ contents
func APIs(
g *generate.Generator,
templateBasePath string,
templateBasePaths []string,
) (*templateset.TemplateSet, error) {
enumDefs, err := g.GetEnumDefs()
if err != nil {
Expand All @@ -62,7 +62,7 @@ func APIs(
}

ts := templateset.New(
templateBasePath,
templateBasePaths,
apisIncludePaths,
apisCopyPaths,
apisFuncMap,
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ var (
// for generating ACK service controller implementations
func Controller(
g *generate.Generator,
templateBasePath string,
templateBasePaths []string,
) (*templateset.TemplateSet, error) {
crds, err := g.GetCRDs()
if err != nil {
return nil, err
}

ts := templateset.New(
templateBasePath,
templateBasePaths,
controllerIncludePaths,
controllerCopyPaths,
controllerFuncMap,
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/ack/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
// generating an ACK service controller release (Helm artifacts, etc)
func Release(
g *generate.Generator,
templateBasePath string,
templateBasePaths []string,
// releaseVersion is the SemVer string describing the release that the Helm
// chart will install
releaseVersion string,
Expand All @@ -59,7 +59,7 @@ func Release(
serviceAccountName string,
) (*templateset.TemplateSet, error) {
ts := templateset.New(
templateBasePath,
templateBasePaths,
releaseIncludePaths,
releaseCopyPaths,
releaseFuncMap,
Expand Down
24 changes: 12 additions & 12 deletions pkg/generate/crossplane/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ import (

var (
apisTemplatePaths = []string{
"apis/doc.go.tpl",
"apis/enums.go.tpl",
"apis/groupversion_info.go.tpl",
"apis/types.go.tpl",
"crossplane/apis/doc.go.tpl",
"crossplane/apis/enums.go.tpl",
"crossplane/apis/groupversion_info.go.tpl",
"crossplane/apis/types.go.tpl",
}
includePaths = []string{
"boilerplate.go.tpl",
"apis/enum_def.go.tpl",
"apis/type_def.go.tpl",
"pkg/sdk_find_read_one.go.tpl",
"pkg/sdk_find_read_many.go.tpl",
"pkg/sdk_find_get_attributes.go.tpl",
"crossplane/boilerplate.go.tpl",
"crossplane/apis/enum_def.go.tpl",
"crossplane/apis/type_def.go.tpl",
"crossplane/pkg/sdk_find_read_one.go.tpl",
"crossplane/pkg/sdk_find_read_many.go.tpl",
"crossplane/pkg/sdk_find_get_attributes.go.tpl",
}
copyPaths = []string{}
funcMap = ttpl.FuncMap{
Expand Down Expand Up @@ -111,7 +111,7 @@ type templateCRDVars struct {
// generating Crossplane API types and controller code for an AWS service API
func Crossplane(
g *generate.Generator,
templateBasePath string,
templateBasePaths []string,
) (*templateset.TemplateSet, error) {
enumDefs, err := g.GetEnumDefs()
if err != nil {
Expand All @@ -127,7 +127,7 @@ func Crossplane(
}

ts := templateset.New(
templateBasePath,
templateBasePaths,
includePaths,
copyPaths,
funcMap,
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/olm/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func BundleAssets(
commonMeta CommonMetadata,
serviceConfig ServiceConfig,
vers string,
templateBasePath string,
templateBasePaths []string,
) (*templateset.TemplateSet, error) {

ts := templateset.New(
templateBasePath,
templateBasePaths,
csvIncludePaths,
csvCopyPaths,
csvFuncMap,
Expand Down
110 changes: 83 additions & 27 deletions pkg/generate/templateset/templateset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@ package templateset

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
ttpl "text/template"

"github.com/pkg/errors"
)

var (
// ErrTemplateNotFound is returned when a template path couldn't be located
// in any base search path
ErrTemplateNotFound = errors.New("Template Not Found")
errMsgTemplateNotFound = "template %s not found in any base search path"
)

func errTemplateNotFound(templatePath string) error {
return errors.WithMessage(
ErrTemplateNotFound,
fmt.Sprintf(errMsgTemplateNotFound, templatePath),
)
}

// templateWithVars contains a template and the variables injected during execution
type templateWithVars struct {
t *ttpl.Template
Expand All @@ -30,28 +47,30 @@ type templateWithVars struct {
// TemplateSet contains a set of templates and copy files for a particular
// target
type TemplateSet struct {
basePath string
includePaths []string
copyPaths []string
templates map[string]templateWithVars
funcMap ttpl.FuncMap
executed map[string]*bytes.Buffer
// baseSearchPaths is a set of directories from which templates will be
// searched
baseSearchPaths []string
includePaths []string
copyPaths []string
templates map[string]templateWithVars
funcMap ttpl.FuncMap
executed map[string]*bytes.Buffer
}

// New returns a pointer to a TemplateSet
func New(
templateBasePath string,
baseSearchPaths []string,
includePaths []string,
copyPaths []string,
funcMap ttpl.FuncMap,
) *TemplateSet {
return &TemplateSet{
basePath: templateBasePath,
includePaths: includePaths,
copyPaths: copyPaths,
funcMap: funcMap,
templates: map[string]templateWithVars{},
executed: map[string]*bytes.Buffer{},
baseSearchPaths: baseSearchPaths,
includePaths: includePaths,
copyPaths: copyPaths,
funcMap: funcMap,
templates: map[string]templateWithVars{},
executed: map[string]*bytes.Buffer{},
}
}

Expand All @@ -61,27 +80,53 @@ func (ts *TemplateSet) Add(
templatePath string,
vars interface{},
) error {
path := filepath.Join(ts.basePath, templatePath)
tplContents, err := ioutil.ReadFile(path)
var foundPath string
for _, basePath := range ts.baseSearchPaths {
path := filepath.Join(basePath, templatePath)
if fileExists(path) {
foundPath = path
break
}
}

if foundPath == "" {
return errTemplateNotFound(templatePath)
}

tplContents, err := ioutil.ReadFile(foundPath)
if err != nil {
return err
}
t := ttpl.New(path)
t := ttpl.New(foundPath)
t = t.Funcs(ts.funcMap)
t, err = t.Parse(string(tplContents))
if err != nil {
return err
}
for _, includePath := range ts.includePaths {
tplPath := filepath.Join(ts.basePath, includePath)
if t, err = includeTemplate(t, tplPath); err != nil {
return err
}
if err = ts.joinIncludes(t); err != nil {
return err
}
ts.templates[outPath] = templateWithVars{t, vars}
return nil
}

// joinIncludes adds all include templates to the supplied template
func (ts *TemplateSet) joinIncludes(t *ttpl.Template) error {
var err error
for _, basePath := range ts.baseSearchPaths {
for _, includePath := range ts.includePaths {
tplPath := filepath.Join(basePath, includePath)
if !fileExists(tplPath) {
continue
}
if t, err = includeTemplate(t, tplPath); err != nil {
return err
}
}
}
return nil
}

// Execute runs all of the template and copy files in our TemplateSet and
// returns whether any error occurred executing any of the templates. Once
// Execute() is run, `TemplateSet.Executed()` can be used to iterate over a set
Expand All @@ -94,13 +139,18 @@ func (ts *TemplateSet) Execute() error {
}
ts.executed[path] = &b
}
for _, path := range ts.copyPaths {
copyPath := filepath.Join(ts.basePath, path)
b, err := byteBufferFromFile(copyPath)
if err != nil {
return err
for _, basePath := range ts.baseSearchPaths {
for _, path := range ts.copyPaths {
copyPath := filepath.Join(basePath, path)
if !fileExists(copyPath) {
continue
}
b, err := byteBufferFromFile(copyPath)
if err != nil {
return err
}
ts.executed[path] = b
}
ts.executed[path] = b
}
return nil
}
Expand Down Expand Up @@ -144,3 +194,9 @@ func includeTemplate(t *ttpl.Template, tplPath string) (*ttpl.Template, error) {
}
return t, nil
}

// fileExists returns tTrue if the supplied file path exists, false otherwise
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
Loading