Skip to content

Commit

Permalink
Only create directory and package per data source and resource if pac…
Browse files Browse the repository at this point in the history
…kageName flag is not specified (#20)
  • Loading branch information
bendbennett committed Sep 5, 2023
1 parent 4eca0ac commit 22d1e3d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
}

// write code
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, formattedDataSourcesToFrom, outputPath)
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, formattedDataSourcesToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func generateProviderCode(spec spec.Specification, outputPath, packageName, gene
}

// write code
err = output.WriteProviders(formattedProvidersSchema, formattedProvidersModels, formattedProvidersModelObjectHelpers, formattedProviderToFrom, outputPath)
err = output.WriteProviders(formattedProvidersSchema, formattedProvidersModels, formattedProvidersModelObjectHelpers, formattedProviderToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func generateResourceCode(spec spec.Specification, outputPath, packageName, gene
}

// write code
err = output.WriteResources(formattedResourcesSchema, formattedResourcesModels, formattedResourcesModelObjectHelpers, formattedResourcesToFrom, outputPath)
err = output.WriteResources(formattedResourcesSchema, formattedResourcesModels, formattedResourcesModelObjectHelpers, formattedResourcesToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
46 changes: 31 additions & 15 deletions internal/output/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ import (
"path/filepath"
)

func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObjectHelpers, dataSourcesToFrom map[string][]byte, outputDir string) error {
// WriteDataSources uses the packageName to determine whether to create a directory and package per data source.
// If packageName is an empty string, this indicates that the flag was not set, and the default behaviour is
// then to create a package and directory per data source. If packageName is set then all generated code is
// placed into the same directory and package.
func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObjectHelpers, dataSourcesToFrom map[string][]byte, outputDir, packageName string) error {
for k, v := range dataSourcesSchema {
dirName := fmt.Sprintf("datasource_%s", k)
dirName := ""

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
if packageName == "" {
dirName = fmt.Sprintf("datasource_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}
}

filename := fmt.Sprintf("%s_data_source_gen.go", k)
Expand Down Expand Up @@ -51,13 +59,17 @@ func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObje
return nil
}

func WriteResources(resourcesSchema, resourcesModels, resourcesModelObjectHelpers, resourcesToFrom map[string][]byte, outputDir string) error {
func WriteResources(resourcesSchema, resourcesModels, resourcesModelObjectHelpers, resourcesToFrom map[string][]byte, outputDir, packageName string) error {
for k, v := range resourcesSchema {
dirName := fmt.Sprintf("resource_%s", k)
dirName := ""

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
if packageName == "" {
dirName = fmt.Sprintf("resource_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}
}

filename := fmt.Sprintf("%s_resource_gen.go", k)
Expand Down Expand Up @@ -91,13 +103,17 @@ func WriteResources(resourcesSchema, resourcesModels, resourcesModelObjectHelper
return nil
}

func WriteProviders(providersSchema, providerModels, providerModelObjectHelpers, providerToFrom map[string][]byte, outputDir string) error {
func WriteProviders(providersSchema, providerModels, providerModelObjectHelpers, providerToFrom map[string][]byte, outputDir, packageName string) error {
for k, v := range providersSchema {
dirName := fmt.Sprintf("provider_%s", k)
dirName := ""

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
if packageName == "" {
dirName = fmt.Sprintf("provider_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}
}

filename := fmt.Sprintf("%s_provider_gen.go", k)
Expand Down

0 comments on commit 22d1e3d

Please sign in to comment.