mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 21:20:06 +00:00
feat: description file for custom theme; use gjson (#433)
* feat: description file for custom theme; use gjson * fix gosec * remove outdated stuff
This commit is contained in:
36
pkg/utils/gjson.go
Normal file
36
pkg/utils/gjson.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrGjsonNotFound = errors.New("specified path does not exist")
|
||||
ErrGjsonWrongType = errors.New("wrong type")
|
||||
)
|
||||
|
||||
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
||||
result := gjson.GetBytes(json, path)
|
||||
if !result.Exists() {
|
||||
return result, ErrGjsonNotFound
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GjsonParseStringMap(jsonObject string) (map[string]string, error) {
|
||||
result := gjson.Parse(jsonObject)
|
||||
if !result.IsObject() {
|
||||
return nil, ErrGjsonWrongType
|
||||
}
|
||||
|
||||
ret := make(map[string]string)
|
||||
result.ForEach(func(key, value gjson.Result) bool {
|
||||
ret[key.String()] = value.String()
|
||||
return true
|
||||
})
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
33
pkg/utils/hfs.go
Normal file
33
pkg/utils/hfs.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
// HybridFS combines embed.FS and os.DirFS.
|
||||
type HybridFS struct {
|
||||
embedFS, dir fs.FS
|
||||
}
|
||||
|
||||
func NewHybridFS(embed embed.FS, subDir string, localDir string) (*HybridFS, error) {
|
||||
subFS, err := fs.Sub(embed, subDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &HybridFS{
|
||||
embedFS: subFS,
|
||||
dir: os.DirFS(localDir),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (hfs *HybridFS) Open(name string) (fs.File, error) {
|
||||
// Ensure embed files are not replaced
|
||||
if file, err := hfs.embedFS.Open(name); err == nil {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
return hfs.dir.Open(name)
|
||||
}
|
||||
Reference in New Issue
Block a user