Fix other

This commit is contained in:
2026-04-19 12:56:16 +02:00
parent 669c5b3011
commit 42effa04a5
7 changed files with 223 additions and 103 deletions

132
main.go
View File

@@ -1,28 +1,21 @@
package main
import (
"Win2Linux/pkg/bcdedit"
"Win2Linux/pkg/windows"
_ "embed"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"Win2Linux/pkg/errors"
"github.com/getlantern/systray"
)
//go:generate go-winres make
type (
Entry struct {
Key string
Attributes map[string]string
}
)
var (
r *regexp.Regexp = regexp.MustCompile(`-+`)
const (
GeneralFailure int32 = 0x01
)
//go:embed tray.ico
@@ -35,10 +28,17 @@ func main() {
}
func onReady() {
entries := list()
systray.SetTitle("Win2Linux")
systray.SetTooltip("Switch from Windows to Linux")
systray.SetIcon(icon)
entries, err := bcdedit.List()
if err != nil {
if wErr, ok := err.(*errors.WinError); ok {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
} else {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
}
}
for _, entry := range entries {
fmt.Println(entry.Attributes)
@@ -49,14 +49,29 @@ func onReady() {
}
mCustom := systray.AddMenuItem(key, "Switch to"+key)
go func() {
<-mCustom.ClickedCh
if uuid == "{fwbootmgr}" {
rebootToFirmware()
return
for {
<-mCustom.ClickedCh
if uuid == "{fwbootmgr}" {
err := bcdedit.RebootToFirmware()
if err != nil {
if wErr, ok := err.(*errors.WinError); ok {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
} else {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
}
}
return
}
err := bcdedit.Reboot(uuid)
if err != nil {
if wErr, ok := err.(*errors.WinError); ok {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
} else {
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
}
}
}
reboot(uuid)
}()
}
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit")
@@ -70,82 +85,3 @@ func onReady() {
func onExit() {
// clean up here
}
func list() []Entry {
cmd := exec.Command("bcdedit", "/enum", "firmware")
out, err := cmd.Output()
if err != nil {
fatal(fmt.Sprintf("failed to run bcdedit: %s\n\nRun this program in administrator mode", err), 1)
}
return parse(string(out))
}
func parse(out string) []Entry {
lines := strings.Split(out, "\r\n")
section := false
lastLine := ""
secName := ""
a := make(map[string][]string)
for _, l := range lines {
if !section {
if r.Match([]byte(l)) {
secName = lastLine
section = true
}
} else {
if len(l) != 0 {
a[secName] = append(a[secName], l)
} else {
section = false
}
}
lastLine = l
}
var entries []Entry
for k, sec := range a {
entry := Entry{
Key: k,
Attributes: make(map[string]string),
}
for _, l := range sec {
l = strings.Join(strings.Fields(l), " ")
val := strings.Split(l, " ")
if len(val) != 2 {
if len(val) > 2 {
val[1] = strings.Join(val[1:], " ")
}
continue
}
entry.Attributes[val[0]] = val[1]
}
entries = append(entries, entry)
}
return entries
}
func reboot(uuid string) {
out, err := exec.Command("bcdedit", "/Set", "{fwbootmgr}", "BootSequence", uuid, "/addFirst").Output()
if err != nil {
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to execute bcdedit: %s", out), "Win2Linux", windows.MB_OK)
return
}
out, err = exec.Command("shutdown", "/r", "/t", "0").Output()
if err != nil {
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
}
}
func rebootToFirmware() {
out, err := exec.Command("shutdown", "/r", "/fw", "/t", "0").Output()
if err != nil {
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
}
}
func fatal(message string, exitCode int) {
windows.MessageBox(windows.NULL, message, "Win2Linux", windows.MB_OK)
os.Exit(exitCode)
}