jws/internal/dependency/dependency.go
Patryk Hegenberg 09b1054588 feat: initial project commit
commit to push first working snapshot to codeberg
2025-03-15 00:13:20 +01:00

315 lines
9.2 KiB
Go

package dependency
import (
"fmt"
osinfo "jws/internal/os"
"jws/pkg/download"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"fyne.io/tools"
)
type Dependency struct {
Name string
Installed bool
Icon fyne.Resource
}
func CheckDependencies(dependencies []Dependency) {
// Check VSCode
dependencies[0].Installed = checkVSCode()
// Check Docker
if runtime.GOOS == "windows" {
dependencies[1].Installed = checkDockerDesktop()
} else {
dependencies[1].Installed = checkDocker()
}
}
func checkVSCode() bool {
switch runtime.GOOS {
case "windows":
_, err := os.Stat(filepath.Join(os.Getenv("LOCALAPPDATA"), "Programs", "Microsoft VS Code", "Code.exe"))
if err == nil {
return true
}
_, err = os.Stat(filepath.Join(os.Getenv("ProgramFiles"), "Microsoft VS Code", "Code.exe"))
return err == nil
case "darwin":
_, err := os.Stat("/Applications/Visual Studio Code.app")
if err != nil {
cmd := tools.CommandInShell("which", "code")
return cmd.Run() == nil
}
return err == nil
case "linux":
cmd := tools.CommandInShell("which", "code")
return cmd.Run() == nil
default:
return false
}
}
func checkDocker() bool {
cmd := tools.CommandInShell("which", "docker")
return cmd.Run() == nil
}
func checkDockerDesktop() bool {
switch runtime.GOOS {
case "windows":
_, err := os.Stat(filepath.Join(os.Getenv("ProgramFiles"), "Docker", "Docker", "Docker Desktop.exe"))
return err == nil
case "darwin":
_, err := os.Stat("/Applications/Docker.app")
return err == nil
case "linux":
cmd := tools.CommandInShell("systemctl", "is-active", "docker")
output, _ := cmd.Output()
return strings.TrimSpace(string(output)) == "active"
default:
return false
}
}
func InstallDependency(index int, sudoPassword string, dependencies []Dependency, mainWindow fyne.Window) {
depName := dependencies[index].Name
var cmd *exec.Cmd
var err error
switch runtime.GOOS {
case "windows":
switch index {
case 0: // VSCode
cmd = tools.CommandInShell("winget", "install", "-e", "--id",
"Microsoft.VisualStudioCode")
case 1: // Docker Desktop
wslCheckCmd := tools.CommandInShell("wsl", "--status")
err := wslCheckCmd.Run()
if err != nil {
wslInstallCmd := tools.CommandInShell("wsl", "--install")
err = wslInstallCmd.Run()
if err != nil {
dialog.ShowError(fmt.Errorf("error: installing WSL: %v", err), mainWindow)
return
}
dialog.ShowInformation("WSL wird installiert", "WSL wird installiert. Bitte warten Sie, bis die Installation abgeschlossen ist und starten Sie die Anwendung neu.", mainWindow)
return
}
cmd = tools.CommandInShell("winget", "install", "-e", "--id",
"Docker.DockerDesktop")
}
case "darwin":
brewCheckCmd := tools.CommandInShell("which", "brew")
err := brewCheckCmd.Run()
if err != nil {
brewInstallCmd := tools.CommandInShell("bin/bash", "-c", "\"$(curl -fsSl https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"")
err := brewInstallCmd.Run()
if err != nil {
dialog.ShowError(fmt.Errorf("error: installing homebrew falied: %v", err), mainWindow)
}
}
switch index {
case 0: // VSCode
cmd = tools.CommandInShell("brew", "install", "--cask", "visual-studio-code")
case 1: // Docker
cmd = tools.CommandInShell("brew", "install", "docker")
}
case "linux":
osInfo, err := osinfo.GetLinuxDistribution()
if err != nil {
log.Println(err)
dialog.ShowError(fmt.Errorf("error getting OS info: %v", err), mainWindow)
return
}
var downloadURL, fileName string
switch index {
case 0: // VSCode
switch osInfo.ID {
case "debian", "ubuntu", "linuxmint":
downloadURL = "https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64"
fileName = "vscode.deb"
case "fedora":
downloadURL = "https://code.visualstudio.com/sha/download?build=stable&os=linux-rpm-x64"
fileName = "vscode.rpm"
default:
dialog.ShowInformation("Nicht unterstützt", fmt.Sprintf("Automatische Installation für dieses OS %v nicht verfügbar", osInfo), mainWindow)
return
}
go func() {
progressBar := widget.NewProgressBar()
progressDialog := dialog.NewCustomWithoutButtons("Download in progress", progressBar, mainWindow)
progressDialog.Show()
homeDir, _ := os.UserHomeDir()
downloadDir := filepath.Join(homeDir, "Downloads")
filePath := filepath.Join(downloadDir, fileName)
err := download.WithProgressBar(downloadURL, filePath, progressBar)
progressDialog.Hide()
if err != nil {
dialog.ShowError(err, mainWindow)
return
}
var installCmd string
switch osInfo.ID {
case "debian", "ubuntu", "linuxmint":
installCmd = fmt.Sprintf("sudo -S dpkg -i %s", filePath)
case "fedora":
installCmd = fmt.Sprintf("sudo -S dnf install -y %s", filePath)
}
cmd := exec.Command("sh", "-c", installCmd)
cmd.Stdin = strings.NewReader(sudoPassword + "\n")
dialog.ShowInformation("Installation gestartet",
"Die Installation von VSCode wurde gestartet.", mainWindow)
output, err := cmd.CombinedOutput()
if err != nil {
dialog.ShowError(fmt.Errorf("installation failed:\n%s", output), mainWindow)
return
} else {
dialog.ShowInformation("Erfolg", "VSCode erfolgreich installiert!", mainWindow)
CheckDependencies(dependencies)
}
}()
case 1: // Docker
go func() {
progressBar := widget.NewProgressBar()
progressDialog := dialog.NewCustomWithoutButtons("Docker Installation läuft...", progressBar, mainWindow)
progressDialog.Show()
osInfo, err := osinfo.GetLinuxDistribution()
if err != nil {
progressDialog.Hide()
dialog.ShowError(fmt.Errorf("error getting os infos: %v", err), mainWindow)
return
}
var commands []string
var cleanupCommands []string
var totalSteps int
switch osInfo.ID {
case "ubuntu", "linuxmint", "debian":
// Ubuntu/Debian Commands
distroPath := "ubuntu"
codeName := "$(. /etc/os-release && echo \"${UBUNTU_CODENAME:-$VERSION_CODENAME}\")"
if osInfo.ID == "debian" {
distroPath = "debian"
codeName = "$(. /etc/os-release && echo \"$VERSION_CODENAME\")"
}
arch := "$(dpkg --print-architecture)"
commands = []string{
"apt-get update",
"apt-get install -y wget",
fmt.Sprintf("wget -qO- https://download.docker.com/linux/%s/dists/%s/pool/stable/%s/ | grep -oP 'href=\"\\K[^\"]*(?=.*deb)' | xargs -I{} wget https://download.docker.com/linux/%s/dists/%s/pool/stable/%s/{}",
distroPath, codeName, arch, distroPath, codeName, arch),
"dpkg -i ./containerd.io*.deb docker-ce*.deb docker-ce-cli*.deb docker-buildx-plugin*.deb docker-compose-plugin*.deb",
"apt-get install -f -y",
"service docker start",
}
cleanupCommands = []string{
"rm -rf ./containerd.io*.deb ./docker-ce*.deb ./docker-ce-cli*.deb ./docker-buildx-plugin*.deb ./docker-compose-plugin*.deb",
"apt-get autoremove -y",
"apt-get clean",
}
totalSteps = len(commands) + len(cleanupCommands)
case "fedora":
// Fedora Commands
fedoraVer := "$(rpm -E %fedora)"
commands = []string{
"dnf install -y wget",
fmt.Sprintf("wget https://download.docker.com/linux/fedora/%s/x86_64/stable/Packages/containerd-*.rpm docker-*.rpm docker-ce-*.rpm", fedoraVer),
"dnf install -y ./*.rpm",
"systemctl enable --now docker",
}
cleanupCommands = []string{
"rm -rf ./containerd-*.rpm ./docker-*.rpm ./docker-ce-*.rpm",
"dnf autoremove -y",
"dnf clean all",
}
totalSteps = len(commands) + len(cleanupCommands)
default:
progressDialog.Hide()
dialog.ShowInformation("not supported", "Automatic Docker installation not supported for your OS.", mainWindow)
return
}
progressStep := 1.0 / float64(totalSteps)
currentProgress := 0.0
for _, cmd := range commands {
command := exec.Command("sudo", "-S", "sh", "-c", cmd)
command.Stdin = strings.NewReader(sudoPassword + "\n")
if output, err := command.CombinedOutput(); err != nil {
progressDialog.Hide()
dialog.ShowError(fmt.Errorf("error at %s:\n%s", cmd, output), mainWindow)
return
}
currentProgress += progressStep
progressBar.SetValue(currentProgress)
}
for _, cmd := range cleanupCommands {
command := exec.Command("sudo", "-S", "sh", "-c", cmd)
command.Stdin = strings.NewReader(sudoPassword + "\n")
if output, err := command.CombinedOutput(); err != nil {
progressDialog.Hide()
dialog.ShowError(fmt.Errorf("cleanup error at %s:\n%s", cmd, output), mainWindow)
return
}
currentProgress += progressStep
progressBar.SetValue(currentProgress)
}
progressDialog.Hide()
dialog.ShowInformation(
"Installation finished",
"Docker was succesfully installed! Please re-start your system to let the changes take effect.",
mainWindow,
)
CheckDependencies(dependencies)
}()
}
}
if cmd != nil {
err = cmd.Start()
if err != nil {
dialog.ShowError(fmt.Errorf("error starting installation process: %v", err), mainWindow)
} else {
dialog.ShowInformation("Installation started",
fmt.Sprintf("Installation of %s was started", depName), mainWindow)
}
}
}