1 Star 0 Fork 0

qp84/aiui-iot-core

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
file.go 1.18 KB
Copy Edit Raw Blame History
cnxkey authored 2025-02-14 19:12 +08:00 . aiui-iot-core
// Package file is a file source. Expected format is json
package file
import (
source2 "gitee.com/qp84/aiui-iot-core/config/tool/source"
"io/ioutil"
"os"
)
type file struct {
path string
opts source2.Options
}
var (
DefaultPath = "config.json"
)
func (f *file) Read() (*source2.ChangeSet, error) {
fh, err := os.Open(f.path)
if err != nil {
return nil, err
}
defer fh.Close()
b, err := ioutil.ReadAll(fh)
if err != nil {
return nil, err
}
info, err := fh.Stat()
if err != nil {
return nil, err
}
cs := &source2.ChangeSet{
Format: format(f.path, f.opts.Encoder),
Source: f.String(),
Timestamp: info.ModTime(),
Data: b,
}
cs.Checksum = cs.Sum()
return cs, nil
}
func (f *file) String() string {
return "file"
}
func (f *file) Watch() (source2.Watcher, error) {
if _, err := os.Stat(f.path); err != nil {
return nil, err
}
return newWatcher(f)
}
func (f *file) Write(cs *source2.ChangeSet) error {
return nil
}
func NewSource(opts ...source2.Option) source2.Source {
options := source2.NewOptions(opts...)
path := DefaultPath
f, ok := options.Context.Value(filePathKey{}).(string)
if ok {
path = f
}
return &file{opts: options, path: path}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qp84/aiui-iot-core.git
git@gitee.com:qp84/aiui-iot-core.git
qp84
aiui-iot-core
aiui-iot-core
v1.0.2

Search