From 7d5c22e06cc76545becfee89fdf6a0402eafc48e Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sun, 25 Feb 2024 03:44:08 +0000 Subject: [PATCH] templates: if a default init/uinit/shell is specified, allow templates to override with empty Signed-off-by: Chris Koch --- uimage/templates/templates.go | 18 ++++++++++++------ uimage/templates/templates_test.go | 18 +++++++++++++++++- uimage/uimage.go | 9 --------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/uimage/templates/templates.go b/uimage/templates/templates.go index 5788e6f..f0fce96 100644 --- a/uimage/templates/templates.go +++ b/uimage/templates/templates.go @@ -52,9 +52,9 @@ type Config struct { BuildTags []string `yaml:"build_tags"` Commands []Command Files []string - Init string - Uinit string - Shell string + Init *string + Uinit *string + Shell *string } // Templates are a set of mkuimage build configs and command templates. @@ -79,15 +79,21 @@ func (t *Templates) Uimage(config string) ([]uimage.Modifier, error) { } m := []uimage.Modifier{ uimage.WithFiles(c.Files...), - uimage.WithInit(c.Init), - uimage.WithUinitCommand(c.Uinit), - uimage.WithShell(c.Shell), uimage.WithEnv( golang.WithGOOS(c.GOOS), golang.WithGOARCH(c.GOARCH), golang.WithBuildTag(c.BuildTags...), ), } + if c.Init != nil { + m = append(m, uimage.WithInit(*c.Init)) + } + if c.Uinit != nil { + m = append(m, uimage.WithUinitCommand(*c.Uinit)) + } + if c.Shell != nil { + m = append(m, uimage.WithShell(*c.Shell)) + } for _, cmds := range c.Commands { switch cmds.Builder { case "binary": diff --git a/uimage/templates/templates_test.go b/uimage/templates/templates_test.go index b87bb92..4d65c9c 100644 --- a/uimage/templates/templates_test.go +++ b/uimage/templates/templates_test.go @@ -21,6 +21,7 @@ func TestMods(t *testing.T) { name string tpl string config string + base []uimage.Modifier want *uimage.Opts err error }{ @@ -109,6 +110,21 @@ configs: `, config: "", }, + { + name: "override base", + tpl: ` +configs: + noinit: + init: "" +`, + config: "noinit", + base: []uimage.Modifier{ + uimage.WithInit("init"), + }, + want: &uimage.Opts{ + Env: golang.Default(), + }, + }, } { t.Run(tt.name, func(t *testing.T) { tpl, err := TemplateFrom([]byte(tt.tpl)) @@ -122,7 +138,7 @@ configs: if len(mods) == 0 { return } - got, err := uimage.OptionsFor(mods...) + got, err := uimage.OptionsFor(append(tt.base, mods...)...) if err != nil { t.Fatal(err) } diff --git a/uimage/uimage.go b/uimage/uimage.go index 2bc88f6..9b15df0 100644 --- a/uimage/uimage.go +++ b/uimage/uimage.go @@ -559,9 +559,6 @@ func WithUinit(arg0 string, args ...string) Modifier { // This can be an absolute path or the name of a command included in // Commands. func WithInit(arg0 string) Modifier { - if arg0 == "" { - return nil - } return func(opts *Opts) error { opts.InitCmd = arg0 return nil @@ -574,9 +571,6 @@ func WithInit(arg0 string) Modifier { // This can be an absolute path or the name of a command included in // Commands. func WithShell(arg0 string) Modifier { - if arg0 == "" { - return nil - } return func(opts *Opts) error { opts.DefaultShell = arg0 return nil @@ -585,9 +579,6 @@ func WithShell(arg0 string) Modifier { // WithTempDir sets a temporary directory to use for building commands. func WithTempDir(dir string) Modifier { - if dir == "" { - return nil - } return func(o *Opts) error { o.TempDir = dir return nil