service-tools/service-generator/targets.go

50 lines
692 B
Go
Raw Normal View History

2018-03-11 08:43:21 +08:00
package main
import (
"strings"
"github.com/coreos/go-systemd/dbus"
)
type Targets []dbus.UnitStatus
func targets() (Targets, error) {
res := []dbus.UnitStatus{}
conn, err := dbus.New()
if err != nil {
return res, err
}
us, err := conn.ListUnits()
if err != nil {
return res, err
}
for _, v := range us {
if !strings.HasSuffix(v.Name, ".target") {
continue
}
res = append(res, v)
}
return res, nil
}
func (ts Targets) Contains(name string) bool {
for _, t := range ts {
if t.Name == name {
return true
}
}
return false
}
2018-03-12 17:10:21 +08:00
func (ts Targets) Strings() []string {
var res []string
for _, t := range ts {
res = append(res, t.Name)
}
return res
}