feat: splitting platforms and maybe fixing shortcut installations as well as admin privileges

This commit is contained in:
2026-09-27 15:52:55 +02:00
parent 061cf03277
commit 231ce0767d
7 changed files with 663 additions and 330 deletions
+217 -329
View File
@@ -9,7 +9,6 @@ import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
@@ -18,11 +17,11 @@ import (
)
const (
appName = "logofclient"
appVersion = "1.0.0"
defaultGitea = "https://git.mypapercloud.de"
defaultOwner = "fierke"
defaultRepo = "logofclient"
appName = "logofclient"
appVersion = "1.0.0"
defaultGitea = "https://git.mypapercloud.de"
defaultOwner = "fierke"
defaultRepo = "logofclient"
)
type Config struct {
@@ -74,33 +73,15 @@ func main() {
os.Exit(1)
}
case "-v", "--version":
fmt.Printf("%s %s\n", appName, appVersion)
case "-h", "--help", "help":
printHelp()
case "-y":
if len(args) < 2 {
if len(args) != 2 {
printHelp()
os.Exit(1)
}
switch args[1] {
case "install":
if err := runPrivileged(config, "install"); err != nil {
printError(err)
os.Exit(1)
}
case "update":
if err := runPrivileged(config, "update"); err != nil {
printError(err)
os.Exit(1)
}
case "uninstall":
if err := runPrivileged(config, "uninstall"); err != nil {
case "install", "update", "uninstall":
if err := runPrivileged(config, args[1]); err != nil {
printError(err)
os.Exit(1)
}
@@ -110,6 +91,12 @@ func main() {
os.Exit(1)
}
case "-v", "--version":
fmt.Printf("%s %s\n", appName, appVersion)
case "-h", "--help", "help":
printHelp()
default:
fmt.Printf("Unbekannter Befehl: %s\n\n", args[0])
printHelp()
@@ -137,14 +124,14 @@ func runTUI(config Config) {
fmt.Print("Auswahl: ")
var choice string
if _, err := fmt.Scanln(&choice); err != nil {
fmt.Println()
return
}
var err error
switch choice {
switch strings.TrimSpace(choice) {
case "1":
err = runPrivileged(config, "install")
@@ -184,7 +171,7 @@ Verwendung:
Optionen:
-h, --help Diese Hilfe anzeigen
-v, --version Version des Installers anzeigen
-y <command> Befehl ohne TUI ausführen
-y <Befehl> Befehl ohne interaktive Abfrage ausführen
Beispiele:
logofclient-installer install
@@ -193,60 +180,37 @@ Beispiele:
logofclient-installer -y install`)
}
// ------------------------------------------------------------
// Privileges
// ------------------------------------------------------------
func runPrivileged(config Config, action string) error {
// Windows:
// Die EXE wird später über ein Manifest mit
// requestedExecutionLevel="requireAdministrator"
// gestartet. Hier ist daher keine Elevation nötig.
if runtime.GOOS != "linux" {
return runAction(config, action)
}
// Linux: bereits root
if os.Geteuid() == 0 {
return runAction(config, action)
}
// Linux: mit sudo neu starten
fmt.Println()
fmt.Println("Für diese Aktion werden Administratorrechte benötigt.")
fmt.Println("Starte Installer mit sudo...")
fmt.Println()
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("Installer-Pfad konnte nicht ermittelt werden: %w", err)
}
cmd := exec.Command("sudo", exe, action)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func runAction(config Config, action string) error {
switch action {
case "install":
return install(config)
case "update":
return update(config)
case "uninstall":
return uninstall(config)
default:
return fmt.Errorf("unbekannte Aktion: %s", action)
}
}
// ------------------------------------------------------------
// Privilegien
// ------------------------------------------------------------
// runPrivileged wird von den Plattformdateien implementiert.
//
// Linux:
// - fragt nach Bestätigung
// - startet sich bei Bedarf über sudo neu
//
// Windows:
// - die EXE wird über das Manifest mit
// requireAdministrator gestartet
// - hier ist keine weitere Elevation nötig
func runPrivileged(config Config, action string) error {
return platformRunPrivileged(config, action)
}
// ------------------------------------------------------------
// Installation
// ------------------------------------------------------------
@@ -260,6 +224,7 @@ func install(config Config) error {
}
tag := release.TagName
if tag == "" {
return errors.New("Release enthält keinen Tag")
}
@@ -268,6 +233,13 @@ func install(config Config) error {
assetName := getAssetName(tag)
if assetName == "" {
return fmt.Errorf(
"kein Paket für Betriebssystem %s definiert",
runtime.GOOS,
)
}
asset, err := findAsset(release, assetName)
if err != nil {
return err
@@ -277,7 +249,10 @@ func install(config Config) error {
tempDir, err := os.MkdirTemp("", "logofclient-installer-*")
if err != nil {
return fmt.Errorf("temporäres Verzeichnis konnte nicht erstellt werden: %w", err)
return fmt.Errorf(
"temporäres Verzeichnis konnte nicht erstellt werden: %w",
err,
)
}
defer os.RemoveAll(tempDir)
@@ -294,16 +269,20 @@ func install(config Config) error {
fmt.Printf("Installing to: %s\n", installDir)
// Alte Installation entfernen
if err := removeIfExists(installDir); err != nil {
return fmt.Errorf("alte Installation konnte nicht entfernt werden: %w", err)
return fmt.Errorf(
"alte Installation konnte nicht entfernt werden: %w",
err,
)
}
if err := os.MkdirAll(installDir, 0755); err != nil {
return fmt.Errorf("Installationsverzeichnis konnte nicht erstellt werden: %w", err)
return fmt.Errorf(
"Installationsverzeichnis konnte nicht erstellt werden: %w",
err,
)
}
// Archiv entpacken
switch runtime.GOOS {
case "linux":
if err := extractTarXz(archivePath, installDir); err != nil {
@@ -316,10 +295,12 @@ func install(config Config) error {
}
default:
return fmt.Errorf("nicht unterstütztes Betriebssystem: %s", runtime.GOOS)
return fmt.Errorf(
"nicht unterstütztes Betriebssystem: %s",
runtime.GOOS,
)
}
// installierte Version speichern
versionFile := filepath.Join(installDir, ".version")
if err := os.WriteFile(
@@ -327,24 +308,22 @@ func install(config Config) error {
[]byte(tag+"\n"),
0644,
); err != nil {
return fmt.Errorf("Version konnte nicht gespeichert werden: %w", err)
return fmt.Errorf(
"Version konnte nicht gespeichert werden: %w",
err,
)
}
// Plattformintegration
switch runtime.GOOS {
case "linux":
if err := installLinuxIntegration(installDir); err != nil {
return err
}
case "windows":
if err := installWindowsIntegration(installDir); err != nil {
return err
}
if err := platformInstallIntegration(installDir); err != nil {
return err
}
fmt.Println()
fmt.Printf("%s %s wurde erfolgreich installiert.\n", appName, tag)
fmt.Printf(
"%s %s wurde erfolgreich installiert.\n",
appName,
tag,
)
return nil
}
@@ -369,7 +348,10 @@ func update(config Config) error {
return install(config)
}
return fmt.Errorf("installierte Version konnte nicht gelesen werden: %w", err)
return fmt.Errorf(
"installierte Version konnte nicht gelesen werden: %w",
err,
)
}
currentVersion := strings.TrimSpace(string(currentVersionData))
@@ -383,8 +365,15 @@ func update(config Config) error {
latestVersion := release.TagName
fmt.Printf("Installierte Version: %s\n", valueOrNone(currentVersion))
fmt.Printf("Neueste Version: %s\n", valueOrNone(latestVersion))
fmt.Printf(
"Installierte Version: %s\n",
valueOrNone(currentVersion),
)
fmt.Printf(
"Neueste Version: %s\n",
valueOrNone(latestVersion),
)
if currentVersion == latestVersion {
fmt.Println()
@@ -393,18 +382,21 @@ func update(config Config) error {
}
fmt.Println()
fmt.Printf("Update von %s auf %s...\n", currentVersion, latestVersion)
fmt.Printf(
"Update von %s auf %s...\n",
currentVersion,
latestVersion,
)
// install() entfernt die alte Version selbst.
return install(config)
}
// ------------------------------------------------------------
// Uninstall
// Deinstallation
// ------------------------------------------------------------
func uninstall(config Config) error {
_ = config // aktuell nicht benötigt
_ = config
installDir, err := getInstallDir()
if err != nil {
@@ -418,20 +410,15 @@ func uninstall(config Config) error {
fmt.Printf("Deinstalliere %s...\n", appName)
switch runtime.GOOS {
case "linux":
if err := removeLinuxIntegration(); err != nil {
return err
}
case "windows":
if err := removeWindowsIntegration(); err != nil {
return err
}
if err := platformRemoveIntegration(); err != nil {
return err
}
if err := removeIfExists(installDir); err != nil {
return fmt.Errorf("Installation konnte nicht entfernt werden: %w", err)
return fmt.Errorf(
"Installation konnte nicht entfernt werden: %w",
err,
)
}
fmt.Println("Logof Client wurde erfolgreich deinstalliert.")
@@ -453,12 +440,17 @@ func getLatestRelease(config Config) (*Release, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Gitea Release konnte nicht abgerufen werden: %w", err)
return nil, fmt.Errorf(
"Gitea Release konnte nicht abgerufen werden: %w",
err,
)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
body, _ := io.ReadAll(
io.LimitReader(resp.Body, 4096),
)
return nil, fmt.Errorf(
"Gitea API antwortete mit HTTP %d: %s",
@@ -470,7 +462,10 @@ func getLatestRelease(config Config) (*Release, error) {
var release Release
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return nil, fmt.Errorf("Gitea-Antwort konnte nicht gelesen werden: %w", err)
return nil, fmt.Errorf(
"Gitea-Antwort konnte nicht gelesen werden: %w",
err,
)
}
return &release, nil
@@ -493,10 +488,18 @@ func findAsset(release *Release, name string) (*Asset, error) {
func getAssetName(tag string) string {
switch runtime.GOOS {
case "linux":
return fmt.Sprintf("%s-%s-linux.tar.xz", appName, tag)
return fmt.Sprintf(
"%s-%s-linux.tar.xz",
appName,
tag,
)
case "windows":
return fmt.Sprintf("%s-%s-win.zip", appName, tag)
return fmt.Sprintf(
"%s-%s-win.zip",
appName,
tag,
)
default:
return ""
@@ -506,7 +509,10 @@ func getAssetName(tag string) string {
func download(url string, destination string) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("Download fehlgeschlagen: %w", err)
return fmt.Errorf(
"Download fehlgeschlagen: %w",
err,
)
}
defer resp.Body.Close()
@@ -519,84 +525,16 @@ func download(url string, destination string) error {
file, err := os.Create(destination)
if err != nil {
return fmt.Errorf("Download-Datei konnte nicht erstellt werden: %w", err)
return fmt.Errorf(
"Download-Datei konnte nicht erstellt werden: %w",
err,
)
}
defer file.Close()
if _, err := io.Copy(file, resp.Body); err != nil {
return fmt.Errorf("Download fehlgeschlagen: %w", err)
}
return nil
}
// ------------------------------------------------------------
// Linux
// ------------------------------------------------------------
func getInstallDir() (string, error) {
switch runtime.GOOS {
case "linux":
return "/opt/logofclient", nil
case "windows":
programFiles := os.Getenv("ProgramFiles")
if programFiles == "" {
return "", errors.New("ProgramFiles ist nicht gesetzt")
}
return filepath.Join(programFiles, "logofclient"), nil
default:
return "", fmt.Errorf("nicht unterstütztes Betriebssystem: %s", runtime.GOOS)
}
}
func installLinuxIntegration(installDir string) error {
binaryPath := filepath.Join(installDir, "Logof Client")
linkPath := "/usr/local/bin/logofclient"
fmt.Println("Creating command symlink...")
if err := removeIfExists(linkPath); err != nil {
return fmt.Errorf("alter Symlink konnte nicht entfernt werden: %w", err)
}
if err := os.Symlink(binaryPath, linkPath); err != nil {
return fmt.Errorf("Symlink konnte nicht erstellt werden: %w", err)
}
desktopDir := "/usr/share/applications"
desktopFile := filepath.Join(desktopDir, "logofclient.desktop")
if err := os.MkdirAll(desktopDir, 0755); err != nil {
return fmt.Errorf("Desktop-Verzeichnis konnte nicht erstellt werden: %w", err)
}
iconPath := filepath.Join(
installDir,
"assets",
"icon.png",
)
desktopEntry := fmt.Sprintf(`[Desktop Entry]
Name=Logof Client
Comment=Logof Client
Exec=%s
Terminal=false
Type=Application
Icon=%s
Categories=Office;
`, linkPath, iconPath)
if err := os.WriteFile(
desktopFile,
[]byte(desktopEntry),
0644,
); err != nil {
return fmt.Errorf(
"Desktop-Datei konnte nicht geschrieben werden: %w",
"Download fehlgeschlagen: %w",
err,
)
}
@@ -604,135 +542,30 @@ Categories=Office;
return nil
}
func removeLinuxIntegration() error {
linkPath := "/usr/local/bin/logofclient"
desktopFile := "/usr/share/applications/logofclient.desktop"
if err := removeIfExists(linkPath); err != nil {
return fmt.Errorf("Symlink konnte nicht entfernt werden: %w", err)
}
if err := removeIfExists(desktopFile); err != nil {
return fmt.Errorf("Desktop-Datei konnte nicht entfernt werden: %w", err)
}
return nil
}
// ------------------------------------------------------------
// Windows
// Archive
// ------------------------------------------------------------
func installWindowsIntegration(installDir string) error {
startMenu := filepath.Join(
os.Getenv("ProgramData"),
"Microsoft",
"Windows",
"Start Menu",
"Programs",
)
if err := os.MkdirAll(startMenu, 0755); err != nil {
return fmt.Errorf(
"Startmenü-Verzeichnis konnte nicht erstellt werden: %w",
err,
)
}
shortcut := filepath.Join(
startMenu,
"Logof Client.lnk",
)
target := filepath.Join(
installDir,
"Logof Client.exe",
)
icon := filepath.Join(
installDir,
"assets",
"icon.ico",
)
// PowerShell wird hier nur intern zur Erstellung der Windows-
// Shell-Verknüpfung verwendet. Der Benutzer muss kein
// PowerShell-Skript ausführen.
script := `
$ws = New-Object -ComObject WScript.Shell
$sc = $ws.CreateShortcut($args[0])
$sc.TargetPath = $args[1]
$sc.WorkingDirectory = $args[2]
$sc.Description = "Logof Client"
if (Test-Path $args[3]) {
$sc.IconLocation = $args[3]
}
$sc.Save()
`
cmd := exec.Command(
"powershell.exe",
"-NoProfile",
"-NonInteractive",
"-Command",
script,
shortcut,
target,
installDir,
icon,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf(
"Startmenü-Verknüpfung konnte nicht erstellt werden: %w",
err,
)
}
return nil
}
func removeWindowsIntegration() error {
startMenu := filepath.Join(
os.Getenv("ProgramData"),
"Microsoft",
"Windows",
"Start Menu",
"Programs",
)
shortcut := filepath.Join(
startMenu,
"Logof Client.lnk",
)
if err := removeIfExists(shortcut); err != nil {
return fmt.Errorf(
"Startmenü-Verknüpfung konnte nicht entfernt werden: %w",
err,
)
}
return nil
}
// ------------------------------------------------------------
// Archive extraction
// ------------------------------------------------------------
func extractTarXz(archivePath string, destination string) error {
func extractTarXz(
archivePath string,
destination string,
) error {
file, err := os.Open(archivePath)
if err != nil {
return fmt.Errorf("Archiv konnte nicht geöffnet werden: %w", err)
return fmt.Errorf(
"Archiv konnte nicht geöffnet werden: %w",
err,
)
}
defer file.Close()
xzReader, err := xz.NewReader(file)
if err != nil {
return fmt.Errorf("XZ-Archiv konnte nicht gelesen werden: %w", err)
return fmt.Errorf(
"XZ-Archiv konnte nicht gelesen werden: %w",
err,
)
}
tarReader := tar.NewReader(xzReader)
@@ -745,17 +578,26 @@ func extractTarXz(archivePath string, destination string) error {
}
if err != nil {
return fmt.Errorf("TAR-Archiv konnte nicht gelesen werden: %w", err)
return fmt.Errorf(
"TAR-Archiv konnte nicht gelesen werden: %w",
err,
)
}
target, err := safeJoin(destination, header.Name)
target, err := safeJoin(
destination,
header.Name,
)
if err != nil {
return err
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, os.FileMode(header.Mode)); err != nil {
if err := os.MkdirAll(
target,
os.FileMode(header.Mode),
); err != nil {
return fmt.Errorf(
"Verzeichnis %s konnte nicht erstellt werden: %w",
target,
@@ -764,7 +606,10 @@ func extractTarXz(archivePath string, destination string) error {
}
case tar.TypeReg:
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
if err := os.MkdirAll(
filepath.Dir(target),
0755,
); err != nil {
return err
}
@@ -781,7 +626,11 @@ func extractTarXz(archivePath string, destination string) error {
)
}
_, copyErr := io.Copy(out, tarReader)
_, copyErr := io.Copy(
out,
tarReader,
)
closeErr := out.Close()
if copyErr != nil {
@@ -797,7 +646,10 @@ func extractTarXz(archivePath string, destination string) error {
}
case tar.TypeSymlink:
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
if err := os.MkdirAll(
filepath.Dir(target),
0755,
); err != nil {
return err
}
@@ -805,9 +657,10 @@ func extractTarXz(archivePath string, destination string) error {
return err
}
linkTarget := header.Linkname
if err := os.Symlink(linkTarget, target); err != nil {
if err := os.Symlink(
header.Linkname,
target,
); err != nil {
return fmt.Errorf(
"Symlink %s konnte nicht erstellt werden: %w",
target,
@@ -826,21 +679,33 @@ func extractTarXz(archivePath string, destination string) error {
return nil
}
func extractZip(archivePath string, destination string) error {
func extractZip(
archivePath string,
destination string,
) error {
reader, err := zip.OpenReader(archivePath)
if err != nil {
return fmt.Errorf("ZIP-Archiv konnte nicht geöffnet werden: %w", err)
return fmt.Errorf(
"ZIP-Archiv konnte nicht geöffnet werden: %w",
err,
)
}
defer reader.Close()
for _, file := range reader.File {
target, err := safeJoin(destination, file.Name)
target, err := safeJoin(
destination,
file.Name,
)
if err != nil {
return err
}
if file.FileInfo().IsDir() {
if err := os.MkdirAll(target, 0755); err != nil {
if err := os.MkdirAll(
target,
0755,
); err != nil {
return fmt.Errorf(
"Verzeichnis %s konnte nicht erstellt werden: %w",
target,
@@ -851,7 +716,10 @@ func extractZip(archivePath string, destination string) error {
continue
}
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
if err := os.MkdirAll(
filepath.Dir(target),
0755,
); err != nil {
return err
}
@@ -901,10 +769,13 @@ func extractZip(archivePath string, destination string) error {
}
// ------------------------------------------------------------
// Helpers
// Hilfsfunktionen
// ------------------------------------------------------------
func safeJoin(base string, name string) (string, error) {
func safeJoin(
base string,
name string,
) (string, error) {
if filepath.IsAbs(name) {
return "", fmt.Errorf(
"unsicherer Archivpfad: %s",
@@ -915,22 +786,34 @@ func safeJoin(base string, name string) (string, error) {
cleanName := filepath.Clean(name)
if cleanName == ".." ||
strings.HasPrefix(cleanName, ".."+string(os.PathSeparator)) {
strings.HasPrefix(
cleanName,
".."+string(os.PathSeparator),
) {
return "", fmt.Errorf(
"unsicherer Archivpfad: %s",
name,
)
}
target := filepath.Join(base, cleanName)
target := filepath.Join(
base,
cleanName,
)
rel, err := filepath.Rel(base, target)
rel, err := filepath.Rel(
base,
target,
)
if err != nil {
return "", err
}
if rel == ".." ||
strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
strings.HasPrefix(
rel,
".."+string(os.PathSeparator),
) {
return "", fmt.Errorf(
"unsicherer Archivpfad: %s",
name,
@@ -950,8 +833,13 @@ func removeIfExists(path string) error {
return nil
}
func envOrDefault(name string, fallback string) string {
value := strings.TrimSpace(os.Getenv(name))
func envOrDefault(
name string,
fallback string,
) string {
value := strings.TrimSpace(
os.Getenv(name),
)
if value == "" {
return fallback
@@ -970,4 +858,4 @@ func valueOrNone(value string) string {
func printError(err error) {
fmt.Printf("\nERROR: %v\n", err)
}
}