-
Notifications
You must be signed in to change notification settings - Fork 9
Add distribution validation with structured error handling #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
008f7fd
151c9da
2703009
a9234f5
68876f3
7e3ef1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi" | ||
| ) | ||
|
|
||
| // distribution represents a supported Linux distribution and version combination | ||
| type distribution struct { | ||
| name string | ||
| version string | ||
| } | ||
|
|
||
| // GetDistribution validates and returns a distribution from the CLI args | ||
| func (c *ImageCreatorCmd) getDistribution() (*distribution, error) { | ||
| dist := &distribution{ | ||
| name: c.Distro, | ||
| version: c.DistroVersion, | ||
| } | ||
| if err := dist.validate(); err != nil { | ||
| return nil, err | ||
| } | ||
| return dist, nil | ||
| } | ||
|
|
||
| // Validate ensures the distribution and version combination is supported | ||
| func (d *distribution) validate() error { | ||
| // Get supported versions for this distribution | ||
|
|
||
| supportedDistros := imagecustomizerapi.GetSupportedDistros() | ||
| validVersions, exists := supportedDistros[d.name] | ||
| if !exists { | ||
| distros := make([]string, 0, len(supportedDistros)) | ||
| for d := range supportedDistros { | ||
| distros = append(distros, d) | ||
| } | ||
| return fmt.Errorf("unsupported distribution %q. Supported distributions are: %s", | ||
| d.name, strings.Join(distros, ", ")) | ||
| } | ||
|
|
||
| // Validate version | ||
| for _, v := range validVersions { | ||
| if v == d.version { | ||
| return nil | ||
| } | ||
| } | ||
| return fmt.Errorf("unsupported version %q for distribution %q. Supported versions: %s", | ||
himaja-kesari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| d.version, d.name, strings.Join(validVersions, ", ")) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ type ImageCreatorCmd struct { | |
| OutputImageFile string `name:"output-image-file" help:"Path to write the customized image to."` | ||
| OutputImageFormat string `name:"output-image-format" placeholder:"(vhd|vhd-fixed|vhdx|qcow2|raw)" help:"Format of output image." enum:"${imageformat}" default:""` | ||
| Distro string `name:"distro" help:"Target distribution for the image." enum:"azurelinux,fedora" default:"azurelinux"` | ||
|
||
| DistroVersion string `name:"distro-version" help:"Target distribution version (e.g., 3.0 for Azure Linux, 42 for Fedora)." default:""` | ||
| DistroVersion string `name:"distro-version" help:"Target distribution version (e.g., 3.0 for Azure Linux, 42 for Fedora)." default:"3.0"` | ||
| exekong.LogFlags | ||
| PackageSnapshotTime string `name:"package-snapshot-time" help:"Only packages published before this snapshot time will be available during customization. Supports 'YYYY-MM-DD' or full RFC3339 timestamp (e.g., 2024-05-20T23:59:59Z)."` | ||
| } | ||
|
|
@@ -54,8 +54,13 @@ func main() { | |
|
|
||
| logger.InitBestEffort(ptrutils.PtrTo(cli.LogFlags.AsLoggerFlags())) | ||
|
|
||
| err := imagecreatorlib.CreateImageWithConfigFile(ctx, cli.BuildDir, cli.ConfigFile, cli.RpmSources, | ||
| cli.ToolsTar, cli.OutputImageFile, cli.OutputImageFormat, cli.Distro, cli.DistroVersion, | ||
| dist, err := cli.getDistribution() | ||
| if err != nil { | ||
| log.Fatalf("invalid distribution arguments: %v", err) | ||
| } | ||
|
|
||
| err = imagecreatorlib.CreateImageWithConfigFile(ctx, cli.BuildDir, cli.ConfigFile, cli.RpmSources, | ||
|
||
| cli.ToolsTar, cli.OutputImageFile, cli.OutputImageFormat, dist.name, dist.version, | ||
| cli.PackageSnapshotTime) | ||
| if err != nil { | ||
| log.Fatalf("image creation failed:\n%v", err) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package imagecustomizerapi | ||
|
|
||
| const ( | ||
himaja-kesari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DistroNameAzureLinux string = "azurelinux" | ||
| DistroNameFedora string = "fedora" | ||
| ) | ||
|
|
||
| func GetSupportedDistros() map[string][]string { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image Customizer and Image Creator will likely have different support lists. So, it would be good to give this a name unique to Image Creator. One option would be to create an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. for now, this applies to both tools. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fedora isn't supported in Image Customizer yet. Also, the |
||
| // supportedDistros defines valid distribution and version combinations | ||
| return map[string][]string{ | ||
| DistroNameAzureLinux: {"2.0", "3.0"}, | ||
| DistroNameFedora: {"42"}, | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.