-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (67 loc) · 1.71 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
package main
import (
"bufio"
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
func main() {
deviceMap := make(map[string]string)
var displayDeviceFunc = func() {
deviceMap = make(map[string]string)
cmd := exec.Command("powercfg", "/devicequery", "wake_armed")
out, err := cmd.Output()
if err != nil {
fmt.Println(err)
}
out, _ = simplifiedchinese.GBK.NewDecoder().Bytes(out)
if strings.HasPrefix(string(out), "无") {
fmt.Println("没有能唤醒休眠的设备")
return
}
fmt.Println("请选择要禁用的设备序号:")
deviceArr := strings.Split(string(out), "\n")
for i, device := range deviceArr {
if len(strings.TrimSpace(device)) > 0 {
index := strconv.Itoa(i + 1)
fmt.Println(index + ":" + device)
deviceMap[index] = strings.Replace(device, "\r", "", -1)
}
}
}
displayDeviceFunc()
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
line := input.Text()
if deviceMap[line] != "" {
cmd := exec.Command("powercfg", "/devicedisablewake", deviceMap[line])
stderr, _ := cmd.StderrPipe()
err := cmd.Start()
var failFunc = func(errMsg string) {
fmt.Println("禁用失败:" + deviceMap[line])
fmt.Println("原因:" + errMsg)
displayDeviceFunc()
}
if err != nil {
failFunc(err.Error())
continue
}
slurp, _ := io.ReadAll(stderr)
b, _ := simplifiedchinese.GBK.NewDecoder().Bytes(slurp)
stderrMsg := string(b)
if len(stderrMsg) > 0 {
failFunc(stderrMsg)
continue
}
fmt.Println("已禁用该设备唤醒功能:" + deviceMap[line])
deviceMap[line] = ""
} else {
fmt.Println("选项不存在,请重新输入序号")
}
displayDeviceFunc()
}
}