You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
944 B
Go
59 lines
944 B
Go
package monitor
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
ScriptsDir string `json:"scriptsDir"`
|
|
}
|
|
|
|
type Service struct {
|
|
Config *Config
|
|
|
|
scriptPaths map[string]string
|
|
}
|
|
|
|
func NewService(config *Config) *Service {
|
|
return &Service{
|
|
Config: config,
|
|
}
|
|
}
|
|
|
|
func (s *Service) LoadScripts() error {
|
|
entries, err := os.ReadDir(s.Config.ScriptsDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.scriptPaths = map[string]string{}
|
|
|
|
for _, entry := range entries {
|
|
newScriptPath := path.Join(s.Config.ScriptsDir, entry.Name())
|
|
s.scriptPaths[entry.Name()] = newScriptPath
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// TODO: Add caching
|
|
func (s *Service) GetLastOutput(command string) (string, error) {
|
|
args := strings.Fields(command)
|
|
|
|
scriptPath := s.scriptPaths[args[0]]
|
|
|
|
cmd := exec.Command(scriptPath, args[1:]...)
|
|
var b bytes.Buffer
|
|
cmd.Stdout = &b
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return b.String(), nil
|
|
}
|