目录
准备
一键开启美好:
go get -t github.com/jialanli/windward
或
cd $GOPATH/src/github.com
git clone https://github.com/jialanli/windward.git v1.0.1
方便、舒适、高效、简洁
支持批量配置不同类型文件读取,举例如下:
工程目录目标配置文件1,c.json
{
"name": "你好",
"id": 25,
"type": "哈哈",
"class": {
"data": "A",
"ins": "无名"
},
"other": [
{
"s1": "aaa",
"s2": 55
},
{
"s1": "aaaaa",
"s2": 99
}
]
}
目标配置文件2,c.yml
apiVersion: v1
kind: Service
metadata:
name: wordpress3
namespace: default
spec:
ports:
- port: 19987
protocol: TCP
targetPort: 19987
sessionAffinity: None
type: ClusterIP
验证
读取&注入配置:
import (
"fmt"
"github.com/jialanli/windward"
)
// 全部配置注入时定义对应结构体
type metadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
type ports struct {
TargetPort int `yaml:"targetPort"`
Port int `yaml:"port"`
Protocol string `yaml:"protocol"`
}
type spec struct {
Ports []ports `yaml:"ports"`
SessionAffinity string `yaml:"sessionAffinity"`
Type string `yaml:"type"`
}
type YamlDemo struct {
Kind string `yaml:"kind"`
Metadata metadata `yaml:"metadata"`
Spec spec `yaml:"spec"`
ApiVersion string `yaml:"apiVersion"`
}
type other struct {
S1 string `json:"s1"`
S2 int `json:"s2"`
}
type JsonDemo struct {
Name string `json:"name"`
ID int `json:"id"`
Type string `json:"type"`
Class struct {
Data string `json:"data"`
Ins string `json:"ins"`
} `json:"class"`
Other []other `json:"other"`
}
func main() {
name, name1 := "./conf/c.json", "./conf/c.yml" // 自定义配置文件所在目录
w := windward.GetWindward()
w.InitConf([]string{name, name1}) // 初始化自定义的配置文件
var data JsonDemo
err := w.ReadConfig(name, &data) // json配置文件的全部配置
fmt.Printf("\nJSON:err:%v \t data.id:%+v", err, data.ID)
var dataYaml YamlDemo
err = w.ReadConfig(name1, &dataYaml) // yml配置文件的全部配置
fmt.Printf("\nYAML:err:%v \tspec.type:%+v", err, dataYaml.Spec.Type)
fmt.Println("\nmetadata.namespace-->", dataYaml.Metadata.Namespace) //
// 指定单个配置获取
c0 := w.GetVal(name, "name")
fmt.Println("name-->", c0) //
c1 := w.GetVal(name, "id")
fmt.Println("id-->", c1) //
c2 := w.GetVal(name, "type")
fmt.Println("type-->", c2) //
c3 := w.GetVal(name, "class")
fmt.Println("class-->", c3) //
c4 := w.GetVal(name, "class.data")
fmt.Println("class.data-->", c4) //
c5 := w.GetVal(name, "class.ins")
fmt.Println("class.ins-->", c5) //
name0 := w.GetVal(name1, "metadata.name")
fmt.Println("metadata.name-->", name0) //
namespace := w.GetVal(name1, "metadata.namespace")
fmt.Println("metadata.namespace-->", namespace) //
ports := w.GetVal(name1, "spec.ports")
fmt.Println("spec.ports-->", ports) //
port := w.GetVal(name1, "spec.ports.port")
fmt.Println("spec.ports.port-->", port) //
protocol := w.GetVal(name1, "spec.ports.protocol")
fmt.Println("spec.ports.protocol-->", protocol) //
targetPort := w.GetVal(name1, "spec.ports.targetPort")
fmt.Println("spec.ports.targetPort-->", targetPort) //
sessionAffinity := w.GetVal(name1, "spec.sessionAffinity")
fmt.Println("spec.sessionAffinity-->", sessionAffinity) //
types := w.GetVal(name1, "spec.type")
fmt.Println("spec.type-->", types) //
}
/*
JSON:err:<nil> data.id:25
YAML:err:<nil> spec.type:ClusterIP
metadata.namespace--> default
name--> 你好
id--> 25
type--> 哈哈
class--> map[data:A ins:无名]
class.data--> A
class.ins--> 无名
metadata.name--> wordpress3
metadata.namespace--> default
spec.ports--> [map[port:19987 protocol:TCP targetPort:19987]]
spec.ports.port--> 19987
spec.ports.protocol--> TCP
spec.ports.targetPort--> 19987
spec.sessionAffinity--> None
spec.type--> ClusterIP
*/