Esempio n. 1
0
func LoadFile(name string) error {
	f, err := os.Open(name)
	if os.IsNotExist(err) {
		f, err = os.Create(name)
		if err != nil {
			return err
		}
		err = toml.NewEncoder(f).Encode(Current)
		if err != nil {
			return err
		}
		err = f.Close()
		if err != nil {
			return err
		}
		return nil
	}
	if err != nil {
		return err
	}
	err = toml.NewDecoder(f).Decode(&Current)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 2
0
func Read(r io.Reader) (*Config, error) {
	config := new(Config)
	if err := toml.NewDecoder(r).Decode(&config); err != nil {
		return nil, err
	}
	return config, nil
}
Esempio n. 3
0
// ReadConfigFile will open the file with the supplied name
// and return the configuration. The configuration is validated.
func ReadConfigFile(file string) (*Config, error) {
	tmpl := template.New(filepath.Base(file)).Funcs(template.FuncMap{
		"env": os.Getenv,
	})
	t, err := tmpl.ParseFiles(file)
	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer
	err = t.Execute(&buf, nil)
	if err != nil {
		return nil, err
	}

	config := Config{}
	err = toml.NewDecoder(&buf).Decode(&config)
	if err != nil {
		return nil, err
	}

	err = config.Validate()
	if err != nil {
		return nil, err
	}
	return &config, nil
}
Esempio n. 4
0
func loadConfig(file string) (*chefLoadConfig, error) {
	f, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	// Initialize default configuration values
	config := chefLoadConfig{
		Nodes:          10,
		NodeNamePrefix: "chef-load",

		Interval: 1800,
		Splay:    300,

		Runs: 0,

		RunList:           make([]string, 0),
		DownloadCookbooks: "never",
		SleepDuration:     0,
	}

	if err = toml.NewDecoder(f).Decode(&config); err != nil {
		return nil, err
	}

	return &config, nil
}
Esempio n. 5
0
func Parse(configData io.Reader) (*Config, error) {
	conf := DefaultConfig()
	decoder := toml.NewDecoder(configData)

	if err := decoder.Decode(conf); err != nil {
		return nil, err
	}

	return conf, nil
}
Esempio n. 6
0
// Decode unmarshals a string of TOML into a target configuration struct.
func Decode(tomlBlob string, target interface{}) error {
	sreader := strings.NewReader(tomlBlob)
	decoder := toml.NewDecoder(sreader)
	err := decoder.Decode(target)

	if err != nil {
		return err
	}

	return nil
}
Esempio n. 7
0
// DecodeFile loads a TOML configuration from a provided path and unmarshals it
// into a target configuration struct.
func DecodeFile(path string, target interface{}) error {
	file, err := os.Open(path)

	if err != nil {
		return err
	}
	decoder := toml.NewDecoder(file)
	err = decoder.Decode(target)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 8
0
func ParseConfig(path string) *Config {
	file, err := os.Open(path)
	if err != nil {
		log.Fatalln("Failed to open config file:", err)
	}
	var conf Config
	decoder := toml.NewDecoder(file)
	err = decoder.Decode(&conf)
	if err != nil {
		log.Fatalln("Failed to parse config file:", err)
	}
	return &conf
}
Esempio n. 9
0
func loadConfigOrDie() {
	var configName string
	if len(os.Args) < 2 {
		configName = defaultConfigLocation
	} else {
		configName = os.Args[1]
		os.Args = append(os.Args[0:1], os.Args[2:]...)
	}

	fh, err := os.Open(configName)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}
	defer fh.Close()

	err = toml.NewDecoder(fh).Decode(&config)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}

	if config.GCPercent <= 0 {
		config.GCPercent = 20
	}

	if config.Proxy.Listen == "" {
		config.Proxy.Listen = "127.0.0.1:17942"
	}
	if config.Proxy.ParallelRequests <= 0 {
		config.Proxy.ParallelRequests = 25
	}
	if config.Proxy.Scrubbers == 0 {
		config.Proxy.Scrubbers = 1
	}

	if config.Chunk.Listen == "" {
		config.Chunk.Listen = "127.0.0.1:17941"
	}
	if config.Chunk.ParallelRequests <= 0 {
		config.Chunk.ParallelRequests = 25
	}
	if config.Chunk.Scrubber.SleepPerFile.Duration <= 0 {
		config.Chunk.Scrubber.SleepPerFile.Duration = 50 * time.Millisecond
	}
	if config.Chunk.Scrubber.SleepPerByte.Duration <= 0 {
		config.Chunk.Scrubber.SleepPerByte.Duration = 1500 * time.Nanosecond
	}
}
Esempio n. 10
0
func ConfigToArgs(r io.Reader) ([]string, error) {
	c := &config{}
	err := toml.NewDecoder(r).Decode(c)
	if err != nil {
		return nil, err
	}

	res := make([]string, 0, len(c.Rule)*4)
	for _, r := range c.Rule {
		res = append(res, "-g")
		res = append(res, r.Glob)
		res = append(res, "-c")
		res = append(res, r.Command)
	}
	return res, nil
}
Esempio n. 11
0
func ReadLocalCfg() localConfig {
	var config = defaultLocal

	user, err := user.Current()
	if err == nil {
		cfgFile := filepath.Join(user.HomeDir, localConfigFile)

		f, err := os.Open(cfgFile)
		defer f.Close()

		if err == nil {
			dec := toml.NewDecoder(f)
			dec.Decode(&config)
		}
	}

	return config
}
Esempio n. 12
0
func loadConfig(filename string) (*config, error) {
	conf := config{}

	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dec := toml.NewDecoder(f)
	err = dec.Decode(&conf)
	if err != nil {
		return nil, err
	}

	if !conf.Shutdown && conf.Commands == nil {
		return nil, errors.New(`"Shutdown" is false or unconfigured and no commands have been specified. Add "Shutdown = <shutdownmode>" to the config file`)
	}
	return &conf, nil
}
Esempio n. 13
0
func ParseConfig() *Config {
	var out Config

	file, err := os.Open(configFile)
	if err != nil {
		Logger.Fatalln(err)
		return nil
	}

	defer file.Close()

	decoder := toml.NewDecoder(file)

	if err := decoder.Decode(&out); err != nil {
		Logger.Fatalln(err)
		return nil
	}

	GlobalConfig = &out

	return &out
}
Esempio n. 14
0
func ReadConfig(r io.Reader, d interface{}) error {
	dec := toml.NewDecoder(r)
	return dec.Decode(d)
}