From 92bbf5fb8b0f3c5a31d3cdafe9ced0234b335be0 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 15 Feb 2023 13:48:35 +0100 Subject: [PATCH] osbuild: add generator function for shell.init stage Generates a stage from a basic nested map. --- internal/osbuild/shell_init_stage.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/osbuild/shell_init_stage.go b/internal/osbuild/shell_init_stage.go index fb95c9ab8..f007f35ef 100644 --- a/internal/osbuild/shell_init_stage.go +++ b/internal/osbuild/shell_init_stage.go @@ -3,6 +3,8 @@ package osbuild import ( "fmt" "regexp" + + "github.com/osbuild/osbuild-composer/internal/shell" ) const filenameRegex = "^[a-zA-Z0-9\\.\\-_]{1,250}$" @@ -55,3 +57,19 @@ func NewShellInitStage(options *ShellInitStageOptions) *Stage { Options: options, } } + +// GenShellInitStage generates an org.osbuild.shell.init stage from a basic map +// of the form filename->key->value. +func GenShellInitStage(initFiles []shell.InitFile) *Stage { + options := new(ShellInitStageOptions) + options.Files = make(map[string]ShellInitFile, len(initFiles)) + for _, file := range initFiles { + vars := make([]EnvironmentVariable, len(file.Variables)) + for idx, envVar := range file.Variables { + vars[idx] = EnvironmentVariable{Key: envVar.Key, Value: envVar.Value} + } + options.Files[file.Filename] = ShellInitFile{Env: vars} + } + + return NewShellInitStage(options) +}