本文整理汇总了Golang中github.com/BurntSushi/toml.Decode函数的典型用法代码示例。如果您正苦于以下问题:Golang Decode函数的具体用法?Golang Decode怎么用?Golang Decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Decode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewConfigFile
func NewConfigFile() (*ConfigFile, error) {
source, err := ioutil.ReadFile(CLIConfig.ConfigFileLocation)
if err != nil {
if CLIConfig.IsPiping || CLIConfig.IsNotifying {
return &ConfigFile{
Data: DataConfig{},
Graphite: GraphiteConfig{},
JobsField: []Job{},
}, nil
}
return nil, errors.New(fmt.Sprintf("Unable to open configuration file at %s. Did you use --config to specify the right path?\n\n", CLIConfig.ConfigFileLocation))
}
result := &ConfigFile{}
_, err = toml.Decode(string(source), result)
for _, job := range result.FlowField {
result.JobsField = append(result.JobsField, job)
}
return result, err
}
示例2: Export
// Retuns the configuration data into a generic object for for us.
func (cr *ConfigReader) Export() (interface{}, error) {
var config interface{}
buf := new(bytes.Buffer)
buf.ReadFrom(cr.reader)
switch cr.Format {
case "yaml":
if err := yaml.Unmarshal(buf.Bytes(), &config); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err)
}
case "json":
if err := json.Unmarshal(buf.Bytes(), &config); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err)
}
case "toml":
if _, err := toml.Decode(buf.String(), &config); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err)
}
default:
return nil, err.UnsupportedConfigError(cr.Format)
}
return config, nil
}
示例3: main
func main() {
configFile := flag.String("config", "benchmark_config.sample.toml", "Config file")
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
data, err := ioutil.ReadFile(*configFile)
if err != nil {
panic(err)
}
var conf benchmarkConfig
if _, err := toml.Decode(string(data), &conf); err != nil {
panic(err)
}
logFile, err := os.OpenFile(conf.LogFile, os.O_RDWR|os.O_CREATE, 0660)
if err != nil {
panic(fmt.Sprintf("Error opening log file \"%s\": %s", conf.LogFile, err))
}
conf.Log = logFile
defer logFile.Close()
fmt.Println("Logging benchmark results to ", conf.LogFile)
logFile.WriteString("Starting benchmark run...\n")
harness := NewBenchmarkHarness(&conf)
startTime := time.Now()
harness.Run()
elapsed := time.Now().Sub(startTime)
message := fmt.Sprintf("Finished in %.3f seconds\n", elapsed.Seconds())
fmt.Printf(message)
logFile.WriteString(message)
}
示例4: ParseDbConfig
func ParseDbConfig(path string) (*DbConfig, error) {
conf := &DbConfig{}
content, err := ioutil.ReadFile(path + "/config.toml")
if err != nil {
return nil, errors.New(fmt.Sprintf("Error while reading config.toml configuration: %v", err))
}
_, err = toml.Decode(string(content), &conf)
if err != nil {
return nil, err
}
if conf.User == "" {
conf.User = "postgres"
}
if conf.Host == "" {
conf.Host = "127.0.0.1"
}
if conf.Port == 0 {
conf.Port = 5432
}
if conf.SslMode == "" {
conf.SslMode = "disable"
}
return conf, nil
}
示例5: TestConfig_Parse
func TestConfig_Parse(t *testing.T) {
// Parse configuration.
var c httpd.Config
if _, err := toml.Decode(`
enabled = true
bind-address = ":8080"
auth-enabled = true
log-enabled = true
write-tracing = true
pprof-enabled = true
https-enabled = true
https-certificate = "/dev/null"
`, &c); err != nil {
t.Fatal(err)
}
// Validate configuration.
if c.Enabled != true {
t.Fatalf("unexpected enabled: %v", c.Enabled)
} else if c.BindAddress != ":8080" {
t.Fatalf("unexpected bind address: %s", c.BindAddress)
} else if c.AuthEnabled != true {
t.Fatalf("unexpected auth enabled: %v", c.AuthEnabled)
} else if c.LogEnabled != true {
t.Fatalf("unexpected log enabled: %v", c.LogEnabled)
} else if c.WriteTracing != true {
t.Fatalf("unexpected write tracing: %v", c.WriteTracing)
} else if c.PprofEnabled != true {
t.Fatalf("unexpected pprof enabled: %v", c.PprofEnabled)
} else if c.HTTPSEnabled != true {
t.Fatalf("unexpected https enabled: %v", c.HTTPSEnabled)
} else if c.HTTPSCertificate != "/dev/null" {
t.Fatalf("unexpected https certificate: %v", c.HTTPSCertificate)
}
}
示例6: TestConfig_Parse
func TestConfig_Parse(t *testing.T) {
// Parse configuration.
var c httpd.Config
if _, err := toml.Decode(`
enabled = true
bind-address = ":8080"
auth-enabled = true
log-enabled = true
write-tracing = true
pprof-enabled = true
max-connections = 5000
`, &c); err != nil {
t.Fatal(err)
}
// Validate configuration.
if c.Enabled != true {
t.Fatalf("unexpected enabled: %v", c.Enabled)
} else if c.BindAddress != ":8080" {
t.Fatalf("unexpected bind address: %s", c.BindAddress)
} else if c.AuthEnabled != true {
t.Fatalf("unexpected auth enabled: %v", c.AuthEnabled)
} else if c.LogEnabled != true {
t.Fatalf("unexpected log enabled: %v", c.LogEnabled)
} else if c.WriteTracing != true {
t.Fatalf("unexpected write tracing: %v", c.WriteTracing)
} else if c.PprofEnabled != true {
t.Fatalf("unexpected pprof enabled: %v", c.PprofEnabled)
} else if c.MaxConnections != 5000 {
t.Fatalf("unexpected max connections: %v", c.MaxConnections)
}
}
示例7: handleTomlMetaData
func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
if _, err := toml.Decode(string(datum), &m); err != nil {
return m, fmt.Errorf("Invalid TOML in %s \nError parsing page meta data: %s", page.FileName, err)
}
return m, nil
}
示例8: NewConfig
// NewConfig reads configuration from path. The format is deductted from file extension
// * .json - is decoded as json
// * .yml - is decoded as yaml
// * .toml - is decoded as toml
func NewConfig(path string) (*Config, error) {
_, err := os.Stat(path)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cfg := &Config{}
switch filepath.Ext(path) {
case ".json":
jerr := json.Unmarshal(data, cfg)
if jerr != nil {
return nil, jerr
}
case ".toml":
_, terr := toml.Decode(string(data), cfg)
if terr != nil {
return nil, terr
}
case ".yml":
yerr := yaml.Unmarshal(data, cfg)
if yerr != nil {
return nil, yerr
}
default:
return nil, errors.New("utron: config file not supported")
}
return cfg, nil
}
示例9: FromToml
// 解析toml字符串
func (config *Configuration) FromToml(data string) error {
config.mutex.Lock()
defer config.mutex.Unlock()
_, err := toml.Decode(data, &config.parameters)
return err
}
示例10: parseTomlConfiguration
func parseTomlConfiguration(filename string) (*Configuration, error) {
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
tomlConfiguration := &TomlConfiguration{}
_, err = toml.Decode(string(body), tomlConfiguration)
if err != nil {
return nil, err
}
config := &Configuration{
AdminHttpPort: tomlConfiguration.Admin.Port,
AdminAssetsDir: tomlConfiguration.Admin.Assets,
ApiHttpPort: tomlConfiguration.Api.Port,
RaftServerPort: tomlConfiguration.Raft.Port,
RaftDir: tomlConfiguration.Raft.Dir,
ProtobufPort: tomlConfiguration.Cluster.ProtobufPort,
SeedServers: tomlConfiguration.Cluster.SeedServers,
DataDir: tomlConfiguration.Storage.Dir,
LogFile: tomlConfiguration.Logging.File,
LogLevel: tomlConfiguration.Logging.Level,
Hostname: tomlConfiguration.Hostname,
}
return config, nil
}
示例11: TestConfig_Parse
func TestConfig_Parse(t *testing.T) {
// Parse configuration.
var c opentsdb.Config
if _, err := toml.Decode(`
enabled = true
bind-address = ":9000"
database = "xxx"
consistency-level ="all"
tls-enabled = true
certificate = "/etc/ssl/cert.pem"
log-point-errors = true
`, &c); err != nil {
t.Fatal(err)
}
// Validate configuration.
if c.Enabled != true {
t.Fatalf("unexpected enabled: %v", c.Enabled)
} else if c.BindAddress != ":9000" {
t.Fatalf("unexpected bind address: %s", c.BindAddress)
} else if c.Database != "xxx" {
t.Fatalf("unexpected database: %s", c.Database)
} else if c.ConsistencyLevel != "all" {
t.Fatalf("unexpected consistency-level: %s", c.ConsistencyLevel)
} else if c.TLSEnabled != true {
t.Fatalf("unexpected tls-enabled: %v", c.TLSEnabled)
} else if c.Certificate != "/etc/ssl/cert.pem" {
t.Fatalf("unexpected certificate: %s", c.Certificate)
} else if !c.LogPointErrors {
t.Fatalf("unexpected log-point-errors: %v", c.LogPointErrors)
}
}
示例12: TestConfig_Parse_EnvOverride
// Ensure the configuration can be parsed.
func TestConfig_Parse_EnvOverride(t *testing.T) {
// Parse configuration.
var c run.Config
if _, err := toml.Decode(`
[replay]
dir = "/tmp/replay"
[task]
dir = "/tmp/task"
`, &c); err != nil {
t.Fatal(err)
}
if err := os.Setenv("KAPACITOR_REPLAY_DIR", "/var/lib/kapacitor/replay"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}
if err := os.Setenv("KAPACITOR_TASK_DIR", "/var/lib/kapacitor/task"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}
if err := c.ApplyEnvOverrides(); err != nil {
t.Fatalf("failed to apply env overrides: %v", err)
}
// Validate configuration.
if c.Replay.Dir != "/var/lib/kapacitor/replay" {
t.Fatalf("unexpected replay dir: %s", c.Replay.Dir)
} else if c.Task.Dir != "/var/lib/kapacitor/task" {
t.Fatalf("unexpected task dir: %s", c.Task.Dir)
}
}
示例13: LoadConfig
// LoadConfig initializes the confd configuration by first setting defaults,
// then overriding setting from the confd config file, and finally overriding
// settings from flags set on the command line.
// It returns an error if any.
func LoadConfig(path string) error {
setDefaults()
if path == "" {
log.Warning("Skipping confd config file.")
} else {
log.Debug("Loading " + path)
configBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
_, err = toml.Decode(string(configBytes), &config)
if err != nil {
return err
}
}
processFlags()
if !isValidateEtcdScheme(config.Confd.EtcdScheme) {
return errors.New("Invalid etcd scheme: " + config.Confd.EtcdScheme)
}
err := setEtcdHosts()
if err != nil {
return err
}
return nil
}
示例14: ParseConfig
// ParseConfig parses a configuration string into a config object.
func ParseConfig(s string) (*Config, error) {
c := NewConfig()
if _, err := toml.Decode(s, &c); err != nil {
return nil, err
}
return c, nil
}
示例15: getConfiguration
func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
var (
buf []byte
err error
)
configuration := new(types.Configuration)
tmpl := template.New(p.Filename).Funcs(funcMap)
if len(p.Filename) > 0 {
buf, err = ioutil.ReadFile(p.Filename)
if err != nil {
return nil, err
}
} else {
buf, err = autogen.Asset(defaultTemplateFile)
if err != nil {
return nil, err
}
}
_, err = tmpl.Parse(string(buf))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, templateObjects)
if err != nil {
return nil, err
}
if _, err := toml.Decode(buffer.String(), configuration); err != nil {
return nil, err
}
return configuration, nil
}