-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
114 lines (87 loc) · 2.65 KB
/
status.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
package devcon
import "strings"
// Status indicates the current status of a device driver.
type Status string
const (
// StatusRunning indicates that the device driver is running.
StatusRunning Status = "running"
// StatusStopped indicates that the device driver is stopped.
StatusStopped Status = "stopped"
// StatusDisabled indicates that the device driver is disabled.
StatusDisabled Status = "disabled"
// StatusUnknown indicates that the status could not be queried or is
// unknown.
StatusUnknown Status = "unknown"
)
// DriverStatus contains details of the status of a device driver.
type DriverStatus struct {
// Device is the device that corresponds to the driver.
Device Device `json:"device"`
// Status is the current status of the device driver.
Status Status `json:"status"`
}
// Status returns the status (running, stopped, or disabled) of the driver for
// devices on the computer.
//
// If the status of the device cannot be determined, such as when the device is
// no longer attached to the computer, the status from the status display.
//
// Running with the WithRemoteComputer() option is allowed.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-status for more information.
func (dc *DevCon) Status() ([]DriverStatus, error) {
lines, err := dc.run(commandStatus)
if err != nil {
return nil, err
}
groupIndices := make([]int, 0)
for index, line := range lines {
if !strings.HasPrefix(line, " ") {
groupIndices = append(groupIndices, index)
}
}
groupIndices = append(groupIndices, len(lines))
statuses := make([]DriverStatus, 0)
for index, groupStart := range groupIndices {
nextIndex := index + 1
if len(groupIndices) == nextIndex {
break
}
groupEnd := groupIndices[nextIndex]
status := DriverStatus{
Device: Device{
ID: "",
Name: "",
},
Status: "",
}
for lineIndex := groupStart; lineIndex < groupEnd; lineIndex++ {
line := lines[lineIndex]
switch {
case lineIndex == groupStart:
status.Device.ID = line
case lineIndex == groupStart+1:
nameParams := parseParams(reName, line)
if name, ok := nameParams["Name"]; ok {
status.Device.Name = name
}
default:
statusLine := trimSpaces(line)
switch {
case strings.Contains(statusLine, "running"):
status.Status = StatusRunning
case strings.Contains(statusLine, "stopped"):
status.Status = StatusStopped
case strings.Contains(statusLine, "disabled"):
status.Status = StatusDisabled
default:
status.Status = StatusUnknown
}
}
}
if status.Device.Name != "" {
statuses = append(statuses, status)
}
}
return statuses, nil
}