Skip to content
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

Feat/scaffold templates from arg #173

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 internal/cmd/generate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cmd *GenerateResourcesCommand) Help() string {
return strBuilder.String()
}

func (a *GenerateResourcesCommand) Synopsis() string {
func (cmd *GenerateResourcesCommand) Synopsis() string {
return "Generate code for resources from an Intermediate Representation (IR) JSON file."
}

Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/scaffold_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ScaffoldDataSourceCommand struct {
flagOutputFile string
flagPackageName string
flagForceOverwrite bool
flagTemplatePath string
}

func (cmd *ScaffoldDataSourceCommand) Flags() *flag.FlagSet {
Expand All @@ -36,6 +37,8 @@ func (cmd *ScaffoldDataSourceCommand) Flags() *flag.FlagSet {
fs.StringVar(&cmd.flagOutputDir, "output-dir", ".", "directory path to output scaffolded code file")
fs.StringVar(&cmd.flagOutputFile, "output-file", "", "file name and extension to write scaffolded code to, default will use the --name flag with '_data_source.go' suffix")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for scaffolded code file")
fs.StringVar(&cmd.flagTemplatePath, "template", "", "path to template file for scaffolded code file")

return fs
}

Expand Down Expand Up @@ -77,7 +80,7 @@ func (cmd *ScaffoldDataSourceCommand) Help() string {
return strBuilder.String()
}

func (a *ScaffoldDataSourceCommand) Synopsis() string {
func (cmd *ScaffoldDataSourceCommand) Synopsis() string {
return "Create scaffolding code for a Terraform Plugin Framework data source."
}

Expand Down Expand Up @@ -110,7 +113,7 @@ func (cmd *ScaffoldDataSourceCommand) runInternal(_ context.Context) error {
return fmt.Errorf("'%s' is not a valid Terraform data source identifier", cmd.flagDataSourceNameSnake)
}

goBytes, err := scaffold.DataSourceBytes(dataSourceIdentifier, cmd.flagPackageName)
goBytes, err := scaffold.DataSourceBytes(dataSourceIdentifier, cmd.flagPackageName, cmd.flagTemplatePath)
if err != nil {
return fmt.Errorf("error creating scaffolding data source Go code: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/scaffold_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ScaffoldResourceCommand struct {
flagOutputFile string
flagPackageName string
flagForceOverwrite bool
flagTemplatePath string
}

func (cmd *ScaffoldResourceCommand) Flags() *flag.FlagSet {
Expand All @@ -36,6 +37,8 @@ func (cmd *ScaffoldResourceCommand) Flags() *flag.FlagSet {
fs.StringVar(&cmd.flagOutputDir, "output-dir", ".", "directory path to output scaffolded code file")
fs.StringVar(&cmd.flagOutputFile, "output-file", "", "file name and extension to write scaffolded code to, default will use the --name flag with '_resource.go' suffix")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for scaffolded code file")
fs.StringVar(&cmd.flagTemplatePath, "template", "", "path to template file for scaffolded code file")

return fs
}

Expand Down Expand Up @@ -77,7 +80,7 @@ func (cmd *ScaffoldResourceCommand) Help() string {
return strBuilder.String()
}

func (a *ScaffoldResourceCommand) Synopsis() string {
func (cmd *ScaffoldResourceCommand) Synopsis() string {
return "Create scaffolding code for a Terraform Plugin Framework resource."
}

Expand Down Expand Up @@ -110,7 +113,7 @@ func (cmd *ScaffoldResourceCommand) runInternal(_ context.Context) error {
return fmt.Errorf("'%s' is not a valid Terraform resource identifier", cmd.flagResourceNameSnake)
}

goBytes, err := scaffold.ResourceBytes(resourceIdentifier, cmd.flagPackageName)
goBytes, err := scaffold.ResourceBytes(resourceIdentifier, cmd.flagPackageName, cmd.flagTemplatePath)
if err != nil {
return fmt.Errorf("error creating scaffolding resource Go code: %w", err)
}
Expand Down
16 changes: 14 additions & 2 deletions internal/scaffold/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ package scaffold

import (
"bytes"
"os"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

// DataSourceBytes will create scaffolding Go code bytes for a Terraform Plugin Framework data source
func DataSourceBytes(dataSourceIdentifier schema.FrameworkIdentifier, packageName string) ([]byte, error) {
t, err := template.New("data_source_scaffold").Parse(dataSourceScaffoldGoTemplate)
func DataSourceBytes(dataSourceIdentifier schema.FrameworkIdentifier, packageName, templatePath string) ([]byte, error) {
templateStr := dataSourceScaffoldGoTemplate

if templatePath != "" {
file, err := os.ReadFile(templatePath)
if err != nil {
return nil, err
}

templateStr = string(file)
}

t, err := template.New("data_source_scaffold").Parse(templateStr)
if err != nil {
return nil, err
}
Expand Down
18 changes: 16 additions & 2 deletions internal/scaffold/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ package scaffold

import (
"bytes"
"os"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

// ResourceBytes will create scaffolding Go code bytes for a Terraform Plugin Framework resource
func ResourceBytes(resourceIdentifier schema.FrameworkIdentifier, packageName string) ([]byte, error) {
t, err := template.New("resource_scaffold").Parse(resourceScaffoldGoTemplate)
func ResourceBytes(resourceIdentifier schema.FrameworkIdentifier, packageName, templatePath string) ([]byte, error) {
templateStr := resourceScaffoldGoTemplate

if templatePath != "" {
file, err := os.ReadFile(templatePath)
if err != nil {
return nil, err
}

templateStr = string(file)
}

t, err := template.New("resource_scaffold").Parse(templateStr)
if err != nil {
return nil, err
}
Expand All @@ -24,11 +36,13 @@ func ResourceBytes(resourceIdentifier schema.FrameworkIdentifier, packageName st
NameSnake string
NameCamel string
NamePascal string
NameKebab string
}{
PackageName: packageName,
NameSnake: string(resourceIdentifier),
NameCamel: resourceIdentifier.ToCamelCase(),
NamePascal: resourceIdentifier.ToPascalCase(),
NameKebab: resourceIdentifier.ToKebabCase(),
}

err = t.Execute(&buf, templateData)
Expand Down
7 changes: 7 additions & 0 deletions internal/schema/framework_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ func (identifier FrameworkIdentifier) ToPrefixPascalCase(prefix string) string {
return pascalCase
}

// ToKebabCase will return kebab case formatted string of the identifier.
// Example:
// - example_resource_thing -> example-resource-thing
func (identifier FrameworkIdentifier) ToKebabCase() string {
return strings.ReplaceAll(identifier.ToString(), "_", "-")
}

// ToString returns the FrameworkIdentifier as a string without any formatting.
// Example:
// - example_resource_thing -> example_resource_thing
Expand Down