代码拉取完成,页面将自动刷新
package file
import (
"errors"
"gitee.com/dennis-kk/service-box-go/util/config/encoder"
"gitee.com/dennis-kk/service-box-go/util/config/source"
"io/ioutil"
"os"
"strings"
)
func format(p string, e encoder.Encoder) string {
parts := strings.Split(p, ".")
if len(parts) > 1 {
return parts[len(parts)-1]
}
return e.String()
}
type file struct {
path string
opts *source.Options
}
func (f *file) Read() (*source.ChangeSet, error) {
fh, err := os.Open(f.path)
if err != nil {
return nil, err
}
defer fh.Close()
//read binary array form file
b, err := ioutil.ReadAll(fh)
if err != nil {
return nil, err
}
//get file description
info, err := fh.Stat()
if err != nil {
return nil, err
}
//处理环境变量,替换环境变量为对应的值
b = []byte(os.ExpandEnv(string(b)))
cs := &source.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() (source.Watcher, error) {
return nil, errors.New("file source not support watch yet ")
}
func NewSource(opts ...source.Option) source.Source {
options := source.NewOptions(opts...)
path := "default.yaml"
f, ok := options.Context.Value(filePathKey{}).(string)
if ok {
path = f
}
return &file{opts: options, path: path}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。