Skip to content

Commit 7a0d7a7

Browse files
authored
Create CLI options struct. (#459)
Whenever a new CLI arg is added, currently 50+ tests have to be updated to handle the new param in the `CustomizeImageWithConfigFile` function. This can be pretty annoying. This change creates a new `ImageCustomizerOptions` struct to store all the options provided on the command-line. Now, the the CLI options can be passed by struct instead of new params being added each time. The existing functions `CustomizeImageWithConfigFile` and `CustomizeImage` have not been changed to avoid needing to update all the existing tests. Also, using the options struct can be quite verbose.
1 parent e16d222 commit 7a0d7a7

File tree

5 files changed

+218
-191
lines changed

5 files changed

+218
-191
lines changed

toolkit/tools/imagecustomizer/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ func runCommand(ctx context.Context, command string, cli *RootCmd) error {
113113
}
114114

115115
func customizeImage(ctx context.Context, cmd CustomizeCmd) error {
116-
err := imagecustomizerlib.CustomizeImageWithConfigFile(ctx, cmd.BuildDir, cmd.ConfigFile, cmd.InputImageFile,
117-
cmd.RpmSources, cmd.OutputImageFile, cmd.OutputImageFormat, !cmd.DisableBaseImageRpmRepos, cmd.PackageSnapshotTime)
116+
err := imagecustomizerlib.CustomizeImageWithConfigFileOptions(ctx, cmd.ConfigFile,
117+
imagecustomizerlib.ImageCustomizerOptions{
118+
BuildDir: cmd.BuildDir,
119+
InputImageFile: cmd.InputImageFile,
120+
RpmsSources: cmd.RpmSources,
121+
OutputImageFile: cmd.OutputImageFile,
122+
OutputImageFormat: cmd.OutputImageFormat,
123+
UseBaseImageRpmRepos: !cmd.DisableBaseImageRpmRepos,
124+
PackageSnapshotTime: cmd.PackageSnapshotTime,
125+
})
118126
if err != nil {
119127
return err
120128
}

toolkit/tools/pkg/imagecreatorlib/validation.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ func validateConfig(ctx context.Context, baseConfigPath string, config *imagecus
8181
}
8282

8383
// TODO: Validate for distro and release
84-
err = imagecustomizerlib.ValidateConfig(ctx, baseConfigPath, config,
85-
"", rpmsSources, outputImageFile, outputImageFormat, false, packageSnapshotTime, true)
84+
err = imagecustomizerlib.ValidateConfig(ctx, baseConfigPath, config, true,
85+
imagecustomizerlib.ImageCustomizerOptions{
86+
RpmsSources: rpmsSources,
87+
OutputImageFile: outputImageFile,
88+
OutputImageFormat: outputImageFormat,
89+
PackageSnapshotTime: packageSnapshotTime,
90+
})
8691
if err != nil {
8792
return err
8893
}

toolkit/tools/pkg/imagecustomizerlib/configvalidation.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var (
4040
ErrInvalidPackageSnapshotTime = NewImageCustomizerError("Validation:InvalidPackageSnapshotTime", "invalid command-line option '--package-snapshot-time'")
4141
)
4242

43-
func ValidateConfig(ctx context.Context, baseConfigPath string, config *imagecustomizerapi.Config, inputImageFile string, rpmsSources []string,
44-
outputImageFile, outputImageFormat string, useBaseImageRpmRepos bool, packageSnapshotTime string, newImage bool,
43+
func ValidateConfig(ctx context.Context, baseConfigPath string, config *imagecustomizerapi.Config, newImage bool,
44+
options ImageCustomizerOptions,
4545
) error {
4646
_, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "validate_config")
4747
defer span.End()
@@ -52,7 +52,7 @@ func ValidateConfig(ctx context.Context, baseConfigPath string, config *imagecus
5252
}
5353

5454
if !newImage {
55-
err = validateInput(baseConfigPath, config.Input, inputImageFile)
55+
err = validateInput(baseConfigPath, config.Input, options.InputImageFile)
5656
if err != nil {
5757
return err
5858
}
@@ -63,7 +63,7 @@ func ValidateConfig(ctx context.Context, baseConfigPath string, config *imagecus
6363
return err
6464
}
6565

66-
err = validateOsConfig(baseConfigPath, config.OS, rpmsSources, useBaseImageRpmRepos)
66+
err = validateOsConfig(baseConfigPath, config.OS, options.RpmsSources, options.UseBaseImageRpmRepos)
6767
if err != nil {
6868
return err
6969
}
@@ -73,12 +73,12 @@ func ValidateConfig(ctx context.Context, baseConfigPath string, config *imagecus
7373
return err
7474
}
7575

76-
err = validateOutput(baseConfigPath, config.Output, outputImageFile, outputImageFormat)
76+
err = validateOutput(baseConfigPath, config.Output, options.OutputImageFile, options.OutputImageFormat)
7777
if err != nil {
7878
return err
7979
}
8080

81-
if err := validateSnapshotTimeInput(packageSnapshotTime, config.PreviewFeatures); err != nil {
81+
if err := validateSnapshotTimeInput(options.PackageSnapshotTime, config.PreviewFeatures); err != nil {
8282
return err
8383
}
8484

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ const (
9696
// The value of this string is inserted during compilation via a linker flag.
9797
var ToolVersion = ""
9898

99+
type ImageCustomizerOptions struct {
100+
BuildDir string
101+
InputImageFile string
102+
RpmsSources []string
103+
OutputImageFile string
104+
OutputImageFormat string
105+
UseBaseImageRpmRepos bool
106+
PackageSnapshotTime string
107+
}
108+
99109
type ImageCustomizerParameters struct {
100110
// build dirs
101111
buildDirAbs string
@@ -147,27 +157,24 @@ type verityDeviceMetadata struct {
147157
hashSignaturePath string
148158
}
149159

150-
func createImageCustomizerParameters(ctx context.Context, buildDir string,
151-
inputImageFile string,
152-
configPath string, config *imagecustomizerapi.Config,
153-
useBaseImageRpmRepos bool, rpmsSources []string,
154-
outputImageFormat string, outputImageFile string, packageSnapshotTime string,
160+
func createImageCustomizerParameters(ctx context.Context, configPath string, config *imagecustomizerapi.Config,
161+
options ImageCustomizerOptions,
155162
) (*ImageCustomizerParameters, error) {
156163
_, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "create_image_customizer_parameters")
157164
defer span.End()
158165

159166
ic := &ImageCustomizerParameters{}
160167

161168
// working directories
162-
buildDirAbs, err := filepath.Abs(buildDir)
169+
buildDirAbs, err := filepath.Abs(options.BuildDir)
163170
if err != nil {
164171
return nil, err
165172
}
166173

167174
ic.buildDirAbs = buildDirAbs
168175

169176
// input image
170-
ic.inputImageFile = inputImageFile
177+
ic.inputImageFile = options.InputImageFile
171178
if ic.inputImageFile == "" && config.Input.Image.Path != "" {
172179
ic.inputImageFile = file.GetAbsPathWithBase(configPath, config.Input.Image.Path)
173180
}
@@ -190,10 +197,10 @@ func createImageCustomizerParameters(ctx context.Context, buildDir string,
190197
len(config.Scripts.PostCustomization) > 0 ||
191198
len(config.Scripts.FinalizeCustomization) > 0
192199

193-
ic.useBaseImageRpmRepos = useBaseImageRpmRepos
194-
ic.rpmsSources = rpmsSources
200+
ic.useBaseImageRpmRepos = options.UseBaseImageRpmRepos
201+
ic.rpmsSources = options.RpmsSources
195202

196-
err = ValidateRpmSources(rpmsSources)
203+
err = ValidateRpmSources(options.RpmsSources)
197204
if err != nil {
198205
return nil, err
199206
}
@@ -202,16 +209,16 @@ func createImageCustomizerParameters(ctx context.Context, buildDir string,
202209
ic.rawImageFile = filepath.Join(buildDirAbs, BaseImageName)
203210

204211
// output image
205-
ic.outputImageFormat = imagecustomizerapi.ImageFormatType(outputImageFormat)
212+
ic.outputImageFormat = imagecustomizerapi.ImageFormatType(options.OutputImageFormat)
206213
if err := ic.outputImageFormat.IsValid(); err != nil {
207-
return nil, fmt.Errorf("%w (format='%s'):\n%w", ErrInvalidOutputFormat, outputImageFormat, err)
214+
return nil, fmt.Errorf("%w (format='%s'):\n%w", ErrInvalidOutputFormat, options.OutputImageFormat, err)
208215
}
209216

210217
if ic.outputImageFormat == "" {
211218
ic.outputImageFormat = config.Output.Image.Format
212219
}
213220

214-
ic.outputImageFile = outputImageFile
221+
ic.outputImageFile = options.OutputImageFile
215222
if ic.outputImageFile == "" && config.Output.Image.Path != "" {
216223
ic.outputImageFile = file.GetAbsPathWithBase(configPath, config.Output.Image.Path)
217224
}
@@ -237,7 +244,7 @@ func createImageCustomizerParameters(ctx context.Context, buildDir string,
237244
}
238245
}
239246

240-
ic.packageSnapshotTime = packageSnapshotTime
247+
ic.packageSnapshotTime = options.PackageSnapshotTime
241248

242249
return ic, nil
243250
}
@@ -246,6 +253,18 @@ func CustomizeImageWithConfigFile(ctx context.Context, buildDir string, configFi
246253
rpmsSources []string, outputImageFile string, outputImageFormat string,
247254
useBaseImageRpmRepos bool, packageSnapshotTime string,
248255
) error {
256+
return CustomizeImageWithConfigFileOptions(ctx, configFile, ImageCustomizerOptions{
257+
BuildDir: buildDir,
258+
InputImageFile: inputImageFile,
259+
RpmsSources: rpmsSources,
260+
OutputImageFile: outputImageFile,
261+
OutputImageFormat: outputImageFormat,
262+
UseBaseImageRpmRepos: useBaseImageRpmRepos,
263+
PackageSnapshotTime: packageSnapshotTime,
264+
})
265+
}
266+
267+
func CustomizeImageWithConfigFileOptions(ctx context.Context, configFile string, options ImageCustomizerOptions) error {
249268
var err error
250269

251270
var config imagecustomizerapi.Config
@@ -262,8 +281,7 @@ func CustomizeImageWithConfigFile(ctx context.Context, buildDir string, configFi
262281
return fmt.Errorf("%w:\n%w", ErrGetAbsoluteConfigPath, err)
263282
}
264283

265-
err = CustomizeImage(ctx, buildDir, absBaseConfigPath, &config, inputImageFile, rpmsSources, outputImageFile, outputImageFormat,
266-
useBaseImageRpmRepos, packageSnapshotTime)
284+
err = CustomizeImageOptions(ctx, absBaseConfigPath, &config, options)
267285
if err != nil {
268286
return err
269287
}
@@ -280,13 +298,27 @@ func cleanUp(ic *ImageCustomizerParameters) error {
280298
return nil
281299
}
282300

283-
func CustomizeImage(ctx context.Context, buildDir string, baseConfigPath string, config *imagecustomizerapi.Config, inputImageFile string,
284-
rpmsSources []string, outputImageFile string, outputImageFormat string,
301+
func CustomizeImage(ctx context.Context, buildDir string, baseConfigPath string, config *imagecustomizerapi.Config,
302+
inputImageFile string, rpmsSources []string, outputImageFile string, outputImageFormat string,
285303
useBaseImageRpmRepos bool, packageSnapshotTime string,
304+
) (err error) {
305+
return CustomizeImageOptions(ctx, baseConfigPath, config, ImageCustomizerOptions{
306+
BuildDir: buildDir,
307+
InputImageFile: inputImageFile,
308+
RpmsSources: rpmsSources,
309+
OutputImageFile: outputImageFile,
310+
OutputImageFormat: outputImageFormat,
311+
UseBaseImageRpmRepos: useBaseImageRpmRepos,
312+
PackageSnapshotTime: packageSnapshotTime,
313+
})
314+
}
315+
316+
func CustomizeImageOptions(ctx context.Context, baseConfigPath string, config *imagecustomizerapi.Config,
317+
options ImageCustomizerOptions,
286318
) (err error) {
287319
ctx, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "customize_image")
288320
span.SetAttributes(
289-
attribute.String("output_image_format", string(outputImageFormat)),
321+
attribute.String("output_image_format", string(options.OutputImageFormat)),
290322
)
291323
defer func() {
292324
if err != nil {
@@ -305,14 +337,12 @@ func CustomizeImage(ctx context.Context, buildDir string, baseConfigPath string,
305337
span.End()
306338
}()
307339

308-
err = ValidateConfig(ctx, baseConfigPath, config, inputImageFile, rpmsSources, outputImageFile, outputImageFormat, useBaseImageRpmRepos, packageSnapshotTime, false)
340+
err = ValidateConfig(ctx, baseConfigPath, config, false, options)
309341
if err != nil {
310342
return fmt.Errorf("%w:\n%w", ErrInvalidImageConfig, err)
311343
}
312344

313-
imageCustomizerParameters, err := createImageCustomizerParameters(ctx, buildDir, inputImageFile,
314-
baseConfigPath, config, useBaseImageRpmRepos, rpmsSources,
315-
outputImageFormat, outputImageFile, packageSnapshotTime)
345+
imageCustomizerParameters, err := createImageCustomizerParameters(ctx, baseConfigPath, config, options)
316346
if err != nil {
317347
return fmt.Errorf("%w:\n%w", ErrInvalidParameters, err)
318348
}

0 commit comments

Comments
 (0)