@@ -22,6 +22,7 @@ import (
22
22
"slices"
23
23
"strconv"
24
24
"strings"
25
+ "text/template"
25
26
26
27
"github.com/magefile/mage/mg"
27
28
"github.com/magefile/mage/sh"
@@ -87,26 +88,27 @@ type OSPackageArgs struct {
87
88
88
89
// PackageSpec specifies package metadata and the contents of the package.
89
90
type PackageSpec struct {
90
- Name string `yaml:"name,omitempty"`
91
- ServiceName string `yaml:"service_name,omitempty"`
92
- OS string `yaml:"os,omitempty"`
93
- Arch string `yaml:"arch,omitempty"`
94
- Vendor string `yaml:"vendor,omitempty"`
95
- Snapshot bool `yaml:"snapshot"`
96
- FIPS bool `yaml:"fips"`
97
- Version string `yaml:"version,omitempty"`
98
- License string `yaml:"license,omitempty"`
99
- URL string `yaml:"url,omitempty"`
100
- Description string `yaml:"description,omitempty"`
101
- DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
102
- PreInstallScript string `yaml:"pre_install_script,omitempty"`
103
- PostInstallScript string `yaml:"post_install_script,omitempty"`
104
- PostRmScript string `yaml:"post_rm_script,omitempty"`
105
- Files map [string ]PackageFile `yaml:"files"`
106
- Qualifier string `yaml:"qualifier,omitempty"` // Optional
107
- OutputFile string `yaml:"output_file,omitempty"` // Optional
108
- ExtraVars map [string ]string `yaml:"extra_vars,omitempty"` // Optional
109
- Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
91
+ Name string `yaml:"name,omitempty"`
92
+ ServiceName string `yaml:"service_name,omitempty"`
93
+ OS string `yaml:"os,omitempty"`
94
+ Arch string `yaml:"arch,omitempty"`
95
+ Vendor string `yaml:"vendor,omitempty"`
96
+ Snapshot bool `yaml:"snapshot"`
97
+ FIPS bool `yaml:"fips"`
98
+ Version string `yaml:"version,omitempty"`
99
+ License string `yaml:"license,omitempty"`
100
+ URL string `yaml:"url,omitempty"`
101
+ Description string `yaml:"description,omitempty"`
102
+ DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
103
+ DockerImageNameTemplate string `yaml:"docker_image_name_template,omitempty"` // Optional: template of the docker image name
104
+ PreInstallScript string `yaml:"pre_install_script,omitempty"`
105
+ PostInstallScript string `yaml:"post_install_script,omitempty"`
106
+ PostRmScript string `yaml:"post_rm_script,omitempty"`
107
+ Files map [string ]PackageFile `yaml:"files"`
108
+ Qualifier string `yaml:"qualifier,omitempty"` // Optional
109
+ OutputFile string `yaml:"output_file,omitempty"` // Optional
110
+ ExtraVars map [string ]string `yaml:"extra_vars,omitempty"` // Optional
111
+ Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
110
112
111
113
evalContext map [string ]interface {}
112
114
packageDir string
@@ -475,6 +477,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
475
477
476
478
// ImageName computes the image name from the spec.
477
479
func (s PackageSpec ) ImageName () string {
480
+ if s .DockerImageNameTemplate != "" {
481
+ imageNameTmpl , err := template .New ("dockerImageTemplate" ).Parse (s .DockerImageNameTemplate )
482
+ if err != nil {
483
+ panic (fmt .Errorf ("parsing docker image name template for %s variant %s: %w" , s .Name , s .DockerVariant , err ))
484
+ }
485
+
486
+ data := s .toMap ()
487
+ for k , v := range varMap () {
488
+ data [k ] = v
489
+ }
490
+
491
+ buf := new (strings.Builder )
492
+ err = imageNameTmpl .Execute (buf , data )
493
+ if err != nil {
494
+ panic (fmt .Errorf ("rendering docker image name template for %s variant %s: %w" , s .Name , s .DockerVariant , err ))
495
+ }
496
+
497
+ imageName := buf .String ()
498
+ if mg .Verbose () {
499
+ log .Printf ("rendered image name for %s variant %s: %s" , s .Name , s .DockerVariant , imageName )
500
+ }
501
+ return imageName
502
+ }
503
+
478
504
if s .DockerVariant == Basic {
479
505
// no suffix for basic docker variant
480
506
return s .Name
@@ -774,7 +800,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
774
800
args = append (args , "--vendor" , spec .Vendor )
775
801
}
776
802
if spec .License != "" {
777
- args = append (args , "--license" , strings .Replace (spec .License , " " , "-" , - 1 ))
803
+ args = append (args , "--license" , strings .ReplaceAll (spec .License , " " , "-" ))
778
804
}
779
805
if spec .Description != "" {
780
806
args = append (args , "--description" , spec .Description )
0 commit comments