Skip to content

Commit

Permalink
templates: if a default init/uinit/shell is specified, allow template…
Browse files Browse the repository at this point in the history
…s to override with empty

Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 25, 2024
1 parent 565b87e commit 285658e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
18 changes: 12 additions & 6 deletions uimage/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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":
Expand Down
18 changes: 17 additions & 1 deletion uimage/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestMods(t *testing.T) {
name string
tpl string
config string
base []uimage.Modifier
want *uimage.Opts
err error
}{
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
9 changes: 0 additions & 9 deletions uimage/uimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 285658e

Please sign in to comment.