Reduce execute.go to execute functions
This commit is contained in:
parent
860c8008e0
commit
e695094a0a
@ -3,7 +3,6 @@ package core
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"database/sql/driver"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -15,77 +14,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Type alias for storing string array as jsonb
|
|
||||||
type argList []string
|
|
||||||
|
|
||||||
func (a argList) Value() (driver.Value, error) {
|
|
||||||
return json.Marshal(a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *argList) Scan(value interface{}) error {
|
|
||||||
return json.Unmarshal([]byte(value.(string)), a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddAction(db DB, source string, name string, argv []string) error {
|
|
||||||
_, err := db.Exec(`
|
|
||||||
insert into actions (source, name, argv)
|
|
||||||
values (?, ?, jsonb(?))
|
|
||||||
`, source, name, argList(argv))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateAction(db DB, source string, name string, argv []string) error {
|
|
||||||
_, err := db.Exec(`
|
|
||||||
update actions
|
|
||||||
set argv = jsonb(?)
|
|
||||||
where source = ? and name = ?
|
|
||||||
`, argList(argv), source, name)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetActionsForSource(db DB, source string) ([]string, error) {
|
|
||||||
rows, err := db.Query(`
|
|
||||||
select name
|
|
||||||
from actions
|
|
||||||
where source = ?
|
|
||||||
`, source)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var names []string
|
|
||||||
for rows.Next() {
|
|
||||||
var name string
|
|
||||||
err = rows.Scan(&name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
names = append(names, name)
|
|
||||||
}
|
|
||||||
return names, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetArgvForAction(db DB, source string, name string) ([]string, error) {
|
|
||||||
rows := db.QueryRow(`
|
|
||||||
select json(argv)
|
|
||||||
from actions
|
|
||||||
where source = ? and name = ?
|
|
||||||
`, source, name)
|
|
||||||
var argv argList
|
|
||||||
err := rows.Scan(&argv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return argv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteAction(db DB, source string, name string) error {
|
|
||||||
_, err := db.Exec(`
|
|
||||||
delete from actions
|
|
||||||
where source = ? and name = ?
|
|
||||||
`, source, name)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func readStdout(stdout io.ReadCloser, source string, items chan Item, cparse chan bool) {
|
func readStdout(stdout io.ReadCloser, source string, items chan Item, cparse chan bool) {
|
||||||
var item Item
|
var item Item
|
||||||
parseError := false
|
parseError := false
|
||||||
|
@ -5,56 +5,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestActionCreate(t *testing.T) {
|
|
||||||
db := EphemeralDb(t)
|
|
||||||
|
|
||||||
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err == nil {
|
|
||||||
t.Fatal("Action created for nonexistent source")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := AddSource(db, "test"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := AddAction(db, "test", "goodbye", []string{"exit", "1"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := UpdateAction(db, "test", "goodbye", []string{"echo", "goodbye"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
actions, err := GetActionsForSource(db, "test")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(actions) != 2 {
|
|
||||||
t.Fatal("expected 2 actions")
|
|
||||||
}
|
|
||||||
found := make(map[string]bool)
|
|
||||||
for _, action := range actions {
|
|
||||||
found[action] = true
|
|
||||||
}
|
|
||||||
if !found["hello"] || !found["goodbye"] {
|
|
||||||
t.Fatalf("missing hello and/or goodbye, got: %v", actions)
|
|
||||||
}
|
|
||||||
|
|
||||||
argv, err := GetArgvForAction(db, "test", "goodbye")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(argv) != 2 || argv[0] != "echo" || argv[1] != "goodbye" {
|
|
||||||
t.Fatalf("expected [echo goodbye], got: %v", argv)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = DeleteAction(db, "test", "hello")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecute(t *testing.T) {
|
func TestExecute(t *testing.T) {
|
||||||
assertLen := func(items []Item, length int) {
|
assertLen := func(items []Item, length int) {
|
||||||
if len(items) != length {
|
if len(items) != length {
|
||||||
|
Loading…
Reference in New Issue
Block a user