55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/vlog"
|
|
)
|
|
|
|
func exeDir() string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return "."
|
|
}
|
|
return filepath.Dir(exe)
|
|
}
|
|
|
|
// notifyUser: best-effort balloon via PowerShell (no extra deps). Always logs.
|
|
func notifyUser(title, body string) {
|
|
vlog.Printf("notify: %s — %s", title, body)
|
|
if os.Getenv("VERAE_NO_NOTIFY") == "1" {
|
|
return
|
|
}
|
|
esc := func(s string) string { return strings.ReplaceAll(s, "'", "''") }
|
|
ps := fmt.Sprintf(`Add-Type -AssemblyName System.Windows.Forms;$n=New-Object System.Windows.Forms.NotifyIcon;$n.Icon=[System.Drawing.SystemIcons]::Information;$n.Visible=$true;$n.ShowBalloonTip(8000,'%s','%s',[System.Windows.Forms.ToolTipIcon]::Info);Start-Sleep 9;$n.Dispose()`, esc(title), esc(body))
|
|
cmd := exec.Command("powershell.exe", "-NoProfile", "-WindowStyle", "Hidden", "-Command", ps)
|
|
hideWindow(cmd)
|
|
_ = cmd.Start()
|
|
}
|
|
|
|
func notify(title, subtitle string) { notifyUser(title, subtitle) }
|
|
|
|
func openFolderPath(p string) { _ = exec.Command("explorer.exe", p).Start() }
|
|
|
|
func runInstallPrinterWindows() int {
|
|
script := filepath.Join(exeDir(), "..", "scripts", "install-printer.ps1")
|
|
ps := filepath.Join(os.Getenv("WINDIR"), "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
cmd := exec.Command(ps, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script)
|
|
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "install-printer failed: %v (run elevated; see %s)\n", err, vlog.Dir())
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func debugFromRegistry() bool {
|
|
out, err := exec.Command("reg.exe", "query", `HKLM\Software\Verae\PrintStamp`, "/v", "Debug").Output()
|
|
return err == nil && strings.Contains(string(out), "0x1")
|
|
}
|