2023-03-31 11:52:04 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2023-04-10 06:30:57 +02:00
|
|
|
"context"
|
2023-04-01 11:58:02 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2023-04-10 09:07:08 +02:00
|
|
|
"log"
|
2023-04-01 11:58:02 +02:00
|
|
|
"path"
|
2023-04-02 11:11:09 +02:00
|
|
|
"path/filepath"
|
2023-04-01 11:58:02 +02:00
|
|
|
"strings"
|
2023-03-31 11:52:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type FsResource struct {
|
2023-04-03 00:53:21 +02:00
|
|
|
MenuResource
|
2023-04-01 15:47:03 +02:00
|
|
|
Path string
|
2023-04-10 06:30:57 +02:00
|
|
|
fns map[string]EntryFunc
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-02 11:11:09 +02:00
|
|
|
func NewFsResource(path string) (FsResource) {
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-03-31 11:52:04 +02:00
|
|
|
return FsResource{
|
2023-04-02 11:11:09 +02:00
|
|
|
Path: absPath,
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
func(fs FsResource) GetTemplate(sym string) (string, error) {
|
2023-04-01 15:47:03 +02:00
|
|
|
fp := path.Join(fs.Path, sym)
|
2023-04-01 11:58:02 +02:00
|
|
|
r, err := ioutil.ReadFile(fp)
|
|
|
|
s := string(r)
|
|
|
|
return strings.TrimSpace(s), err
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 11:58:02 +02:00
|
|
|
func(fs FsResource) GetCode(sym string) ([]byte, error) {
|
2023-04-02 11:11:09 +02:00
|
|
|
fb := sym + ".bin"
|
|
|
|
fp := path.Join(fs.Path, fb)
|
|
|
|
return ioutil.ReadFile(fp)
|
2023-04-01 11:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
|
2023-04-10 06:30:57 +02:00
|
|
|
fn, ok := fs.fns[sym]
|
|
|
|
if ok {
|
|
|
|
return fn, nil
|
|
|
|
}
|
2023-04-13 08:56:35 +02:00
|
|
|
_, err := fs.getFuncNoCtx(sym, nil)
|
2023-04-10 09:07:08 +02:00
|
|
|
if err != nil {
|
2023-04-10 06:30:57 +02:00
|
|
|
return nil, fmt.Errorf("unknown sym: %s", sym)
|
|
|
|
}
|
|
|
|
return fs.getFunc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(fs FsResource) String() string {
|
|
|
|
return fmt.Sprintf("fs resource at path: %s", fs.Path)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:16:17 +02:00
|
|
|
func(fs FsResource) getFunc(sym string, input []byte, ctx context.Context) (Result, error) {
|
2023-04-13 08:56:35 +02:00
|
|
|
return fs.getFuncNoCtx(sym, input)
|
2023-04-01 11:58:02 +02:00
|
|
|
}
|
2023-04-02 11:11:09 +02:00
|
|
|
|
2023-04-13 10:16:17 +02:00
|
|
|
func(fs FsResource) getFuncNoCtx(sym string, input []byte) (Result, error) {
|
2023-04-10 06:30:57 +02:00
|
|
|
fb := sym + ".txt"
|
|
|
|
fp := path.Join(fs.Path, fb)
|
2023-04-10 09:07:08 +02:00
|
|
|
log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym)
|
2023-04-10 06:30:57 +02:00
|
|
|
r, err := ioutil.ReadFile(fp)
|
|
|
|
if err != nil {
|
2023-04-13 10:16:17 +02:00
|
|
|
return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
|
2023-04-10 06:30:57 +02:00
|
|
|
}
|
|
|
|
s := string(r)
|
2023-04-13 10:16:17 +02:00
|
|
|
return Result{
|
|
|
|
Content: strings.TrimSpace(s),
|
|
|
|
}, nil
|
2023-04-02 11:11:09 +02:00
|
|
|
}
|