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