Skip to content
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

windows: Detect Windows architecture from filename #721

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
type cmdRepackWindows struct {
global *cmdGlobal

flagDrivers string
flagWindowsVersion string
flagDrivers string
flagWindowsVersion string
flagWindowsArchitecture string
}

func init() {
Expand Down Expand Up @@ -89,6 +90,7 @@ func (c *cmdRepackWindows) command() *cobra.Command {

cmd.Flags().StringVar(&c.flagDrivers, "drivers", "", "Path to drivers ISO"+"``")
cmd.Flags().StringVar(&c.flagWindowsVersion, "windows-version", "", "Windows version to repack"+"``")
cmd.Flags().StringVar(&c.flagWindowsArchitecture, "windows-arch", "", "Windows architecture to repack"+"``")

return cmd
}
Expand All @@ -113,6 +115,22 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
}
}

if c.flagWindowsArchitecture == "" {
detectedArchitecture := detectWindowsArchitecture(filepath.Base(args[0]))

if detectedArchitecture == "" {
return errors.New("Failed to detect Windows architecture. Please provide the architecture using the --windows-arch flag")
}

c.flagWindowsArchitecture = detectedArchitecture
} else {
supportedArchitectures := []string{"amd64", "ARM64"}

if !lxd.StringInSlice(c.flagWindowsArchitecture, supportedArchitectures) {
return fmt.Errorf("Architecture must be one of %v", supportedArchitectures)
}
}

// Check dependencies
err := c.checkDependencies()
if err != nil {
Expand Down Expand Up @@ -521,7 +539,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error {
"driverName": driver,
}

sourceDir := filepath.Join(driverPath, driver, c.flagWindowsVersion, "amd64")
sourceDir := filepath.Join(driverPath, driver, c.flagWindowsVersion, c.flagWindowsArchitecture)
targetBasePath := filepath.Join(dirs["filerepository"], info.PackageName)

if !lxd.PathExists(targetBasePath) {
Expand Down Expand Up @@ -690,6 +708,23 @@ func detectWindowsVersion(fileName string) string {
return ""
}

func detectWindowsArchitecture(fileName string) string {
aliases := map[string][]string{
"amd64": {"amd64", "x64"},
"ARM64": {"arm64"},
}

for k, v := range aliases {
for _, alias := range v {
if regexp.MustCompile(fmt.Sprintf("(?i)%s", alias)).MatchString(fileName) {
return k
}
}
}

return ""
}

// toHex is a pongo2 filter which converts the provided value to a hex value understood by the Windows registry.
func toHex(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
dst := make([]byte, hex.EncodedLen(len(in.String())))
Expand Down
30 changes: 30 additions & 0 deletions distrobuilder/main_repack-windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,33 @@ func Test_detectWindowsVersion(t *testing.T) {
})
}
}

func Test_detectWindowsArchitecture(t *testing.T) {
type args struct {
fileName string
}

tests := []struct {
name string
args args
want string
}{
{
"Windows 11 (1)",
args{"Win10_22H2_English_x64.iso"},
"amd64",
},
{
"Windows 11 (2)",
args{"Win10_22H2_English_arm64.iso"},
"ARM64",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectWindowsArchitecture(tt.args.fileName)
assert.Equal(t, tt.want, got)
})
}
}