Init
This commit is contained in:
commit
d9dae02ffc
17 changed files with 1742 additions and 0 deletions
224
spm/utils.go
Normal file
224
spm/utils.go
Normal file
|
@ -0,0 +1,224 @@
|
|||
package spm
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// global mutable variable to store the chosen install directory
|
||||
var (
|
||||
mu sync.Mutex
|
||||
installedDir string
|
||||
envVar = "SPITFIRE_INSTALL_DIR" // Environment variable name
|
||||
)
|
||||
|
||||
// SetInstallDir sets the global install directory variable and updates the persistent environment variable.
|
||||
func SetInstallDir(path string) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
installedDir = path
|
||||
|
||||
// Persist the environment variable on Windows
|
||||
if runtime.GOOS == "windows" {
|
||||
err := persistSystemEnvVar(envVar, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// For non-Windows platforms, just set it in the current process environment
|
||||
err := os.Setenv(envVar, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInstallDir returns the currently set install directory if available;
|
||||
// otherwise, it calls GetDefaultInstallDir() and sets that.
|
||||
func GetInstallDir() (string, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// If already set, return it
|
||||
if installedDir != "" {
|
||||
return installedDir, nil
|
||||
}
|
||||
|
||||
// Check if it's stored in the system environment variable
|
||||
if envDir := os.Getenv(envVar); envDir != "" {
|
||||
installedDir = envDir
|
||||
return installedDir, nil
|
||||
}
|
||||
|
||||
// Compute and store the default directory if not already set
|
||||
defDir, err := GetDefaultInstallDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
installedDir = defDir
|
||||
|
||||
// Persist the default directory as an environment variable on Windows
|
||||
if runtime.GOOS == "windows" {
|
||||
_ = persistSystemEnvVar(envVar, defDir)
|
||||
} else {
|
||||
_ = os.Setenv(envVar, defDir)
|
||||
}
|
||||
|
||||
return defDir, nil
|
||||
}
|
||||
|
||||
// persistSystemEnvVar sets a persistent environment variable on Windows using the `setx` command.
|
||||
func persistSystemEnvVar(key, value string) error {
|
||||
cmd := exec.Command("cmd", "/C", "setx", key, value)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDefaultInstallDir generates the default installation directory
|
||||
// based on the OS and environment, then also sets it via SetInstallDir.
|
||||
func GetDefaultInstallDir() (string, error) {
|
||||
var installDir string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
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/Spitfire 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)
|
||||
}
|
||||
|
||||
// Also store it globally so future calls to GetInstallDir() return the same
|
||||
SetInstallDir(installDir)
|
||||
return installDir, nil
|
||||
}
|
||||
|
||||
// SetDownloadFolder ensures customDir exists, returns it
|
||||
func SetDownloadFolder(customDir string) (string, error) {
|
||||
if err := os.MkdirAll(customDir, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return customDir, nil
|
||||
}
|
||||
|
||||
// IsMatchingEntry checks if a package entry matches the requested specs
|
||||
func IsMatchingEntry(e AppIndexEntry, name, release, arch, osName, pkgType string) bool {
|
||||
return e.Name == name &&
|
||||
e.Release == release &&
|
||||
e.Arch == arch &&
|
||||
e.OS == osName &&
|
||||
e.Type == pkgType
|
||||
}
|
||||
|
||||
// DecompressPackage determines the package format and decompresses it
|
||||
// DecompressPackage: uses a consistent folder name based on "expectedFileName".
|
||||
func DecompressPackage(downloadDir, packageName, arch, osName, pkgType, release, version string) (string, error) {
|
||||
// 1) Construct the .tar.gz name
|
||||
expectedFileName := fmt.Sprintf(
|
||||
"%s@%s@%s@%s@%s@%s.tar.gz",
|
||||
packageName, arch, osName, pkgType, release, version,
|
||||
)
|
||||
packagePath := filepath.Join(downloadDir, expectedFileName)
|
||||
|
||||
// Check that file exists
|
||||
if _, err := os.Stat(packagePath); os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("package file not found: %s", packagePath)
|
||||
}
|
||||
|
||||
// 2) Build the folder path (minus ".tar.gz")
|
||||
folderName := strings.TrimSuffix(expectedFileName, ".tar.gz")
|
||||
tempDir := GetTempDir() // e.g. C:\Users\<User>\AppData\Local\Temp\spm_temp_164326
|
||||
decompressDir := filepath.Join(tempDir, folderName)
|
||||
|
||||
// Ensure the folder
|
||||
if err := os.MkdirAll(decompressDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create decompressDir: %w", err)
|
||||
}
|
||||
|
||||
// 3) Decompress everything into `decompressDir`
|
||||
if err := decompressTarGz(packagePath, decompressDir); err != nil {
|
||||
return "", fmt.Errorf("failed to decompress: %w", err)
|
||||
}
|
||||
|
||||
// Return the folder path we used
|
||||
return decompressDir, nil
|
||||
}
|
||||
|
||||
func decompressTarGz(srcFile, destDir string) error {
|
||||
f, err := os.Open(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
gzr, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer gzr.Close()
|
||||
|
||||
tarReader := tar.NewReader(gzr)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outPath := filepath.Join(destDir, header.Name)
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if err := os.MkdirAll(outPath, os.FileMode(header.Mode)); err != nil {
|
||||
return err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
outFile, err := os.Create(outPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(outFile, tarReader)
|
||||
outFile.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
// handle symlinks etc. if needed
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue