登录
首页 >  Golang >  Go问答

golang 解析CFG文件,有没有类似解析json的包

来源:SegmentFault

时间:2023-01-13 13:28:34 138浏览 收藏

本篇文章向大家介绍《golang 解析CFG文件,有没有类似解析json的包》,主要包括go,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

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 

安装

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"
            }
        ]
    }
}

Github代码记录

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表