Fix other
This commit is contained in:
119
pkg/bcdedit/bcdedit.go
Normal file
119
pkg/bcdedit/bcdedit.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user