// initConfig initializes the confd configuration by first setting defaults,
// then overriding settings from the confd config file, then overriding
// settings from environment variables, and finally overriding
// settings from flags set on the command line.
// It returns an error if any.
initConfig initializes the confd configuration by first setting defaults,
then overriding settings from the confd config file, then overriding
settings from environment variables, and finally overriding
settings from flags set on the command line.
It returns an error if any.
// initConfig initializes the confd configuration by first setting defaults // then overriding settings from the confd config file then overriding // settings from environment variables and finally overriding // settings from flags set on the command line . // It returns an error if any .
func initConfig() error {
	if configFile == "" {
		if _, err := os.Stat(defaultConfigFile); !os.IsNotExist(err) {
			configFile = defaultConfigFile
		}
	}
	config = Config{
		Backend:  "etcd",
		ConfDir:  "/etc/confd",
		Interval: 600,
		Prefix:   "",
		Scheme:   "http",
	}
	if configFile == "" {
		log.Debug("Skipping confd config file.")
	} else {
		log.Debug("Loading " + configFile)
		configBytes, err := ioutil.ReadFile(configFile)
		if err != nil {
			return err
		}
		_, err = toml.Decode(string(configBytes), &config)
		if err != nil {
			return err
		}
	}
	processEnv()
	processFlags()
	var pgpPrivateKey []byte
	if config.SecretKeyring != "" {
		kr, err := os.Open(config.SecretKeyring)
		if err != nil {
			log.Fatal(err.Error())
		}
		defer kr.Close()
		pgpPrivateKey, err = ioutil.ReadAll(kr)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	if config.LogLevel != "" {
		log.SetLevel(config.LogLevel)
	}
	if config.SRVDomain != "" && config.SRVRecord == "" {
		config.SRVRecord = fmt.Sprintf("_%s._tcp.%s.", config.Backend, config.SRVDomain)
	}
	if config.Backend != "env" && config.SRVRecord != "" {
		log.Info("SRV record set to " + config.SRVRecord)
		srvNodes, err := getBackendNodesFromSRV(config.SRVRecord)
		if err != nil {
			return errors.New("Cannot get nodes from SRV records " + err.Error())
		}
		switch config.Backend {
		case "etcd":
			vsm := make([]string, len(srvNodes))
			for i, v := range srvNodes {
				vsm[i] = config.Scheme + "://" + v
			}
			srvNodes = vsm
		}
		config.BackendNodes = srvNodes
	}
	if len(config.BackendNodes) == 0 {
		switch config.Backend {
		case "consul":
			config.BackendNodes = []string{"127.0.0.1:8500"}
		case "etcd":
			peerstr := os.Getenv("ETCDCTL_PEERS")
			if len(peerstr) > 0 {
				config.BackendNodes = strings.Split(peerstr, ",")
			} else {
				config.BackendNodes = []string{"http://127.0.0.1:4001"}
			}
		case "etcdv3":
			config.BackendNodes = []string{"127.0.0.1:2379"}
		case "redis":
			config.BackendNodes = []string{"127.0.0.1:6379"}
		case "vault":
			config.BackendNodes = []string{"http://127.0.0.1:8200"}
		case "zookeeper":
			config.BackendNodes = []string{"127.0.0.1:2181"}
		}
	}
	log.Info("Backend set to " + config.Backend)
	if config.Watch {
		unsupportedBackends := map[string]bool{
			"dynamodb": true,
			"ssm":      true,
		}
		if unsupportedBackends[config.Backend] {
			log.Info(fmt.Sprintf("Watch is not supported for backend %s. Exiting...", config.Backend))
			os.Exit(1)
		}
	}
	if config.Backend == "dynamodb" && config.Table == "" {
		return errors.New("No DynamoDB table configured")
	}
	backendsConfig = backends.Config{
		AuthToken:    config.AuthToken,
		AuthType:     config.AuthType,
		Backend:      config.Backend,
		BasicAuth:    config.BasicAuth,
		ClientCaKeys: config.ClientCaKeys,
		ClientCert:   config.ClientCert,
		ClientKey:    config.ClientKey,
		BackendNodes: config.BackendNodes,
		Password:     config.Password,
		Scheme:       config.Scheme,
		Table:        config.Table,
		Separator:    config.Separator,
		Username:     config.Username,
		AppID:        config.AppID,
		UserID:       config.UserID,
		RoleID:       config.RoleID,
		SecretID:     config.SecretID,
		YAMLFile:     config.YAMLFile,
	}
	templateConfig = template.Config{
		ConfDir:       config.ConfDir,
		ConfigDir:     filepath.Join(config.ConfDir, "conf.d"),
		KeepStageFile: keepStageFile,
		Noop:          config.Noop,
		Prefix:        config.Prefix,
		SyncOnly:      config.SyncOnly,
		TemplateDir:   filepath.Join(config.ConfDir, "templates"),
		PGPPrivateKey: pgpPrivateKey,
	}
	return nil
}
func initConfig ( ) error { if configFile == " " { if _ , err := os . Stat ( defaultConfigFile ) ; ! os . IsNotExist ( err ) { configFile = defaultConfigFile } } config = Config { Backend : " " , ConfDir : " " , Interval : 600 , Prefix : " " , Scheme : " " , } if configFile == " " { log . Debug ( " " ) } else { log . Debug ( " " + configFile ) configBytes , err := ioutil . ReadFile ( configFile ) if err != nil { return err } _ , err = toml . Decode ( string ( configBytes ) , & config ) if err != nil { return err } } processEnv ( ) processFlags ( ) var pgpPrivateKey [ ] byte if config . SecretKeyring != " " { kr , err := os . Open ( config . SecretKeyring ) if err != nil { log . Fatal ( err . Error ( ) ) } defer kr . Close ( ) pgpPrivateKey , err = ioutil . ReadAll ( kr ) if err != nil { log . Fatal ( err . Error ( ) ) } } if config . LogLevel != " " { log . SetLevel ( config . LogLevel ) } if config . SRVDomain != " " && config . SRVRecord == " " { config . SRVRecord = fmt . Sprintf ( " " , config . Backend , config . SRVDomain ) } if config . Backend != " " && config . SRVRecord != " " { log . Info ( " " + config . SRVRecord ) srvNodes , err := getBackendNodesFromSRV ( config . SRVRecord ) if err != nil { return errors . New ( " " + err . Error ( ) ) } switch config . Backend { case " " : vsm := make ( [ ] string , len ( srvNodes ) ) for i , v := range srvNodes { vsm [ i ] = config . Scheme + " " + v } srvNodes = vsm } config . BackendNodes = srvNodes } if len ( config . BackendNodes ) == 0 { switch config . Backend { case " " : config . BackendNodes = [ ] string { " " } case " " : peerstr := os . Getenv ( " " ) if len ( peerstr ) > 0 { config . BackendNodes = strings . Split ( peerstr , " " ) } else { config . BackendNodes = [ ] string { " " } } case " " : config . BackendNodes = [ ] string { " " } case " " : config . BackendNodes = [ ] string { " " } case " " : config . BackendNodes = [ ] string { " " } case " " : config . BackendNodes = [ ] string { " " } } } log . Info ( " " + config . Backend ) if config . Watch { unsupportedBackends := map [ string ] bool { " " : true , " " : true , } if unsupportedBackends [ config . Backend ] { log . Info ( fmt . Sprintf ( " " , config . Backend ) ) os . Exit ( 1 ) } } if config . Backend == " " && config . Table == " " { return errors . New ( " " ) } backendsConfig = backends . Config { AuthToken : config . AuthToken , AuthType : config . AuthType , Backend : config . Backend , BasicAuth : config . BasicAuth , ClientCaKeys : config . ClientCaKeys , ClientCert : config . ClientCert , ClientKey : config . ClientKey , BackendNodes : config . BackendNodes , Password : config . Password , Scheme : config . Scheme , Table : config . Table , Separator : config . Separator , Username : config . Username , AppID : config . AppID , UserID : config . UserID , RoleID : config . RoleID , SecretID : config . SecretID , YAMLFile : config . YAMLFile , } templateConfig = template . Config { ConfDir : config . ConfDir , ConfigDir : filepath . Join ( config . ConfDir , " " ) , KeepStageFile : keepStageFile , Noop : config . Noop , Prefix : config . Prefix , SyncOnly : config . SyncOnly , TemplateDir : filepath . Join ( config . ConfDir , " " ) , PGPPrivateKey : pgpPrivateKey , } return nil }
initConfig initializes the confd configuration by first setting defaults,
then overriding settings from the confd config file, then overriding
settings from environment variables, and finally overriding
settings from flags set on the command line.
initConfig initializes the confd configuration by first setting defaults then overriding settings from the confd config file then overriding settings from environment variables and finally overriding settings from flags set on the command line .
// Set defaults. // Update config from the TOML configuration file. // Update config from environment variables. // Update config from commandline flags. // Update BackendNodes from SRV records. // Initialize the storage client // Template configuration.