-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
41 lines (32 loc) · 824 Bytes
/
state.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
package main
import (
"context"
"github.com/coreos/go-systemd/v22/dbus"
)
// SystemdState type is the state of systemd.
type SystemdState struct {
*dbus.Property
}
// String gives the string value of the state ("running",
// "maintenance", ...).
func (s *SystemdState) String() string {
return s.Value.Value().(string)
}
// IsRunning is true when the systemd state is running and false
// otherwise.
func (s *SystemdState) IsRunning() bool {
return (s.String() == "running")
}
// State returns the systemd state.
func State(ctx context.Context) (SystemdState, error) {
conn, err := dbus.NewSystemdConnectionContext(ctx)
if err != nil {
return SystemdState{}, err
}
defer conn.Close()
p, err := conn.SystemStateContext(ctx)
if err != nil {
return SystemdState{}, err
}
return SystemdState{p}, nil
}