@@ -22,6 +22,7 @@ import (
2222 "slices"
2323 "strconv"
2424 "strings"
25+ "text/template"
2526
2627 "github.com/magefile/mage/mg"
2728 "github.com/magefile/mage/sh"
@@ -87,26 +88,27 @@ type OSPackageArgs struct {
8788
8889// PackageSpec specifies package metadata and the contents of the package.
8990type 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
110112
111113 evalContext map [string ]interface {}
112114 packageDir string
@@ -475,6 +477,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
475477
476478// ImageName computes the image name from the spec.
477479func (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+
478504 if s .DockerVariant == Basic {
479505 // no suffix for basic docker variant
480506 return s .Name
@@ -774,7 +800,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
774800 args = append (args , "--vendor" , spec .Vendor )
775801 }
776802 if spec .License != "" {
777- args = append (args , "--license" , strings .Replace (spec .License , " " , "-" , - 1 ))
803+ args = append (args , "--license" , strings .ReplaceAll (spec .License , " " , "-" ))
778804 }
779805 if spec .Description != "" {
780806 args = append (args , "--description" , spec .Description )
0 commit comments