88 "fmt"
99 "go/format"
1010 "path/filepath"
11+ "slices"
1112 "strings"
1213 "text/template"
1314
@@ -44,6 +45,7 @@ type Data struct {
4445 DestinationPath string
4546 ClientPackagePath string
4647 PackageName string
48+ DisableLinks bool
4749}
4850
4951var ValidGenerationTargets = map [string ]func (* Data ) error {
@@ -58,6 +60,13 @@ type ExtensionsConfig struct {
5860 Template string `mapstructure:"template"`
5961 GenerateType string `mapstructure:"generate_type"`
6062 ClientPackagePath string `mapstructure:"client_path"`
63+ DisableLinks bool `mapstructure:"disable_links"`
64+ }
65+
66+ type StaticFileConfig struct {
67+ ClientName string
68+ Destination string
69+ Exclusions []string
6170}
6271
6372func DefaultExtensionsConfig () * ExtensionsConfig {
@@ -118,6 +127,24 @@ func GenerateDataStruct(cfg ExtensionsConfig) (d *Data, err error) {
118127 }, nil
119128}
120129
130+ func GenerateStaticFileConfigStruct (cfg ExtensionsConfig ) (d * StaticFileConfig , err error ) {
131+ clientName , err := getPackageNameFromPath (cfg .ClientPackagePath )
132+ if err != nil {
133+ return
134+ }
135+
136+ var exclusions []string
137+ if cfg .DisableLinks {
138+ exclusions = append (exclusions , "static/extension_model_hal_link_data.go.static" )
139+ }
140+
141+ return & StaticFileConfig {
142+ ClientName : clientName ,
143+ Destination : filepath .Dir (cfg .Output ),
144+ Exclusions : exclusions ,
145+ }, nil
146+ }
147+
121148func GenerateTemplateFile (ctx context.Context , d * Data ) (err error ) {
122149 t , err := template .
123150 New (filepath .Base (d .TemplatePath )).
@@ -227,23 +254,28 @@ func isExtensionFlagSet(props openapi3.ExtensionProps, flagKey string) (isSet bo
227254 return
228255}
229256
230- func CopyStaticFiles (ctx context.Context , clientName string , destination string ) (err error ) {
257+ func CopyStaticFiles (ctx context.Context , staticFileConfig * StaticFileConfig ) (err error ) {
231258 efs , fsErr := filesystem .NewEmbedFileSystem (& static )
232259 if fsErr != nil {
233- return commonerrors .Newf (commonerrors .ErrUnexpected , "failed to create a filesystem for directory `%s`: %s" , destination , fsErr .Error ())
260+ return commonerrors .Newf (commonerrors .ErrUnexpected , "failed to create a filesystem for directory `%s`: %s" , staticFileConfig . Destination , fsErr .Error ())
234261 }
235262
236263 files , lsErr := efs .FindAll ("." , "go.static" )
237264 if lsErr != nil {
238- return commonerrors .Newf (commonerrors .ErrUnexpected , "no files with the '.go.static' extension were found in the directory `%s`" , destination )
265+ return commonerrors .Newf (commonerrors .ErrUnexpected , "no files with the '.go.static' extension were found in the directory `%s`" , staticFileConfig . Destination )
239266 }
240267
241- mkdirErr := filesystem .MkDir (destination )
268+ mkdirErr := filesystem .MkDir (staticFileConfig . Destination )
242269 if mkdirErr != nil {
243- return commonerrors .Newf (commonerrors .ErrUnexpected , "could not create directory `%s`: %s" , destination , mkdirErr .Error ())
270+ return commonerrors .Newf (commonerrors .ErrUnexpected , "could not create directory `%s`: %s" , staticFileConfig . Destination , mkdirErr .Error ())
244271 }
245272
246273 for _ , f := range files {
274+ // Skip the file if it's contained in exclusions
275+ if slices .Contains (staticFileConfig .Exclusions , f ) {
276+ continue
277+ }
278+
247279 t , tmplErr := template .
248280 New (filesystem .FilePathBase (efs , f )).
249281 ParseFS (static , f )
@@ -255,8 +287,8 @@ func CopyStaticFiles(ctx context.Context, clientName string, destination string)
255287 resultFileName = strings .TrimSuffix (resultFileName , ".static" )
256288
257289 d := Data {
258- DestinationPath : filepath .Join (destination , resultFileName ),
259- PackageName : clientName ,
290+ DestinationPath : filepath .Join (staticFileConfig . Destination , resultFileName ),
291+ PackageName : staticFileConfig . ClientName ,
260292 }
261293 err = generateSourceCode (ctx , & d , t )
262294 if err != nil {
0 commit comments