serviceman/manager/install_nixes.go

75 lines
1.4 KiB
Go
Raw Normal View History

2019-08-05 11:53:32 +00:00
// +build !windows
package manager
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
2020-12-02 23:18:34 +00:00
"git.rootprojects.org/root/serviceman/service"
2019-08-05 11:53:32 +00:00
)
// this code is shared between Mac and Linux, but may diverge in the future
func list(c *service.Service) ([]string, []string, []error) {
confDir := srvSysPath
if !c.System {
confDir = filepath.Join(c.Home, srvUserPath)
}
// Enuser path exists
err := os.MkdirAll(confDir, 0755)
if nil != err {
return nil, nil, []error{err}
}
fis, err := ioutil.ReadDir(confDir)
if nil != err {
return nil, nil, []error{err}
}
managed := []string{}
others := []string{}
errs := []error{}
b := make([]byte, 256)
for i := range fis {
fi := fis[i]
if !strings.HasSuffix(strings.ToLower(fi.Name()), srvExt) || len(fi.Name()) <= srvLen {
continue
}
2019-08-05 11:57:50 +00:00
confFile := filepath.Join(confDir, fi.Name())
r, err := os.Open(confFile)
2019-08-05 11:53:32 +00:00
if nil != err {
2019-08-05 11:57:50 +00:00
errs = append(errs, &ManageError{
Name: confFile,
Hint: "Open file",
Parent: err,
})
2019-08-05 11:53:32 +00:00
continue
}
n, err := r.Read(b)
if nil != err {
2019-08-05 11:57:50 +00:00
errs = append(errs, &ManageError{
Name: confFile,
Hint: "Read file",
Parent: err,
})
2019-08-05 11:53:32 +00:00
continue
}
b = b[:n]
name := fi.Name()[:len(fi.Name())-srvLen]
if bytes.Contains(b, []byte("for serviceman.")) {
managed = append(managed, name)
} else {
others = append(others, name)
}
}
return managed, others, errs
}