/
env.go
49 lines (41 loc) · 1.16 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package client
import (
"context"
"os"
"strings"
"gopkg.in/yaml.v2"
)
const (
Env = "env"
)
var (
_ Client = (*envClient)(nil)
)
func init() {
_ = RegisterClient(Env, newEnvClient)
}
type envClient struct{}
func (c *envClient) Close() error { return nil }
func (c *envClient) Pull(ctx context.Context, path string) (data []byte, err error) {
ps := map[string]interface{}{}
environ := os.Environ()
for _, item := range environ {
fullExpr := []rune(item)
idx := strings.Index(item, "=")
envKey := string(fullExpr[0:idx])
propsKey := strings.Replace(envKey, "_", ".", -1)
propsKey = strings.ToLower(propsKey)
// 保存原始配置和变换后的配置
ps[envKey] = string(fullExpr[idx+1:])
if propsKey[0] != '.' {
ps[propsKey] = ps[envKey]
}
}
data, err = yaml.Marshal(ps)
return
}
func (c *envClient) Push(ctx context.Context, path string, data []byte) error { return nil }
func (c *envClient) Watch(path string, cb ChangedCallback) error { return nil }
func newEnvClient(cfg *Config) (Client, error) {
return &envClient{}, nil
}