135 lines
3.2 KiB
Go
135 lines
3.2 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/go-ole/go-ole"
|
|
"github.com/go-ole/go-ole/oleutil"
|
|
)
|
|
|
|
func platformInstallIntegration(installDir string) error {
|
|
targetPath := filepath.Join(installDir, "Logof Client.exe")
|
|
workingDir := installDir
|
|
iconPath := filepath.Join(installDir, "assets", "icon.ico")
|
|
|
|
startMenuDir := filepath.Join(
|
|
os.Getenv("ProgramData"),
|
|
"Microsoft",
|
|
"Windows",
|
|
"Start Menu",
|
|
"Programs",
|
|
)
|
|
|
|
shortcutPath := filepath.Join(
|
|
startMenuDir,
|
|
"Logof Client.lnk",
|
|
)
|
|
|
|
if err := os.MkdirAll(startMenuDir, 0755); err != nil {
|
|
return fmt.Errorf("Startmenü-Verzeichnis konnte nicht erstellt werden: %w", err)
|
|
}
|
|
|
|
fmt.Println("Creating Start Menu shortcut...")
|
|
|
|
if err := platformCreateShortcut(
|
|
shortcutPath,
|
|
targetPath,
|
|
workingDir,
|
|
iconPath,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func platformRemoveIntegration() error {
|
|
startMenuDir := filepath.Join(
|
|
os.Getenv("ProgramData"),
|
|
"Microsoft",
|
|
"Windows",
|
|
"Start Menu",
|
|
"Programs",
|
|
)
|
|
|
|
shortcutPath := filepath.Join(
|
|
startMenuDir,
|
|
"Logof Client.lnk",
|
|
)
|
|
|
|
if err := os.Remove(shortcutPath); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("Startmenü-Verknüpfung konnte nicht entfernt werden: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func platformCreateShortcut(
|
|
shortcutPath string,
|
|
targetPath string,
|
|
workingDir string,
|
|
iconPath string,
|
|
) error {
|
|
// COM für den aktuellen Thread initialisieren.
|
|
if err := ole.CoInitialize(0); err != nil {
|
|
return fmt.Errorf("COM konnte nicht initialisiert werden: %w", err)
|
|
}
|
|
defer ole.CoUninitialize()
|
|
|
|
// Windows Scripting Host.
|
|
unknown, err := oleutil.CreateObject("WScript.Shell")
|
|
if err != nil {
|
|
return fmt.Errorf("WScript.Shell konnte nicht erstellt werden: %w", err)
|
|
}
|
|
defer unknown.Release()
|
|
|
|
shell, err := unknown.QueryInterface(ole.IID_IDispatch)
|
|
if err != nil {
|
|
return fmt.Errorf("WScript.Shell IDispatch konnte nicht abgerufen werden: %w", err)
|
|
}
|
|
defer shell.Release()
|
|
|
|
// WScript.Shell.CreateShortcut(...)
|
|
result, err := oleutil.CallMethod(shell, "CreateShortcut", shortcutPath)
|
|
if err != nil {
|
|
return fmt.Errorf("Shortcut-Objekt konnte nicht erstellt werden: %w", err)
|
|
}
|
|
defer result.Clear()
|
|
|
|
shortcut := result.ToIDispatch()
|
|
if shortcut == nil {
|
|
return fmt.Errorf("ungültiges Shortcut-Objekt erhalten")
|
|
}
|
|
defer shortcut.Release()
|
|
|
|
// Zielprogramm.
|
|
if _, err := oleutil.PutProperty(shortcut, "TargetPath", targetPath); err != nil {
|
|
return fmt.Errorf("TargetPath konnte nicht gesetzt werden: %w", err)
|
|
}
|
|
|
|
// Arbeitsverzeichnis.
|
|
if _, err := oleutil.PutProperty(shortcut, "WorkingDirectory", workingDir); err != nil {
|
|
return fmt.Errorf("WorkingDirectory konnte nicht gesetzt werden: %w", err)
|
|
}
|
|
|
|
// Beschreibung.
|
|
if _, err := oleutil.PutProperty(shortcut, "Description", "Logof Client"); err != nil {
|
|
return fmt.Errorf("Description konnte nicht gesetzt werden: %w", err)
|
|
}
|
|
|
|
// Icon.
|
|
if _, err := oleutil.PutProperty(shortcut, "IconLocation", iconPath); err != nil {
|
|
return fmt.Errorf("IconLocation konnte nicht gesetzt werden: %w", err)
|
|
}
|
|
|
|
// Verknüpfung tatsächlich auf die Festplatte schreiben.
|
|
if _, err := oleutil.CallMethod(shortcut, "Save"); err != nil {
|
|
return fmt.Errorf("Shortcut konnte nicht gespeichert werden: %w", err)
|
|
}
|
|
|
|
return nil
|
|
} |