Skip to content

Commit

Permalink
Merge pull request #721 from monstermunchkin/issues/705-virtio
Browse files Browse the repository at this point in the history
windows: Detect Windows architecture from filename
  • Loading branch information
stgraber authored Jun 13, 2023
2 parents d1477e2 + da75f56 commit 697e6e2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
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)
})
}
}

0 comments on commit 697e6e2

Please sign in to comment.