golang 解析CFG文件,有没有类似解析json的包
本文介绍了golang 解析CFG文件,有没有类似解析json的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
1.解析cfg文件,形成struct
2.cfg文件格式
virtual_server {
label JuXG-HTTPS
ip 123.103.91.122
port 443
lb_algo rr
lb_kind tun
protocol TCP
real_server {
label RealServer1
ip 123.103.91.115
port 443
weight 100
HTTP_GET {
check_port 80
path 'health'
http_recv 'Welcome to nginx'
connect_timeout 3
}
}
real_server {
label RealServer2
ip 123.103.91.116
port 443
weight 100
HTTP_GET {
check_port 80
path 'health'
http_recv 'Welcome to nginx'
connect_timeout 3
}
}
}
3.这个cfg文件取自lvs配置,我想获取里面的数据,想问大家有没有方法
解决方案
不好意思才来 SegmentFault
,貌似看到有点晚了,顺手写了一个,权当冲冲人气了,?
写了一段解析的代码,代码中所有配置项值都被解析成字符串
,具体见代码
实现代码
package lvs
import (
"fmt"
"strings"
)
//ConfigBlock 配置块结构
type ConfigBlock map[string]interface{}
//Unmarshal 解析配置
func Unmarshal(config string) (*ConfigBlock, error) {
block := ConfigBlock{}
r := strings.NewReader(config)
contextStack := []*ConfigBlock{&block}
previous := ""
current := ""
serverID := -1
for r.Len() > 0 {
char, _, _ := r.ReadRune()
m := string(char)
switch m {
case "\t":
fallthrough
case " ": //当前字符为空
if current == "" {
continue
}
//得到了一个key,或者value
previous = current
current = ""
case "{": //遇到{进行切换到下部context
newConfigBlock := &ConfigBlock{}
if previous == "real_server" {
serverID++
previous = fmt.Sprintf("real_server_%d", serverID)
}
(*contextStack[len(contextStack)-1])[previous] = newConfigBlock
contextStack = append(contextStack, newConfigBlock)
previous = ""
current = ""
case "}": //遇到}进行切换回上层context
contextStack = contextStack[:len(contextStack)-1]
previous = ""
current = ""
case "\n": //遇到换行了
if previous != "" && current != "" {
(*contextStack[len(contextStack)-1])[previous] = strings.Trim(current, `'"`)
}
previous = ""
current = ""
default:
current += m
}
}
//转换real_server 为数组
if serverID > -1 {
serverBlocks := []interface{}{}
vertualServer := block["virtual_server"].(*ConfigBlock)
for i := 0; i <= serverID; i++ {
key := fmt.Sprintf("real_server_%d", i)
serverBlocks = append(serverBlocks, (*vertualServer)[key])
delete(*vertualServer, key)
}
(*vertualServer)["real_server"] = serverBlocks
}
return &block, nil
}
安装
go get github.com/chekun/code-snippets/go/lvs
使用方法
package main
import (
"fmt"
"encoding/json"
"github.com/chekun/code-snippets/go/lvs"
)
func main() {
config := `
virtual_server {
label JuXG-HTTPS
ip 123.103.91.122
port 443
lb_algo rr
lb_kind tun
protocol TCP
real_server {
label RealServer1
ip 123.103.91.115
port 443
weight 100
HTTP_GET {
check_port 80
path 'health'
http_recv 'Welcome to nginx'
connect_timeout 3
}
}
real_server {
label RealServer2
ip 123.103.91.116
port 443
weight 100
HTTP_GET {
check_port 80
path 'health'
http_recv 'Welcome to nginx'
connect_timeout 3
}
}
}`
r, _ := lvs.Unmarshal(config)
rJSON, _ := json.Marshal(r)
fmt.Println(string(rJSON))
}
示例代码输出
{
"virtual_server": {
"ip": "123.103.91.122",
"label": "JuXG-HTTPS",
"lb_algo": "rr",
"lb_kind": "tun",
"port": "443",
"protocol": "TCP",
"real_server": [
{
"HTTP_GET": {
"check_port": "80",
"connect_timeout": "3",
"path": "health",
"to": "nginx"
},
"ip": "123.103.91.115",
"label": "RealServer1",
"port": "443",
"weight": "100"
},
{
"HTTP_GET": {
"check_port": "80",
"connect_timeout": "3",
"path": "health",
"to": "nginx"
},
"ip": "123.103.91.116",
"label": "RealServer2",
"port": "443",
"weight": "100"
}
]
}
}
这篇关于golang 解析CFG文件,有没有类似解析json的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文