configui/file.go

106 lines
1.9 KiB
Go

package configui
import (
"encoding/json"
"errors"
"log"
"os"
"os/exec"
"runtime"
"sync"
"time"
)
type File struct {
Path string `json:"path"`
Name string `json:"name"`
Action string `json:"action"`
RO bool `json:"ro"`
Lang string `json:"lang"`
// used for parsing post data
Data string `json:"data"`
lock sync.RWMutex `json:"-"`
}
func (f *File) Read() ([]byte, error) {
f.lock.RLock()
defer f.lock.RUnlock()
data, err := os.ReadFile(f.Path)
if err != nil {
log.Println(err)
return nil, err
}
return data, nil
}
func (f *File) Write(data []byte) error {
if f.RO {
return errors.New("this file has readonly set")
}
log.Println("dfsdf")
f.lock.Lock()
defer f.lock.Unlock()
info, err := os.Stat(f.Path)
if err != nil {
return err
}
return os.WriteFile(f.Path, data, info.Mode())
}
func (f *File) Do() (string, error) {
log.Println("do ", f.Action)
if f.Action == "" {
return "", nil
}
timeout := time.After(2 * time.Second)
cmd := &exec.Cmd{}
if runtime.GOOS == "windows" {
cmd = exec.Command(WIN_SHELL, "/c", f.Action)
} else {
exec.Command(UNIX_SHELL, "-c", f.Action)
}
done := make(chan string, 1)
log.Println("start ", f.Action)
go func() {
out, _ := cmd.CombinedOutput()
// real cmd err is only pass down
// if err != nil {
// return string(out), err
// }
done <- string(out)
}()
select {
case <-timeout:
cmd.Process.Kill()
log.Println("timeout")
return "", errors.New("command timeout")
case out := <-done:
log.Println(out)
return out, nil
}
}
func ReadConfig(confstr string) ([]File, error) {
conf := []File{}
err := json.Unmarshal([]byte(confstr), &conf)
if err != nil {
return nil, err
}
for i := range conf {
if conf[i].Name == "" {
conf[i].Name = conf[i].Path
}
}
return conf, nil
}
func GetFileMap(files []File) map[string]*File {
fileMap := map[string]*File{}
for i := range files {
fileMap[files[i].Name] = &files[i]
}
return fileMap
}