doutui9606 2016-01-02 17:28
浏览 90

使用Viper初始化新的Go配置

I've been exploring configuration management in Go using the viper package, which I learned about in another question. I'm having trouble understanding how to initialize a new configuration. What I want to do is to find system configurations if they exist, then user configuration, and then, if all else fails, to create (and then use) a new user configuration from defaults. The next time I run the app, I'd expect to find the configuration that I previously created. I'm using it like this:

import (
    "fmt"
    "github.com/spf13/viper"
    "os"
    "os/user"
    "path/filepath"
)

usr, err := user.Current()
appMode := "test" // or "production" or...
configDir := filepath.Join(usr.HomeDir, ".myapp")
config := viper.New()

config.SetConfigName(appMode)
config.SetConfigType("json")

config.SetDefault("group1.key1", "value1.1")
config.SetDefault("group1.key2", 1234)
config.SetDefault("group2.key1", "value2.1")
config.SetDefault("group2.key2", 5678)

config.AddConfigPath("/usr/share/myapp/config")
config.AddConfigPath("/usr/local/share/myapp/config")
config.AddConfigPath(configDir)

if err := config.ReadInConfig(); err != nil {
    filename := filepath(configDir, fmt.Sprintf("%s.json", appMode))
    _, err := os.Create(filename)
    if err != nil {
        panic(fmt.Stringf("Failed to create %s", filename))
    }
}

if err := config.ReadInConfig(); err != nil {
    panic("Created %s, but Viper failed to read it: %s",
          filename, err)
}

At this point, I would like for ~/.myapp/test.json to be created for me with the following:

{
    "group1": {
        "key1": "value1.1",
        "key2": 1234
    },
    "group2": {
        "key1": "value2.1",
        "key2": 5678
    }
}

The result is that the file is empty, and the second attempt to read the file also fails with the message "open : no such file or directory" even though it exists. If I hand-edit the file, Viper finds it and parses it. Obviously I can create the JSON file programatically, but this seems like such an obvious use case that I must be missing something here.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据