2018-03-23 14:45:54 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2018-03-27 06:13:03 +08:00
|
|
|
"strings"
|
2018-03-23 14:45:54 +08:00
|
|
|
|
|
|
|
"github.com/coreos/go-systemd/sdjournal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServiceItem struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Matches []sdjournal.Match
|
|
|
|
}
|
|
|
|
|
2018-03-24 14:58:07 +08:00
|
|
|
func serviceModel(specialServices, activeOnly bool) ([]ServiceItem, error) {
|
2018-03-23 14:45:54 +08:00
|
|
|
ts, err := services()
|
|
|
|
if err != nil {
|
|
|
|
return []ServiceItem{}, fmt.Errorf("Can't find systemd services: %s", err)
|
|
|
|
}
|
|
|
|
if activeOnly {
|
|
|
|
ts = ts.ActiveOnly()
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(ts, func(i, j int) bool {
|
2018-03-27 06:13:03 +08:00
|
|
|
return strings.ToLower(ts[i].Name) < strings.ToLower(ts[j].Name)
|
2018-03-23 14:45:54 +08:00
|
|
|
})
|
|
|
|
|
2018-03-24 14:58:07 +08:00
|
|
|
model := []ServiceItem{}
|
|
|
|
if specialServices {
|
|
|
|
model = append(model, []ServiceItem{
|
|
|
|
{
|
|
|
|
Name: "All",
|
|
|
|
Description: "Everything in the log",
|
|
|
|
Matches: []sdjournal.Match{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Kernel",
|
|
|
|
Description: "Kernel log",
|
|
|
|
Matches: []sdjournal.Match{
|
|
|
|
{
|
|
|
|
Field: sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER,
|
|
|
|
Value: "kernel",
|
|
|
|
},
|
2018-03-23 14:45:54 +08:00
|
|
|
},
|
|
|
|
},
|
2018-03-24 14:58:07 +08:00
|
|
|
}...)
|
2018-03-23 14:45:54 +08:00
|
|
|
}
|
2018-03-24 14:58:07 +08:00
|
|
|
|
2018-03-23 14:45:54 +08:00
|
|
|
for _, service := range ts {
|
|
|
|
model = append(model, ServiceItem{
|
|
|
|
Name: service.Name,
|
|
|
|
Description: service.Description,
|
|
|
|
Matches: []sdjournal.Match{
|
|
|
|
{
|
|
|
|
Field: sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT,
|
|
|
|
Value: service.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return model, nil
|
|
|
|
}
|