Exemple #1
0
// newConfig checks for a ini file in XDG_CONFIG_HOME/gohead/config.ini, then
// looks for $HOME/.config/gohead/config.ini. If neither one can be found,
// a default (blank) configuration is used.
func newConfig() *config {
	var dict ini.Dict
	var err error

	conf := &config{
		outputs: make(map[string]string, 10),
	}

	if len(flagConfig) > 0 {
		dict, err = ini.Load(flagConfig)
		if err != nil {
			log.Printf("There was an error when trying to load '%s': %s.",
				flagConfig, err)
			return conf
		}
	} else {
		dict, err = ini.Load(xdgfile)
		if err != nil {
			if os.IsNotExist(err) && xdgfile != myfile {
				dict, err = ini.Load(myfile)
				if err != nil {
					log.Printf("Neither '%s' nor '%s' could be read: %s.",
						xdgfile, myfile, err)
					return conf
				}
			} else {
				log.Printf("There was an error when trying to load '%s': %s.",
					xdgfile, err)
				return conf
			}
		}
	}

	for _, section := range dict.GetSections() {
		switch section {
		case "monitors":
			for niceName := range dict[section] {
				if output, ok := dict.GetString(section, niceName); ok {
					conf.outputs[output] = niceName
				}
			}
		case "":
		default:
			log.Printf("I don't know what to do with section '%s'.", section)
		}
	}
	return conf
}
Exemple #2
0
// NewWithOptions creates a GlobalConf from the provided
// Options. The caller is responsible for creating any
// referenced config files.
func NewWithOptions(opts *Options) (g *GlobalConf, err error) {
	Register("", flag.CommandLine)

	var dict ini.Dict
	if opts.Filename != "" {
		dict, err = ini.Load(opts.Filename)
		if err != nil {
			return nil, err
		}
	} else {
		dict = make(ini.Dict, 0)
	}

	return &GlobalConf{
		Filename:  opts.Filename,
		EnvPrefix: opts.EnvPrefix,
		dict:      &dict,
	}, nil
}