2021-10-18 08:49:16 +00:00
|
|
|
package configui
|
|
|
|
|
|
|
|
import (
|
2021-10-18 10:59:09 +00:00
|
|
|
"errors"
|
2021-10-18 08:49:16 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
2021-10-21 08:04:57 +00:00
|
|
|
"time"
|
2021-10-18 08:49:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Path string `json:"path"`
|
2021-10-23 04:56:24 +00:00
|
|
|
Name string `json:"name"`
|
2021-10-18 08:49:16 +00:00
|
|
|
Action string `json:"action"`
|
2021-10-23 04:56:24 +00:00
|
|
|
// RO is readonly
|
|
|
|
RO bool `json:"ro"`
|
|
|
|
Lang string `json:"lang"`
|
|
|
|
|
|
|
|
// Order order of the display on ui
|
|
|
|
Order int `json:"order"`
|
2021-10-18 08:49:16 +00:00
|
|
|
|
|
|
|
// used for parsing post data
|
|
|
|
Data string `json:"data"`
|
|
|
|
|
2021-11-03 19:35:51 +00:00
|
|
|
lock sync.RWMutex `json:"-"`
|
|
|
|
owner *ConfigUI `json:"-"`
|
2021-10-18 08:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Read() ([]byte, error) {
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
data, err := os.ReadFile(f.Path)
|
|
|
|
if err != nil {
|
2021-11-03 19:35:51 +00:00
|
|
|
f.owner.log.Error(err)
|
2021-10-18 08:49:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Write(data []byte) error {
|
2021-10-18 10:59:09 +00:00
|
|
|
if f.RO {
|
|
|
|
return errors.New("this file has readonly set")
|
|
|
|
}
|
2021-10-18 08:49:16 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-11-03 10:08:37 +00:00
|
|
|
func (f *File) Do(CmdTimeout time.Duration) (string, error) {
|
2021-10-18 08:49:16 +00:00
|
|
|
if f.Action == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-10-23 04:56:24 +00:00
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
2021-10-21 08:04:57 +00:00
|
|
|
cmd := &exec.Cmd{}
|
2021-10-18 08:49:16 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cmd = exec.Command(WIN_SHELL, "/c", f.Action)
|
2021-10-21 08:04:57 +00:00
|
|
|
} else {
|
2021-10-23 04:56:24 +00:00
|
|
|
cmd = exec.Command(UNIX_SHELL, "-c", f.Action)
|
2021-10-21 08:04:57 +00:00
|
|
|
}
|
2021-11-03 19:35:51 +00:00
|
|
|
f.owner.log.Info("DO: ", f.Action)
|
2021-10-23 04:56:24 +00:00
|
|
|
done := make(chan string, 1)
|
2021-10-21 08:04:57 +00:00
|
|
|
go func() {
|
2021-10-23 04:56:24 +00:00
|
|
|
out, _ := cmd.CombinedOutput()
|
|
|
|
// real cmd err is unhandled, but passed to client
|
2021-10-21 08:04:57 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return string(out), err
|
|
|
|
// }
|
2021-10-23 04:56:24 +00:00
|
|
|
done <- string(out)
|
2021-10-21 08:04:57 +00:00
|
|
|
}()
|
|
|
|
select {
|
2021-11-03 10:08:37 +00:00
|
|
|
case <-time.After(CmdTimeout):
|
2021-10-21 08:04:57 +00:00
|
|
|
cmd.Process.Kill()
|
2021-11-03 19:35:51 +00:00
|
|
|
f.owner.log.Error("timeout")
|
2021-10-21 08:04:57 +00:00
|
|
|
return "", errors.New("command timeout")
|
2021-10-23 04:56:24 +00:00
|
|
|
case out := <-done:
|
2021-11-03 19:35:51 +00:00
|
|
|
f.owner.log.Info("\n", out)
|
2021-10-23 04:56:24 +00:00
|
|
|
return out, nil
|
2021-10-18 08:49:16 +00:00
|
|
|
}
|
|
|
|
}
|