fs.go (3329B)
1 package resource 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path" 10 "path/filepath" 11 "strings" 12 13 "git.defalsify.org/vise.git/lang" 14 ) 15 16 type FsResource struct { 17 MenuResource 18 Path string 19 fns map[string]EntryFunc 20 // languageStrict bool 21 } 22 23 func NewFsResource(path string) *FsResource { 24 absPath, err := filepath.Abs(path) 25 if err != nil { 26 panic(err) 27 } 28 return &FsResource{ 29 Path: absPath, 30 } 31 } 32 33 //func(fsr *FsResource) WithStrictLanguage() *FsResource { 34 // fsr.languageStrict = true 35 // return fsr 36 //} 37 38 func(fsr FsResource) GetTemplate(ctx context.Context, sym string) (string, error) { 39 fp := path.Join(fsr.Path, sym) 40 fpl := fp 41 v := ctx.Value("Language") 42 if v != nil { 43 lang := v.(lang.Language) 44 fpl += "_" + lang.Code 45 } 46 var r []byte 47 var err error 48 r, err = ioutil.ReadFile(fpl) 49 if err != nil { 50 if errors.Is(err, os.ErrNotExist) { 51 if fpl != fp { 52 r, err = ioutil.ReadFile(fp) 53 } 54 } 55 if err != nil { 56 return "", fmt.Errorf("failed getting template for sym '%s': %v", sym, err) 57 } 58 } 59 s := string(r) 60 return strings.TrimSpace(s), err 61 } 62 63 func(fsr FsResource) GetCode(sym string) ([]byte, error) { 64 fb := sym + ".bin" 65 fp := path.Join(fsr.Path, fb) 66 return ioutil.ReadFile(fp) 67 } 68 69 func(fsr FsResource) GetMenu(ctx context.Context, sym string) (string, error) { 70 fp := path.Join(fsr.Path, sym + "_menu") 71 fpl := fp 72 v := ctx.Value("Language") 73 if v != nil { 74 lang := v.(lang.Language) 75 fpl += "_" + lang.Code 76 } 77 var r []byte 78 var err error 79 r, err = ioutil.ReadFile(fpl) 80 if err != nil { 81 if errors.Is(err, os.ErrNotExist) { 82 if fpl != fp { 83 r, err = ioutil.ReadFile(fp) 84 } 85 } 86 if err != nil { 87 if errors.Is(err, os.ErrNotExist) { 88 return sym, nil 89 } 90 return "", fmt.Errorf("failed getting template for sym '%s': %v", sym, err) 91 } 92 } 93 s := string(r) 94 return strings.TrimSpace(s), err 95 } 96 97 func(fsr *FsResource) AddLocalFunc(sym string, fn EntryFunc) { 98 if fsr.fns == nil { 99 fsr.fns = make(map[string]EntryFunc) 100 } 101 fsr.fns[sym] = fn 102 } 103 104 func(fsr FsResource) FuncFor(sym string) (EntryFunc, error) { 105 fn, ok := fsr.fns[sym] 106 if ok { 107 return fn, nil 108 } 109 _, err := fsr.getFuncNoCtx(sym, nil, nil) 110 if err != nil { 111 return nil, fmt.Errorf("unknown sym: %s", sym) 112 } 113 return fsr.getFunc, nil 114 } 115 116 func(fsr FsResource) String() string { 117 return fmt.Sprintf("fs resource at path: %s", fsr.Path) 118 } 119 120 func(fsr FsResource) getFunc(ctx context.Context, sym string, input []byte) (Result, error) { 121 v := ctx.Value("Language") 122 if v == nil { 123 return fsr.getFuncNoCtx(sym, input, nil) 124 } 125 language := v.(lang.Language) 126 return fsr.getFuncNoCtx(sym, input, &language) 127 } 128 129 func(fsr FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Language) (Result, error) { 130 fb := sym 131 fbl := fb 132 if language != nil { 133 fbl += "_" + language.Code 134 } 135 fb += ".txt" 136 fbl += ".txt" 137 fp := path.Join(fsr.Path, fb) 138 fpl := path.Join(fsr.Path, fbl) 139 Logg.Debugf("getfunc search dir", "dir", fsr.Path, "path", fp, "lang_path", fpl, "sym", sym, "language", language) 140 r, err := ioutil.ReadFile(fpl) 141 if err != nil { 142 if errors.Is(err, os.ErrNotExist) { 143 if fpl != fp { 144 r, err = ioutil.ReadFile(fp) 145 } 146 } 147 if err != nil { 148 return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err) 149 } 150 } 151 s := string(r) 152 return Result{ 153 Content: strings.TrimSpace(s), 154 }, nil 155 }