From 4415ffe2eed1fca827a99a89c83ad7508d6f7cc4 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 11 Nov 2021 10:21:10 +0800 Subject: [PATCH] fix: deprecate 'action' and use 'cmd' in file --- app.go | 4 ++-- configui.go | 6 ++++++ file.go | 17 ++++++++++------- handler.go | 20 ++++++++++---------- templates/components/menu.tmpl | 2 +- templates/components/result.tmpl | 2 +- templates/components/toolbar.tmpl | 4 ++-- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index 8cb4455..ac8fa72 100644 --- a/app.go +++ b/app.go @@ -12,7 +12,7 @@ type ActiveFile struct { RO bool Path string Name string - Action string + Cmd string Content string Order int } @@ -100,7 +100,7 @@ func (cui *ConfigUI) App(w http.ResponseWriter, r *http.Request) { } } else { tmp, err = file.Read() - data.File.Action = file.Action + data.File.Cmd = file.Cmd data.File.Name = file.Name if file.Lang != "" { data.Editor.Lang = file.Lang diff --git a/configui.go b/configui.go index 54d2ed7..5ba8116 100644 --- a/configui.go +++ b/configui.go @@ -139,6 +139,12 @@ func (cui *ConfigUI) LoadConfig(confstr string) error { } f.owner = cui tmpIndex[f.Name] = i + + // deprecated fix + if f.Cmd == "" && f.Action != "" { + f.Cmd = f.Action + f.Action = "" + } } // copy diff --git a/file.go b/file.go index b4e21ea..d93a6e7 100644 --- a/file.go +++ b/file.go @@ -12,9 +12,9 @@ import ( ) type File struct { - Path string `json:"path"` - Name string `json:"name"` - Action string `json:"action"` + Path string `json:"path"` + Name string `json:"name"` + Cmd string `json:"cmd"` // RO is readonly RO bool `json:"ro"` Lang string `json:"lang"` @@ -29,6 +29,9 @@ type File struct { owner *ConfigUI `json:"-"` run chan struct{} `json:"-"` pid int `json:"-"` + + // deprecated + Action string `json:"action,omitempty"` } func (f *File) Read() ([]byte, error) { @@ -56,7 +59,7 @@ func (f *File) Write(data []byte) error { } func (f *File) Do(CmdTimeout time.Duration) (string, error) { - if f.Action == "" { + if f.Cmd == "" { return "", nil } f.lock.RLock() @@ -77,11 +80,11 @@ func (f *File) Do(CmdTimeout time.Duration) (string, error) { // prepare cmd cmd := &exec.Cmd{} if runtime.GOOS == "windows" { - cmd = exec.Command(WIN_SHELL, "/c", f.Action) + cmd = exec.Command(WIN_SHELL, "/c", f.Cmd) } else { - cmd = exec.Command(UNIX_SHELL, "-c", f.Action) + cmd = exec.Command(UNIX_SHELL, "-c", f.Cmd) } - f.owner.log.Info("DO: ", f.Action) + f.owner.log.Info("DO: ", f.Cmd) done := make(chan string, 1) go func() { diff --git a/handler.go b/handler.go index e81c71e..ceee09c 100644 --- a/handler.go +++ b/handler.go @@ -37,11 +37,11 @@ func (cui *ConfigUI) GetFile(w http.ResponseWriter, r *http.Request) { panic(err) } response, err := json.Marshal(map[string]string{ - "path": file.Path, - "name": file.Name, - "action": file.Action, - "data": string(data), - "delta": strconv.Itoa(int(stat.Size())), + "path": file.Path, + "name": file.Name, + "cmd": file.Cmd, + "data": string(data), + "delta": strconv.Itoa(int(stat.Size())), }) if err != nil { panic(err) @@ -81,11 +81,11 @@ func (cui *ConfigUI) GetDelta(w http.ResponseWriter, r *http.Request) { } response, err := json.Marshal(map[string]string{ - "path": file.Path, - "name": file.Name, - "action": file.Action, - "data": string(buf), - "delta": strconv.Itoa(int(stat.Size())), + "path": file.Path, + "name": file.Name, + "cmd": file.Cmd, + "data": string(buf), + "delta": strconv.Itoa(int(stat.Size())), }) if err != nil { panic(err) diff --git a/templates/components/menu.tmpl b/templates/components/menu.tmpl index 87d7771..e428be6 100644 --- a/templates/components/menu.tmpl +++ b/templates/components/menu.tmpl @@ -21,7 +21,7 @@ diff --git a/templates/components/result.tmpl b/templates/components/result.tmpl index 9883951..6893981 100644 --- a/templates/components/result.tmpl +++ b/templates/components/result.tmpl @@ -7,7 +7,7 @@ diff --git a/templates/components/toolbar.tmpl b/templates/components/toolbar.tmpl index da78ea0..f6162cb 100644 --- a/templates/components/toolbar.tmpl +++ b/templates/components/toolbar.tmpl @@ -91,10 +91,10 @@ {{end}}

{{end}} - {{if .File.Action}} + {{if .File.Cmd}}