當前位置: 首頁>>代碼示例>>Golang>>正文


Golang toml.NewEncoder函數代碼示例

本文整理匯總了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
}
開發者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


注:本文中的github.com/BurntSushi/toml.NewEncoder函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。