osbuild: add generator function for shell.init stage

Generates a stage from a basic nested map.
This commit is contained in:
Achilleas Koutsou 2023-02-15 13:48:35 +01:00 committed by Tomáš Hozza
parent a024b923a3
commit 92bbf5fb8b

View file

@ -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)
}