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

119
pkg/bcdedit/bcdedit.go Normal file
View File

@@ -0,0 +1,119 @@
package bcdedit
import (
"Win2Linux/pkg/windows/cmd"
"regexp"
"strings"
)
type (
Entry struct {
Key string
Attributes map[string]string
}
)
var (
r *regexp.Regexp = regexp.MustCompile(`-+`)
)
func List() ([]Entry, error) {
args := []string{
"bcdedit",
"/enum",
"firmware",
}
out, err := cmd.Execute(args)
if err != nil {
return nil, err
}
return parse(out), nil
}
func Reboot(uuid string) error {
args := []string{
"bcdedit",
"/bootsequence",
uuid,
"/addfirst",
}
_, err := cmd.Execute(args)
if err != nil {
return err
}
args = []string{
"shutdown",
"/r",
"/t",
"0",
}
_, err = cmd.Execute(args)
if err != nil {
return err
}
return nil
}
func RebootToFirmware() error {
args := []string{
"shutdown",
"/r",
"/fw",
"/t",
"0",
}
_, err := cmd.Execute(args)
if err != nil {
return err
}
return nil
}
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
}

25
pkg/errors/errors.go Normal file
View File

@@ -0,0 +1,25 @@
package errors
import "fmt"
type (
WinError struct {
err string
_code uint32
}
)
func New(err any, exitCode uint32) *WinError {
return &WinError{
err: fmt.Sprintf("%v", err),
_code: exitCode,
}
}
func (e *WinError) Error() string {
return e.err
}
func (e *WinError) ExitCode() uint32 {
return e._code
}

18
pkg/windows/cmd/cmd.go Normal file
View File

@@ -0,0 +1,18 @@
package cmd
import (
"Win2Linux/pkg/errors"
"fmt"
"os/exec"
"strings"
)
func Execute(c []string) (string, error) {
a := strings.Join(c, " ")
cmd := exec.Command("cmd", "/d", "/c", "chcp 65001>nul && "+a)
out, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(fmt.Sprintf("failed to run the command: %s: %s", err, out), uint32(cmd.ProcessState.ExitCode()))
}
return string(out), nil
}

View File

@@ -1,6 +1,7 @@
package windows
import (
"os"
"syscall"
"unsafe"
)
@@ -10,8 +11,13 @@ const (
MB_OK = 0
)
func Fatal(title, message string, exitCode int) {
messageBox(NULL, message, title, MB_OK)
os.Exit(exitCode)
}
// MessageBox of Win32 API.
func MessageBox(hwnd uintptr, caption, title string, flags uint) int {
func messageBox(hwnd uintptr, caption, title string, flags uint) int {
_caption, err := syscall.UTF16PtrFromString(caption)
if err != nil {
panic(err)