package monitor import ( "bytes" "log" "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) log.Printf("Running script %q with args %v", args[0], args[1:]) 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 }