From 18dccf9fb8736e06acf1d39317710d049a306586 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Thu, 20 Mar 2025 20:59:36 +0100 Subject: [PATCH] feat: improve docker installation process by adding wsl feature activation --- internal/dependency/dependency.go | 44 +++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/internal/dependency/dependency.go b/internal/dependency/dependency.go index 6853f31..ee8e0fd 100644 --- a/internal/dependency/dependency.go +++ b/internal/dependency/dependency.go @@ -126,11 +126,26 @@ func installWindowsDependencies(index int, cmd *exec.Cmd, mainWindow *fyne.Windo cmd = tools.CommandInShell("winget", "install", "-e", "--id", "Microsoft.VisualStudioCode") case 1: // Docker Desktop + wslEnabled, err := checkWslFeaturesEnabled() + if err != nil { + return nil, fmt.Errorf("error checking WSL features: %v", err) + } + + if !wslEnabled { + dialog.ShowInformation("activate wsl", "WSL has to be activated. Please confirm to active necessary features.", *mainWindow) + err = enableWslFeatures() + if err != nil { + return nil, fmt.Errorf("error enabling WSL features: %v", err) + } + dialog.ShowInformation("reboot required", "Please reboot your PC to finish wsl activation and reopen 'jws'.", *mainWindow) + return nil, fmt.Errorf("reboot required") + } + wslCheckCmd := tools.CommandInShell("wsl", "--status") - err := wslCheckCmd.Run() + err = wslCheckCmd.Run() if err != nil { wslInstallCmd := tools.CommandInShell("wsl", "--install", "ubuntu") - dialog.ShowInformation("WSL wird installiert", "WSL wird installiert. Bitte warten Sie, bis die Installation abgeschlossen ist und starten Sie die Anwendung neu.", *mainWindow) + dialog.ShowInformation("installing wsl", "WSL will be installed. Please wait untill installation is finished and reopen 'jws'.", *mainWindow) err = wslInstallCmd.Run() if err != nil { errMsg := fmt.Errorf("error: installing WSL: %v", err) @@ -145,6 +160,31 @@ func installWindowsDependencies(index int, cmd *exec.Cmd, mainWindow *fyne.Windo return cmd, nil } +func checkWslFeaturesEnabled() (bool, error) { + cmd := tools.CommandInShell("powershell", "-Command", + "[bool](Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform).State -and [bool](Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux).State") + + output, err := cmd.CombinedOutput() + if err != nil { + return false, fmt.Errorf("error executing PowerShell command: %v\nOutput: %s", err, string(output)) + } + + enabled := strings.TrimSpace(string(output)) == "True" + return enabled, nil +} + +func enableWslFeatures() error { + cmd := tools.CommandInShell("powershell", "-Command", + "Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart; Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart") + + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("error executing PowerShell command: %v\nOutput: %s", err, string(output)) + } + + return nil +} + func installDarwinDependencies(index int, cmd *exec.Cmd, mainWindow *fyne.Window) (*exec.Cmd, error) { brewCheckCmd := tools.CommandInShell("which", "brew") err := brewCheckCmd.Run()