4 Star 5 Fork 4

Plato/Service-Box-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
file.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
CloudGuan 提交于 2022-11-01 13:58 . update: 配置系统支持apollo
package file
import (
"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 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}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dennis-kk/service-box-go.git
git@gitee.com:dennis-kk/service-box-go.git
dennis-kk
service-box-go
Service-Box-go
v0.4.11

搜索帮助