Search for scripts
This commit is contained in:
parent
83f858285f
commit
38eef01e34
2 changed files with 68 additions and 10 deletions
48
script.go
48
script.go
|
|
@ -26,7 +26,7 @@ type Hook struct {
|
|||
SSH string
|
||||
}
|
||||
|
||||
func GetScripts() (scripts []Script, err error) {// {{{
|
||||
func GetScripts() (scripts []Script, err error) { // {{{
|
||||
scripts = []Script{}
|
||||
|
||||
var rows *sqlx.Rows
|
||||
|
|
@ -54,8 +54,8 @@ func GetScripts() (scripts []Script, err error) {// {{{
|
|||
}
|
||||
|
||||
return
|
||||
}// }}}
|
||||
func UpdateScript(scriptID int, data []byte) (script Script, err error) {// {{{
|
||||
} // }}}
|
||||
func UpdateScript(scriptID int, data []byte) (script Script, err error) { // {{{
|
||||
err = json.Unmarshal(data, &script)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
|
|
@ -106,29 +106,57 @@ func UpdateScript(scriptID int, data []byte) (script Script, err error) {// {{{
|
|||
}
|
||||
|
||||
return
|
||||
}// }}}
|
||||
func DeleteScript(scriptID int) (err error) {// {{{
|
||||
} // }}}
|
||||
func DeleteScript(scriptID int) (err error) { // {{{
|
||||
_, err = db.Exec(`DELETE FROM script WHERE id = $1`, scriptID)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}// }}}
|
||||
} // }}}
|
||||
func SearchScripts(search string) (scripts []Script, err error) { // {{{
|
||||
scripts = []Script{}
|
||||
|
||||
func UpdateHook(hook Hook) (err error) {// {{{
|
||||
row := db.QueryRow(`
|
||||
SELECT
|
||||
json_agg(script) AS scripts
|
||||
FROM public.script
|
||||
WHERE
|
||||
name ILIKE $1
|
||||
`,
|
||||
search,
|
||||
)
|
||||
|
||||
var jsonBody []byte
|
||||
err = row.Scan(&jsonBody)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonBody, &scripts)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
} // }}}
|
||||
|
||||
func UpdateHook(hook Hook) (err error) { // {{{
|
||||
_, err = db.Exec(`UPDATE hook SET ssh=$2 WHERE id=$1`, hook.ID, strings.TrimSpace(hook.SSH))
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}// }}}
|
||||
func DeleteHook(hookID int) (err error) {// {{{
|
||||
} // }}}
|
||||
func DeleteHook(hookID int) (err error) { // {{{
|
||||
_, err = db.Exec(`DELETE FROM hook WHERE id=$1`, hookID)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}// }}}
|
||||
} // }}}
|
||||
|
|
|
|||
30
webserver.go
30
webserver.go
|
|
@ -50,6 +50,7 @@ func initWebserver() (err error) {
|
|||
http.HandleFunc("/scripts/", actionScripts)
|
||||
http.HandleFunc("/scripts/update/{scriptID}", actionScriptUpdate)
|
||||
http.HandleFunc("/scripts/delete/{scriptID}", actionScriptDelete)
|
||||
http.HandleFunc("/hooks/search", actionScriptsSearch)
|
||||
http.HandleFunc("/hooks/update", actionHookUpdate)
|
||||
http.HandleFunc("/hooks/delete/{hookID}", actionHookDelete)
|
||||
|
||||
|
|
@ -622,6 +623,35 @@ func actionScriptDelete(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionScriptsSearch(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
var search struct {
|
||||
Search string
|
||||
}
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
err := json.Unmarshal(body, &search)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
scripts, err := SearchScripts(search.Search)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
Scripts []Script
|
||||
}{
|
||||
true,
|
||||
scripts,
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
func actionHookUpdate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
var hook Hook
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue