本文整理汇总了Golang中github.com/BurntSushi/toml.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: StoreResponseFile
func StoreResponseFile(path string, resp []Response) error {
sort.Sort(Responses(resp))
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(responses{resp}); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
示例2: saveToFile
// saveToFile saves the Config in the file named path. This is a helper method
// for generating sample configuration files.
func (c *Config) saveToFile(path string) error {
var data []byte
switch filepath.Ext(path) {
case ".json":
d, err := json.MarshalIndent(c, "", "\t") // use tab indent to make it human friendly
if err != nil {
return err
}
data = d
case ".yml":
d, err := yaml.Marshal(c)
if err != nil {
return err
}
data = d
case ".toml":
b := &bytes.Buffer{}
err := toml.NewEncoder(b).Encode(c)
if err != nil {
return err
}
data = b.Bytes()
}
return ioutil.WriteFile(path, data, 0600)
}
示例3: String
func (c *Config) String() string {
var b bytes.Buffer
e := toml.NewEncoder(&b)
e.Indent = " "
e.Encode(c)
return b.String()
}
示例4: CreateConfig
func (c *Config) CreateConfig() error {
var yahooConfig YahooConfig
fmt.Print("Input YahooToken: ")
yahooConfig.Token = c.scan()
var slackConfig SlackConfig
fmt.Print("Input SlackToken: ")
slackConfig.Token = c.scan()
fmt.Print("Input SlackChannel: ")
slackConfig.Channel = c.scan()
fmt.Print("Input SlackNDaysAgo: ")
nDaysAgo, err := strconv.Atoi(c.scan())
if err != nil {
return err
}
slackConfig.NDaysAgo = nDaysAgo
var generalConfig GeneralConfig
fmt.Print("Input Upload Filename (.gif): ")
generalConfig.Filename = c.scan()
c.Yahoo = yahooConfig
c.Slack = slackConfig
c.General = generalConfig
var buffer bytes.Buffer
encoder := toml.NewEncoder(&buffer)
if err := encoder.Encode(c); err != nil {
return err
}
ioutil.WriteFile(configFile, []byte(buffer.String()), 0644)
return nil
}
示例5: Toml
func Toml(w http.ResponseWriter, r *http.Request) {
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
mib := conf.MIB{}
err = json.Unmarshal(buf, &mib)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
meta := &struct{ Name string }{}
err = json.Unmarshal(buf, meta)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
toToml := struct {
MIBs map[string]conf.MIB
}{MIBs: map[string]conf.MIB{meta.Name: mib}}
toml.NewEncoder(w).Encode(toToml)
}
示例6: StorePolynomialFile
func StorePolynomialFile(path string, pols []Polynomial) error {
sort.Sort(Polynomials(pols))
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(polynomials{pols}); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
示例7: Marshal
func (c *Config) Marshal() []byte {
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(c); err != nil {
panic(err)
}
return buf.Bytes()
}
示例8: TestLoadQuery
func TestLoadQuery(t *testing.T) {
queries := &Queries{}
for i := 0; i < 10; i++ {
query := Query{
Name: fmt.Sprintf("name_%d", i+1),
SQL: fmt.Sprintf("sql_%d", i+1),
}
queries.Query = append(queries.Query, query)
}
tempFile, err := ioutil.TempFile("", utils.GetAppName()+"-test")
if err != nil {
t.Errorf("failed to create the temp file: %s", err.Error())
}
if err := toml.NewEncoder(tempFile).Encode(queries); err != nil {
t.Errorf("failed to create the toml file: %s", err.Error())
}
tempFile.Sync()
tempFile.Close()
defer os.Remove(tempFile.Name())
query, err := LoadQuery(tempFile.Name())
if err != nil {
t.Errorf("query file load error: %s", err.Error())
}
if !reflect.DeepEqual(queries, query) {
t.Errorf("query data not match: %+v/%+v", queries, query)
}
}
示例9: create_config_files
func create_config_files(nr int, outputDir string) {
quaggaConfigList := make([]*QuaggaConfig, 0)
gobgpConf := config.Bgp{}
gobgpConf.Global.Config.As = 65000
gobgpConf.Global.Config.RouterId = "192.168.255.1"
for i := 1; i < nr+1; i++ {
c := config.Neighbor{}
c.Config.PeerAs = 65000 + uint32(i)
c.Config.NeighborAddress = fmt.Sprintf("10.0.0.%d", i)
c.Config.AuthPassword = fmt.Sprintf("hoge%d", i)
gobgpConf.Neighbors = append(gobgpConf.Neighbors, c)
q := NewQuaggaConfig(i, &gobgpConf.Global, &c, net.ParseIP("10.0.255.1"))
quaggaConfigList = append(quaggaConfigList, q)
os.Mkdir(fmt.Sprintf("%s/q%d", outputDir, i), 0755)
err := ioutil.WriteFile(fmt.Sprintf("%s/q%d/bgpd.conf", outputDir, i), q.Config().Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
}
var buffer bytes.Buffer
encoder := toml.NewEncoder(&buffer)
encoder.Encode(gobgpConf)
err := ioutil.WriteFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), buffer.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
}
示例10: specAction
func specAction(c *cli.Context) {
ec := &config.ExtensionConfig{
Name: "nginx",
ConfigPath: "/etc/conf/nginx.conf",
PidPath: "/etc/conf/nginx.pid",
BackendOverrideAddress: "",
}
if err := config.SetConfigDefaults(ec); err != nil {
log.Fatal(err)
}
cfg := &config.Config{
ListenAddr: ":8080",
DockerURL: "unix:///var/run/docker.sock",
EnableMetrics: true,
Extensions: []*config.ExtensionConfig{
ec,
},
}
if err := toml.NewEncoder(os.Stdout).Encode(cfg); err != nil {
log.Fatal(err)
}
}
示例11: TestParse
func (s *TomlSuite) TestParse(c *C) {
type Nested struct {
Parent string `toml:"parent"`
}
type TestConfig struct {
FileName string `toml:"file_name"`
Count int `toml:"count"`
Child Nested `toml:"child"`
}
config := TestConfig{
FileName: "test.me",
Count: 10,
Child: Nested{
Parent: "parent",
},
}
file, err := ioutil.TempFile("", "test")
c.Assert(err, IsNil)
defer func() {
_ = file.Close()
}()
err = toml.NewEncoder(file).Encode(config)
c.Assert(err, IsNil)
// let's confirm that we can parse the data again
config2 := TestConfig{}
err = parseToml(file.Name(), &config2)
c.Assert(err, IsNil)
c.Assert(config2, DeepEquals, config)
}
示例12: StoreModelFile
func StoreModelFile(path string, mods []Model) error {
sort.Sort(Models(mods))
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(models{mods}); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
示例13: AppendDeployment
func AppendDeployment(d Deployment, initIfMissing bool) (DeploymentConfig, error) {
f, err := os.Open(path)
if err != nil && initIfMissing {
f, err = os.Create(path)
}
f.Close()
config, err := ReadDeploymentConfig()
if err != nil {
return DeploymentConfig{}, err
}
if config.Deployments == nil {
config.Deployments = make(map[string]Deployment)
}
config.Deployments[d.Id] = d
f, _ = os.Create(path)
encoder := toml.NewEncoder(f)
err = encoder.Encode(config)
if err != nil {
return DeploymentConfig{}, err
}
return config, nil
}
示例14: TestConfig
// TestConfig creates config with all files in root directory
func TestConfig(rootDir string) string {
cfg := NewConfig()
cfg.Common.Logfile = filepath.Join(rootDir, "go-carbon.log")
cfg.Whisper.DataDir = rootDir
cfg.Whisper.SchemasFilename = filepath.Join(rootDir, "schemas.conf")
// cfg.Whisper.Aggregation = filepath.Join(rootDir, "aggregation.conf")
configFile := filepath.Join(rootDir, "go-carbon.conf")
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
encoder.Indent = ""
if err := encoder.Encode(cfg); err != nil {
return configFile
}
ioutil.WriteFile(cfg.Whisper.SchemasFilename, []byte(`
[default]
priority = 1
pattern = .*
retentions = 60:43200,3600:43800`), 0644)
ioutil.WriteFile(configFile, buf.Bytes(), 0644)
return configFile
}
示例15: Write
func (dbConf DBConf) Write(w io.Writer) error {
encoder := toml.NewEncoder(w)
if err := encoder.Encode(dbConf); err != nil {
return err
}
return nil
}