diff --git a/core/execute.go b/core/execute.go index f097626..2f2e647 100644 --- a/core/execute.go +++ b/core/execute.go @@ -3,7 +3,6 @@ package core import ( "bufio" "context" - "database/sql/driver" "encoding/json" "errors" "fmt" @@ -15,77 +14,6 @@ import ( "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) { var item Item parseError := false diff --git a/core/execute_test.go b/core/execute_test.go index 9a33f04..78df685 100644 --- a/core/execute_test.go +++ b/core/execute_test.go @@ -5,56 +5,6 @@ import ( "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) { assertLen := func(items []Item, length int) { if len(items) != length {