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
6 changes: 5 additions & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
{
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
},
{
"path": "detect_secrets.filters.common.is_baseline_file",
"filename": ".secrets.baseline"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
"min_level": 2
Expand Down Expand Up @@ -145,5 +149,5 @@
}
]
},
"generated_at": "2025-05-17T07:56:49Z"
"generated_at": "2025-06-17T15:02:37Z"
}
1 change: 1 addition & 0 deletions changes/20250617120442.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: Add support to disable generating extensions for HAL link data
23 changes: 15 additions & 8 deletions generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -16,12 +15,13 @@ import (
)

const (
app = "generate_code"
inputArg = "input"
outputArg = "output"
templateArg = "template"
GenerateType = "generate"
clientPath = "client_path"
app = "generate_code"
inputArg = "input"
outputArg = "output"
templateArg = "template"
GenerateType = "generate"
clientPath = "client-path"
disableLinksArg = "disable-links"
)

var (
Expand Down Expand Up @@ -49,12 +49,14 @@ func init() {
rootCmd.Flags().StringP(templateArg, "t", "", "path to the template file if defaults need overriding")
rootCmd.Flags().StringP(GenerateType, "g", "", "what to generate")
rootCmd.Flags().StringP(clientPath, "c", "", "path to the client go package generated by OpenAPI Generator")
rootCmd.Flags().StringP(disableLinksArg, "d", "", "disable the generation of HAL link data")

_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_INPUT", rootCmd.Flags().Lookup(inputArg))
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_OUTPUT", rootCmd.Flags().Lookup(outputArg))
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_TEMPLATE", rootCmd.Flags().Lookup(templateArg))
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_GENERATE_TYPE", rootCmd.Flags().Lookup(GenerateType))
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_CLIENT_PATH", rootCmd.Flags().Lookup(clientPath))
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_DISABLE_LINKS", rootCmd.Flags().Lookup(disableLinksArg))
}

func RunCLI(ctx context.Context) (err error) {
Expand All @@ -81,7 +83,12 @@ func RunCLI(ctx context.Context) (err error) {
return
}

err = codegen.CopyStaticFiles(ctx, d.PackageName, filepath.Dir(d.DestinationPath))
staticFileConfig, err := codegen.GenerateStaticFileConfigStruct(extensionConfig)
if err != nil {
return
}

err = codegen.CopyStaticFiles(ctx, staticFileConfig)
if err != nil {
return
}
Expand Down
46 changes: 39 additions & 7 deletions generator/codegen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"go/format"
"path/filepath"
"slices"
"strings"
"text/template"

Expand Down Expand Up @@ -44,6 +45,7 @@ type Data struct {
DestinationPath string
ClientPackagePath string
PackageName string
DisableLinks bool
}

var ValidGenerationTargets = map[string]func(*Data) error{
Expand All @@ -58,6 +60,13 @@ type ExtensionsConfig struct {
Template string `mapstructure:"template"`
GenerateType string `mapstructure:"generate_type"`
ClientPackagePath string `mapstructure:"client_path"`
DisableLinks bool `mapstructure:"disable_links"`
}

type StaticFileConfig struct {
ClientName string
Destination string
Exclusions []string
}

func DefaultExtensionsConfig() *ExtensionsConfig {
Expand Down Expand Up @@ -118,6 +127,24 @@ func GenerateDataStruct(cfg ExtensionsConfig) (d *Data, err error) {
}, nil
}

func GenerateStaticFileConfigStruct(cfg ExtensionsConfig) (d *StaticFileConfig, err error) {
clientName, err := getPackageNameFromPath(cfg.ClientPackagePath)
if err != nil {
return
}

var exclusions []string
if cfg.DisableLinks {
exclusions = append(exclusions, "static/extension_model_hal_link_data.go.static")
}

return &StaticFileConfig{
ClientName: clientName,
Destination: filepath.Dir(cfg.Output),
Exclusions: exclusions,
}, nil
}

func GenerateTemplateFile(ctx context.Context, d *Data) (err error) {
t, err := template.
New(filepath.Base(d.TemplatePath)).
Expand Down Expand Up @@ -227,23 +254,28 @@ func isExtensionFlagSet(props openapi3.ExtensionProps, flagKey string) (isSet bo
return
}

func CopyStaticFiles(ctx context.Context, clientName string, destination string) (err error) {
func CopyStaticFiles(ctx context.Context, staticFileConfig *StaticFileConfig) (err error) {
efs, fsErr := filesystem.NewEmbedFileSystem(&static)
if fsErr != nil {
return commonerrors.Newf(commonerrors.ErrUnexpected, "failed to create a filesystem for directory `%s`: %s", destination, fsErr.Error())
return commonerrors.Newf(commonerrors.ErrUnexpected, "failed to create a filesystem for directory `%s`: %s", staticFileConfig.Destination, fsErr.Error())
}

files, lsErr := efs.FindAll(".", "go.static")
if lsErr != nil {
return commonerrors.Newf(commonerrors.ErrUnexpected, "no files with the '.go.static' extension were found in the directory `%s`", destination)
return commonerrors.Newf(commonerrors.ErrUnexpected, "no files with the '.go.static' extension were found in the directory `%s`", staticFileConfig.Destination)
}

mkdirErr := filesystem.MkDir(destination)
mkdirErr := filesystem.MkDir(staticFileConfig.Destination)
if mkdirErr != nil {
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", destination, mkdirErr.Error())
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", staticFileConfig.Destination, mkdirErr.Error())
}

for _, f := range files {
// Skip the file if it's contained in exclusions
if slices.Contains(staticFileConfig.Exclusions, f) {
continue
}

t, tmplErr := template.
New(filesystem.FilePathBase(efs, f)).
ParseFS(static, f)
Expand All @@ -255,8 +287,8 @@ func CopyStaticFiles(ctx context.Context, clientName string, destination string)
resultFileName = strings.TrimSuffix(resultFileName, ".static")

d := Data{
DestinationPath: filepath.Join(destination, resultFileName),
PackageName: clientName,
DestinationPath: filepath.Join(staticFileConfig.Destination, resultFileName),
PackageName: staticFileConfig.ClientName,
}
err = generateSourceCode(ctx, &d, t)
if err != nil {
Expand Down
Loading