Installer/spm/utils.go

72 lines
1.9 KiB
Go
Raw Normal View History

2024-12-25 10:58:31 +01:00
package spm
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
func GetTempDownloadDir() string {
dir, err := os.MkdirTemp("", "spm_downloads")
if err != nil {
panic(err)
}
return dir
}
func SetDownloadFolder(customDir string) (string, error) {
if err := os.MkdirAll(customDir, os.ModePerm); err != nil {
return "", err
}
return customDir, nil
}
// GetDefaultInstallDir generates the default installation directory based on the OS and environment.
func GetDefaultInstallDir() (string, error) {
var installDir string
switch runtime.GOOS {
case "windows":
// Use %APPDATA% or Program Files on Windows
appData := os.Getenv("APPDATA")
if appData != "" {
installDir = filepath.Join(appData, "Spitfire")
} else {
programFiles := os.Getenv("ProgramFiles")
if programFiles == "" {
return "", fmt.Errorf("unable to determine default install directory on Windows")
}
installDir = filepath.Join(programFiles, "Spitfire")
}
case "darwin":
// Use ~/Library/Application Support on macOS
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unable to determine home directory on macOS: %w", err)
}
installDir = filepath.Join(homeDir, "Library", "Application Support", "Spitfire")
case "linux":
// Use ~/.local/share or /opt on Linux
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unable to determine home directory on Linux: %w", err)
}
installDir = filepath.Join(homeDir, ".local", "share", "Spitfire")
default:
return "", fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
return installDir, nil
}
2025-01-21 23:31:53 +01:00
// DecompressPackage determines the appropriate package format and decompresses it.
func DecompressPackage(downloadDir string) (string, error) {
osName := runtime.GOOS
packagePath := filepath.Join(downloadDir, fmt.Sprintf("browser-amd64-nightly-%s.tar.gz", osName)) // If file naming changes this will break!!
return DecompressToTemp(packagePath)
}