• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang toml.NewEncoder函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/BurntSushi/toml.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewEncoder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:dc735,项目名称:delta,代码行数:26,代码来源:response.go


示例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)
}
开发者ID:antonsofyan,项目名称:utron,代码行数:28,代码来源:config.go


示例3: String

func (c *Config) String() string {
	var b bytes.Buffer
	e := toml.NewEncoder(&b)
	e.Indent = "    "
	e.Encode(c)
	return b.String()
}
开发者ID:CowLeo,项目名称:qdb,代码行数:7,代码来源:config.go


示例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
}
开发者ID:tknhs,项目名称:weather,代码行数:33,代码来源:config.go


示例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)
}
开发者ID:noblehng,项目名称:bosun,代码行数:25,代码来源:main.go


示例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
}
开发者ID:dc735,项目名称:delta,代码行数:26,代码来源:polynomial.go


示例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()
}
开发者ID:ably-forks,项目名称:flynn,代码行数:7,代码来源:config.go


示例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)
	}
}
开发者ID:val00238,项目名称:rds-try,代码行数:31,代码来源:query_test.go


示例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)
	}
}
开发者ID:osrg,项目名称:gobgp,代码行数:33,代码来源:quagga-rsconfig.go


示例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)
	}
}
开发者ID:tmrudick,项目名称:interlock,代码行数:25,代码来源:spec.go


示例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)
}
开发者ID:basharal,项目名称:config,代码行数:35,代码来源:toml_test.go


示例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
}
开发者ID:dc735,项目名称:delta,代码行数:26,代码来源:model.go


示例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
}
开发者ID:evandbrown,项目名称:dm,代码行数:25,代码来源:config.go


示例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
}
开发者ID:xyntrix,项目名称:go-carbon,代码行数:31,代码来源:config.go


示例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
}
开发者ID:ndaniels,项目名称:cablastp2,代码行数:7,代码来源:dbconf.go


示例16: saveCurrentState

// saveCurrentState saves the current running turtle apps.
func saveCurrentState() error {
	// Create a new state value.
	var s state

	// Get all apps.
	curApps := apps.Apps()

	for _, app := range curApps {
		// Skip if not running.
		if !app.IsRunning() {
			continue
		}

		// Add the app name to the running apps slice.
		s.RunningApps = append(s.RunningApps, app.Name())
	}

	// Encode the state value to TOML.
	buf := new(bytes.Buffer)
	err := toml.NewEncoder(buf).Encode(&s)
	if err != nil {
		return fmt.Errorf("failed to encode turtle state value to toml: %v", err)
	}

	// Write the result to the app settings file.
	err = ioutil.WriteFile(config.Config.StateFilePath(), buf.Bytes(), 0600)
	if err != nil {
		return fmt.Errorf("failed to save turtle state value: %v", err)
	}

	return nil
}
开发者ID:gitter-badger,项目名称:turtle,代码行数:33,代码来源:state.go


示例17: Run

// Run parses and prints the current config loaded.
func (cmd *PrintConfigCommand) Run(args ...string) error {
	// Parse command flags.
	fs := flag.NewFlagSet("", flag.ContinueOnError)
	configPath := fs.String("config", "", "")
	fs.Usage = func() { fmt.Fprintln(cmd.Stderr, printConfigUsage) }
	if err := fs.Parse(args); err != nil {
		return err
	}

	// Parse config from path.
	config, err := cmd.parseConfig(*configPath)
	if err != nil {
		return fmt.Errorf("parse config: %s", err)
	}

	// Apply any environment variables on top of the parsed config
	if err := config.ApplyEnvOverrides(); err != nil {
		return fmt.Errorf("apply env config: %v", err)
	}

	// Validate the configuration.
	if err := config.Validate(); err != nil {
		return fmt.Errorf("%s. To generate a valid configuration file run `influxd config > influxdb.generated.conf`", err)
	}

	toml.NewEncoder(cmd.Stdout).Encode(config)
	fmt.Fprint(cmd.Stdout, "\n")

	return nil
}
开发者ID:hawson,项目名称:influxdb,代码行数:31,代码来源:config_command.go


示例18: StorePAZFile

func StorePAZFile(path string, filters []PAZ) error {

	sort.Sort(PAZs(filters))

	buf := new(bytes.Buffer)
	if err := toml.NewEncoder(buf).Encode(pazs{filters}); 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
}
开发者ID:dc735,项目名称:delta,代码行数:26,代码来源:paz.go


示例19: Run

// Run parses and prints the current config loaded.
func (cmd *PrintConfigCommand) Run(args ...string) error {
	// Parse command flags.
	fs := flag.NewFlagSet("", flag.ContinueOnError)
	configPath := fs.String("config", "", "")
	hostname := fs.String("hostname", "", "")
	fs.Usage = func() { fmt.Fprintln(cmd.Stderr, printConfigUsage) }
	if err := fs.Parse(args); err != nil {
		return err
	}

	// Parse config from path.
	config, err := cmd.parseConfig(*configPath)
	if err != nil {
		return fmt.Errorf("parse config: %s", err)
	}

	// Override config properties.
	if *hostname != "" {
		config.Meta.Hostname = *hostname
	}

	toml.NewEncoder(cmd.Stdout).Encode(config)
	fmt.Fprint(cmd.Stdout, "\n")

	return nil
}
开发者ID:messagedb,项目名称:messagedb,代码行数:27,代码来源:config_command.go


示例20: main

func main() {
	b := config.Bgp{
		Global: config.Global{
			Config: config.GlobalConfig{
				As:       12332,
				RouterId: "10.0.0.1",
			},
		},
		Neighbors: []config.Neighbor{
			config.Neighbor{
				Config: config.NeighborConfig{
					PeerAs:          12333,
					AuthPassword:    "apple",
					NeighborAddress: "192.168.177.33",
				},
				AfiSafis: []config.AfiSafi{
					config.AfiSafi{AfiSafiName: "ipv4-unicast"},
					config.AfiSafi{AfiSafiName: "ipv6-unicast"},
				},
				ApplyPolicy: config.ApplyPolicy{

					Config: config.ApplyPolicyConfig{
						ImportPolicyList:    []string{"pd1"},
						DefaultImportPolicy: config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE,
					},
				},
			},

			config.Neighbor{
				Config: config.NeighborConfig{
					PeerAs:          12334,
					AuthPassword:    "orange",
					NeighborAddress: "192.168.177.32",
				},
			},

			config.Neighbor{
				Config: config.NeighborConfig{
					PeerAs:          12335,
					AuthPassword:    "grape",
					NeighborAddress: "192.168.177.34",
				},
			},
		},
	}

	var buffer bytes.Buffer
	encoder := toml.NewEncoder(&buffer)
	err := encoder.Encode(b)
	if err != nil {
		panic(err)
	}

	err = encoder.Encode(policy())
	if err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", buffer.String())
}
开发者ID:rguliyev,项目名称:gobgp,代码行数:59,代码来源:example_toml.go



注:本文中的github.com/BurntSushi/toml.NewEncoder函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang toml.Unmarshal函数代码示例发布时间:2022-05-24
下一篇:
Golang toml.DecodeReader函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap