doupao1530 2018-07-10 17:05
浏览 316
已采纳

Golang Map YAML对象

I have a yaml configuration file that contains sets of configuration commands to send to network devices. Inside of each set, there are vendor-specific keys and the values for each vendor key can be either a configuration command string, a list of configuration command strings, or a list of key-value pairs mapping a vendor-specific model string to a configuration command string. Below is an example:

# example.yml
---
cmds:
  setup:
    cisco: "terminal length 0"
  config:
    cisco:
      - basic  : "show version"
      - basic  : "show boot"
        "3560" : "3560 boot command"
        "2960x": "2960x boot command"
      - basic  : "dir flash:"
        "3560" : "3560 dir command"
  cleanup:
    cisco: ["terminal no length", "quit"]

I want to combine these commands into a map like so:

var cmdMap = map[string][]string{
    "cisco": []string{
        "terminal length 0",
        "show version",
        "show boot",
        "dir flash:",
        "terminal no length",
        "quit",
    },
    "cisco.3560": []string{
        "terminal length 0",
        "show version",
        "3560 boot command",
        "3560 dir command",
        "terminal no length",
        "quit",
    },
    "cisco.2960x": []string{
        "terminal length 0",
        "show version",
        "2960x boot command",
        "dir flash:",
        "terminal no length",
        "quit",
    }
}

I am using spf13/viper to handle parsing the yaml file and have been able to add the specific commands to each vendor and model, but adding the commands that apply to both vendor and specific model is where I am stuck. This is the actual output of my program followed by my code:

$ go run main.go example.yml 
cmdMap["cisco"]
terminal length 0
show version
show boot
dir flash:
terminal no length
quit

# missing terminal length 0, show version, etc.
cmdMap["cisco.3560"]
3560 boot command
3560 dir command

# missing terminal length 0, show version, etc.
cmdMap["cisco.2960x"]
2960x boot command

My code:

package main

import (
    "github.com/spf13/viper"
    "fmt"
    "flag"
    "log"
)

func main() {
    flag.Parse()
    cfgFile := flag.Arg(0)

    v := viper.New()
    v.SetConfigType("yaml")
    v.SetConfigFile(cfgFile)

    if err := v.ReadInConfig(); err != nil {
        log.Fatal(err)
    }

    for k, v := range MapCfgCmds(v.GetStringMap("cmds")) {
        fmt.Printf("cmdMap[\"%s\"]
", k)
        for _, cmd := range v {
            fmt.Println(cmd)
        }
        fmt.Println()
    }
}

func MapCfgCmds(cfgCmds map[string]interface{}) map[string][]string {
    cmdMap := make(map[string][]string)
    for _, cmdSet := range cfgCmds {
        cmdSet, _ := cmdSet.(map[string]interface{})
        for vendor, cmdList := range cmdSet {
            switch cmds := cmdList.(type) {
            case string:
                // single string command (i.e., vendor: cmd)
                cmdMap[vendor] = append(cmdMap[vendor], cmds)
            case []interface{}:
                for _, cmd := range cmds {
                    switch c := cmd.(type) {
                    case string:
                        // list of strings (i.e., vendor: [cmd1,cmd2,...,cmdN])
                        cmdMap[vendor] = append(cmdMap[vendor], c)
                    case map[interface{}]interface{}:
                        // This is where I am stuck
                        //
                        // list of key-value pairs (i.e., vendor: {model: modelCmd})
                        for model, modelCmd := range c {
                            modelCmd, _ := modelCmd.(string)
                            if model == "basic" {
                                cmdMap[vendor] = append(cmdMap[vendor], modelCmd)
                                continue
                            }
                            modelKey := fmt.Sprintf("%s.%s", vendor, model)
                            cmdMap[modelKey] = append(cmdMap[modelKey], modelCmd)
                        }
                    }
                }
            }
        }
    }
    return cmdMap
}

How can I combine the "universal" and model-specific commands to get the expected cmdMap value from above?

  • 写回答

2条回答 默认 最新

  • dousi4257 2018-07-11 08:33
    关注

    I think viper is not helping you here, in the sense that viper does a lot of things you don't need, but it doesn't do one thing you could use here - clear mapping of the data. If you use the yaml library directly, you could declare a structure that corresponds to your data and makes it easier to understand it.

    There are several possible approaches to your problem, here is my attempt at solving it (you might need to tweak few things as I wrote it in the editor, without compiling it):

        type Data struct {
           Cmds struct {
             Setup map[string]interface{} `yaml:"setup"`
             Config map[string][]map[string]string `yaml:"config"`
             Cleanup map[string][]string `yaml:"cleanup"`
           } `yaml:"cmds"`
    
        }   
    
        data := Data{}
        err := yaml.Unmarshal([]byte(input), &data)
        if err != nil {
                log.Fatalf("error: %v", err)
        }
    
        setupCmds := make(map[string][]string)
        cleanupCmds := make(map[string][]string)
    
        result := make(map[string][]string)
    
    
        // Prepare setup commands, grouped by vendor
        for vendor, setupCmd := range data.Cmds.Setup {
            setupCmds[vendor] = append(setupCmds[vendor], setupCmd)
        }
    
        // Prepare cleanup commands, grouped by vendor
         for vendor, commands := range data.Cmds.Cleanup {
            cleanupCmds[vendor] = append(cleanupCmds[vendor], commands...)
        }
    
        // iterate over vendors and models, combine with setup & cleanup commands and store in result
        for vendor, configCmds := range data.Cmds.Config { // vendor = string (e.g. "cisco"), configCmds = []map[string][string] (e.g. - basic: "show version")
            // we now how many config commands there will be
            result[vendor] = make([]string, len(configCmds))
    
            // variantsCache will store all variants we've seen so far
            variantsCache := make(map[string]struct{})
            for i, model := range models { // i = int (number of command) model = map[string]string
                // we assume "basic" is available for each command
                result[vendor][i] = model["basic"]
                for variant, command := range model { // variant = string (e.g. "basic"), command = string (e.g. "show version")
                    if variant == "basic" {
                        // we already covered that
                        continue
                    }
                    variantKey := vendor + "." + variant
                    variantsCache[variantKey]
                    if _, ok := result[variantKey]; !ok {
                        // first command for this model, create a slice
                        result[variantKey] = make([]string, len(configCmds))
                    }
                    result[variantKey][i] = command
                }
            }
            // We need to iterate over all commands for all variants and copy "basic" command if there is none
            for variant, _ := range variantsCache {
                for i, command := range result[variant] {
                    if command == "" {
                        // copy the "basic" command, since there was no variant specific command
                        result[variant][i] = result[vendor][i]
                    }
                } 
            }
        }
    
        // combine setup and cleanup with config
        for variant, _ := result {
            // will return "cisco" for both "cisco" and "cisco.x3650"
            vendor := strings.Split(variant, ".")[0] 
            result[variant] = append(setupCmds[vendor], result[variant]...)
            result[variant] = append(result[variant], cleanupCmds[vendor]...)
        }
    
        return result
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?