ae-send/main_windows.go

67 lines
1.2 KiB
Go

//go:build windows
// +build windows
package main
import (
"io"
"os"
"os/exec"
"path/filepath"
"golang.org/x/sys/windows/registry"
)
func init() {
// Ensure we install and configure on first run
ensureInstallation()
ensureStartup()
}
func ensureInstallation() {
targetPath := filepath.Join(os.Getenv("ProgramFiles"), "AE-Send", "ae-send.exe")
currentPath, _ := os.Executable()
if currentPath != targetPath {
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
return // handle error
}
if err := copyFile(currentPath, targetPath); err != nil {
return // handle error
}
// Launch new instance
cmd := exec.Command(targetPath)
cmd.Start()
os.Exit(0)
}
}
func ensureStartup() {
key, _, err := registry.CreateKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.SET_VALUE)
if err != nil {
return // handle error
}
defer key.Close()
currentPath, _ := os.Executable()
key.SetStringValue("AE-Send", currentPath)
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}