Installer/installer.go
partisan 646851f9f8
All checks were successful
/ test-on-windows (push) Successful in 7s
/ test-on-alpine (push) Successful in 4s
added SPM
2024-12-25 08:46:25 +01:00

105 lines
2.6 KiB
Go

package main
import (
"fmt"
"path/filepath"
"spitfire-installer/spm"
)
// Installer manages the download, decompression, and installation processes.
type Installer struct {
// Progress info from SPM
Progress int
Task string
// Internal states
IsDownloading bool
IsInstalling bool
DoneDownload bool
DoneInstall bool
LastError error
// Paths
DownloadDir string
TempDir string
}
// NewInstaller creates a new Installer with initial state.
func NewInstaller() *Installer {
return &Installer{}
}
// StartDownloadDecompress starts the download and decompression in a background goroutine.
func (inst *Installer) StartDownloadDecompress() {
inst.IsDownloading = true
go func() {
defer func() {
inst.IsDownloading = false
inst.DoneDownload = (inst.LastError == nil)
}()
// Prepare download directory
spm.UpdateProgress(0, "Preparing to download...")
inst.DownloadDir = spm.GetTempDownloadDir()
// Download APPINDEX
appIndexPath := filepath.Join(inst.DownloadDir, "APPINDEX")
spm.UpdateProgress(0, "Downloading APPINDEX")
if err := spm.DownloadAppIndex(appIndexPath); err != nil {
inst.LastError = err
return
}
// Download package
packageName := "spitfire-browser"
release := "nightly"
spm.UpdateProgress(0, "Downloading package...")
if err := spm.DownloadPackageFromAppIndex(appIndexPath, packageName, release, inst.DownloadDir); err != nil {
inst.LastError = err
return
}
// Decompress
spm.UpdateProgress(0, "Decompressing...")
packagePath := filepath.Join(inst.DownloadDir, "browser-amd64-nightly-linux.tar.gz")
tempDir, err := spm.DecompressToTemp(packagePath)
if err != nil {
inst.LastError = err
return
}
inst.TempDir = tempDir
}()
}
// FinalInstall moves files to the final install directory in a background goroutine.
func (inst *Installer) FinalInstall() {
if !inst.DoneDownload {
inst.LastError = fmt.Errorf("Cannot install: download and decompression are not complete")
return
}
inst.IsInstalling = true
go func() {
defer func() {
inst.IsInstalling = false
inst.DoneInstall = (inst.LastError == nil)
}()
// Move files to install directory
spm.UpdateProgress(0, "Installing...")
installDir := "/home/czmisacz/Stažené/spitfire"
if err := spm.MoveFilesToInstallDir(inst.TempDir, installDir); err != nil {
inst.LastError = err
return
}
spm.UpdateProgress(100, "Installation complete!")
}()
}
// PollProgress fetches the latest progress and task from SPM.
func (inst *Installer) PollProgress() {
p, t := spm.GetProgress()
inst.Progress, inst.Task = p, t
}