-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
123 lines (112 loc) · 2.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"github.com/bhendo/go-powershell"
"github.com/bhendo/go-powershell/backend"
"log"
"os"
"strings"
)
func pauseFatal(output string){
fmt.Println(output)
fmt.Print(`Enter to exit:`)
fmt.Scanln()
os.Exit(0)
}
func main(){
shell, err := powershell.New(&backend.Local{})
if err != nil {
log.Panicln("Open Powershell failed:", err.Error())
}
defer shell.Exit()
stdout,_,err:=shell.Execute("bcdedit /enum firmware")
if err!=nil{
log.Panicln("Execute Error:", err.Error())
}
windowsBootManager:="{bootmgr}"
if !strings.Contains(stdout,windowsBootManager){
pauseFatal(stdout)
}
firmwareLines:=strings.Split(stdout,"\r\n")
UEFIBootList:=make(map[string]string)
var identifierList []string
currentIdentifier :=""
for _,line:=range firmwareLines{
if len(line)==0 || line[0]=='-'{
continue
}
lineSplit:=strings.Split(line,` `)
switch lineSplit[0] {
case "identifier":
currentIdentifier =lineSplit[len(lineSplit)-1]
identifierList=append(identifierList,currentIdentifier)
case "description":
description:=lineSplit[len(lineSplit)-1]
description=strings.Trim(description,` `)
UEFIBootList[currentIdentifier]=description
}
}
fmt.Println("----UEFI Boot List----")
for k,v:=range identifierList{
if k==0{
continue
}
fmt.Println(k,UEFIBootList[v])
}
var inputIndex int
defaultIndex:=1
for k,v:=range identifierList{
if k==0{
continue
}
if strings.Contains(UEFIBootList[v],"Windows Boot Manager"){
continue
}
if strings.Contains(UEFIBootList[v],"Internal"){
continue
}
defaultIndex=k
break
}
INPUT:
fmt.Printf("\nPlease input index of the next boot, Enter [%s]:",UEFIBootList[identifierList[defaultIndex]])
_,err=fmt.Scanln(&inputIndex)
if err!=nil{
switch err.Error() {
case "unexpected newline":
inputIndex=defaultIndex
break
case "EOF":
os.Exit(0)
default:
fmt.Println(err.Error())
goto INPUT
}
}
if inputIndex>=len(identifierList){
fmt.Println("Invalid Index")
goto INPUT
}
setNextBoot(shell,identifierList[inputIndex])
fmt.Println("Next boot:",UEFIBootList[identifierList[inputIndex]])
fmt.Print("Enter to reboot:")
_,err=fmt.Scanln(&inputIndex)
if err!=nil && err.Error()=="unexpected newline"{
rebootNow(shell)
}
}
func setNextBoot(shell powershell.Shell,identifier string){
cmd:=fmt.Sprintf(`bcdedit /set "{fwbootmgr}" bootsequence "%s"`,identifier)
stdout,_,err:=shell.Execute(cmd)
if err!=nil{
pauseFatal(err.Error())
}
fmt.Print(stdout)
}
func rebootNow(shell powershell.Shell){
cmd:=`shutdown /r /t 0`
_,_,err:=shell.Execute(cmd)
if err!=nil{
pauseFatal(err.Error())
}
}