Add actions to the database

This commit is contained in:
Tim Van Baak 2025-01-20 19:53:22 -08:00
parent 2a58c01319
commit fb0d4e9aee
3 changed files with 124 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package core
import (
"bufio"
"context"
"database/sql/driver"
"encoding/json"
"errors"
"io"
@ -13,6 +14,77 @@ 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 = ?
`)
return err
}
func readStdout(stdout io.ReadCloser, items chan Item, cparse chan bool) {
var item Item
parseError := false

View File

@ -5,6 +5,51 @@ 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)
}
}
func TestExecute(t *testing.T) {
assertLen := func(items []Item, length int) {
if len(items) != length {

View File

@ -2,6 +2,13 @@ create table sources(
name text not null,
primary key (name)
) strict;
create table actions(
source text not null,
name text not null,
argv blob not null,
primary key (source, name),
foreign key (source) references sources (name) on delete cascade
) strict;
create table items(
source text not null,
id text not null,