58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package events
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/fs"
|
|
"sort"
|
|
|
|
bellEvent "git.ilapage.cn/ila/yovision/Bell/server/app/event"
|
|
)
|
|
|
|
//go:embed *.json
|
|
var files embed.FS
|
|
|
|
type Fixture struct {
|
|
Name string `json:"name"`
|
|
Label string `json:"label"`
|
|
ValidationError string `json:"validation_error,omitempty"`
|
|
Command bellEvent.Command `json:"command"`
|
|
}
|
|
|
|
func All() ([]Fixture, error) {
|
|
entries, err := fs.ReadDir(files, ".")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := []Fixture{}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
data, err := files.ReadFile(entry.Name())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var item Fixture
|
|
if err := json.Unmarshal(data, &item); err != nil {
|
|
return nil, fmt.Errorf("decode fixture %s: %w", entry.Name(), err)
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
sort.Slice(items, func(i, j int) bool { return items[i].Name < items[j].Name })
|
|
return items, nil
|
|
}
|
|
func Find(name string) (Fixture, error) {
|
|
items, err := All()
|
|
if err != nil {
|
|
return Fixture{}, err
|
|
}
|
|
for _, item := range items {
|
|
if item.Name == name {
|
|
return item, nil
|
|
}
|
|
}
|
|
return Fixture{}, fmt.Errorf("unknown synthetic fixture %q", name)
|
|
}
|