func loadConfig() (*Config, error) {
	var firstErr error
	saveError := func(err error) {
		if err != nil && firstErr == nil {
			firstErr = err
		}
	}

	strVar := func(n, def string) string {
		v := strings.TrimSpace(os.Getenv(n))
		if v != "" {
			return v
		}
		return def
	}
	intVar := func(n, def string) int {
		v, err := strconv.Atoi(strVar(n, def))
		saveError(err)
		return v
	}
	boolVar := func(n, def string) bool {
		v := strVar(n, def)
		if v == "" {
			v = def
		}
		switch v {
		case "1", "true":
			return true
		case "0", "false":
			return false
		default:
			saveError(fmt.Errorf("bad bool %q", v))
			return false
		}
	}
	listVar := func(n, def string) map[string]struct{} {
		ev := strVar(n, def)
		if len(ev) == 0 {
			return nil
		}
		v := make(map[string]struct{})
		for _, s := range listRegexp.Split(ev, -1) {
			v[s] = struct{}{}
		}
		return v
	}

	// Parse simple fields.
	cfg := Config{
		emailHostname:          strVar("EMAIL_HOSTNAME", ""),
		emailPort:              intVar("EMAIL_PORT", "25"),
		emailUsername:          strVar("EMAIL_USERNAME", ""),
		emailPassword:          strVar("EMAIL_PASSWORD", ""),
		emailBuildTriggerIDs:   listVar("EMAIL_BUILD_TRIGGER_IDS", ""),
		emailBuildTriggerNames: listVar("EMAIL_BUILD_TRIGGER_NAMES", ""),
		emailBuildStatuses:     listVar("EMAIL_BUILD_STATUSES", "FAILURE,INTERNAL_ERROR,TIMEOUT"),
		badgeBucket:            strVar("BADGE_BUCKET", ""),
		badgeReports:           boolVar("BADGE_REPORTS", "false"),
	}
	if firstErr != nil {
		return nil, firstErr
	}

	// Parse email addresses.
	var err error
	if v := strVar("EMAIL_FROM", ""); v != "" {
		if cfg.emailFrom, err = mail.ParseAddress(v); err != nil {
			return nil, fmt.Errorf("bad EMAIL_FROM: %v", err)
		}
	}
	if v := strVar("EMAIL_RECIPIENTS", ""); v != "" {
		if cfg.emailRecipients, err = mail.ParseAddressList(v); err != nil {
			return nil, fmt.Errorf("bad EMAIL_RECIPIENTS: %v", err)
		}
	}

	// Load and validate time zone.
	if cfg.emailTimeZone, err = time.LoadLocation(strVar("EMAIL_TIME_ZONE", "Etc/UTC")); err != nil {
		return nil, fmt.Errorf("bad EMAIL_TIME_ZONE: %v", err)
	}

	// Validate build statuses.
	for s := range cfg.emailBuildStatuses {
		if _, ok := cbpb.Build_Status_value[s]; !ok {
			return nil, fmt.Errorf("bad status %q in EMAIL_BUILD_STATUSES", s)
		}
	}

	if cfg.badgeReports && cfg.badgeBucket == "" {
		return nil, errors.New("BADGE_REPORTS requires BADGE_BUCKET")
	}

	return &cfg, nil
}
// loadConfig constructs a new Config object from environment variables.
// An error is returned if any variables are unparseable.
loadConfig constructs a new Config object from environment variables.
An error is returned if any variables are unparseable.
// loadConfig constructs a new Config object from environment variables . // An error is returned if any variables are unparseable .
func loadConfig() (*Config, error) {
	var firstErr error
	saveError := func(err error) {
		if err != nil && firstErr == nil {
			firstErr = err
		}
	}
	strVar := func(n, def string) string {
		v := strings.TrimSpace(os.Getenv(n))
		if v != "" {
			return v
		}
		return def
	}
	intVar := func(n, def string) int {
		v, err := strconv.Atoi(strVar(n, def))
		saveError(err)
		return v
	}
	boolVar := func(n, def string) bool {
		v := strVar(n, def)
		if v == "" {
			v = def
		}
		switch v {
		case "1", "true":
			return true
		case "0", "false":
			return false
		default:
			saveError(fmt.Errorf("bad bool %q", v))
			return false
		}
	}
	listVar := func(n, def string) map[string]struct{} {
		ev := strVar(n, def)
		if len(ev) == 0 {
			return nil
		}
		v := make(map[string]struct{})
		for _, s := range listRegexp.Split(ev, -1) {
			v[s] = struct{}{}
		}
		return v
	}
	cfg := Config{
		emailHostname:          strVar("EMAIL_HOSTNAME", ""),
		emailPort:              intVar("EMAIL_PORT", "25"),
		emailUsername:          strVar("EMAIL_USERNAME", ""),
		emailPassword:          strVar("EMAIL_PASSWORD", ""),
		emailBuildTriggerIDs:   listVar("EMAIL_BUILD_TRIGGER_IDS", ""),
		emailBuildTriggerNames: listVar("EMAIL_BUILD_TRIGGER_NAMES", ""),
		emailBuildStatuses:     listVar("EMAIL_BUILD_STATUSES", "FAILURE,INTERNAL_ERROR,TIMEOUT"),
		badgeBucket:            strVar("BADGE_BUCKET", ""),
		badgeReports:           boolVar("BADGE_REPORTS", "false"),
	}
	if firstErr != nil {
		return nil, firstErr
	}
	var err error
	if v := strVar("EMAIL_FROM", ""); v != "" {
		if cfg.emailFrom, err = mail.ParseAddress(v); err != nil {
			return nil, fmt.Errorf("bad EMAIL_FROM: %v", err)
		}
	}
	if v := strVar("EMAIL_RECIPIENTS", ""); v != "" {
		if cfg.emailRecipients, err = mail.ParseAddressList(v); err != nil {
			return nil, fmt.Errorf("bad EMAIL_RECIPIENTS: %v", err)
		}
	}
	if cfg.emailTimeZone, err = time.LoadLocation(strVar("EMAIL_TIME_ZONE", "Etc/UTC")); err != nil {
		return nil, fmt.Errorf("bad EMAIL_TIME_ZONE: %v", err)
	}
	for s := range cfg.emailBuildStatuses {
		if _, ok := cbpb.Build_Status_value[s]; !ok {
			return nil, fmt.Errorf("bad status %q in EMAIL_BUILD_STATUSES", s)
		}
	}
	if cfg.badgeReports && cfg.badgeBucket == "" {
		return nil, errors.New("BADGE_REPORTS requires BADGE_BUCKET")
	}
	return &cfg, nil
}
func loadConfig ( ) ( * Config , error ) { var firstErr error saveError := func ( err error ) { if err != nil && firstErr == nil { firstErr = err } } strVar := func ( n , def string ) string { v := strings . TrimSpace ( os . Getenv ( n ) ) if v != " " { return v } return def } intVar := func ( n , def string ) int { v , err := strconv . Atoi ( strVar ( n , def ) ) saveError ( err ) return v } boolVar := func ( n , def string ) bool { v := strVar ( n , def ) if v == " " { v = def } switch v { case " " , " " : return true case " " , " " : return false default : saveError ( fmt . Errorf ( " " , v ) ) return false } } listVar := func ( n , def string ) map [ string ] struct { } { ev := strVar ( n , def ) if len ( ev ) == 0 { return nil } v := make ( map [ string ] struct { } ) for _ , s := range listRegexp . Split ( ev , - 1 ) { v [ s ] = struct { } { } } return v } cfg := Config { emailHostname : strVar ( " " , " " ) , emailPort : intVar ( " " , " " ) , emailUsername : strVar ( " " , " " ) , emailPassword : strVar ( " " , " " ) , emailBuildTriggerIDs : listVar ( " " , " " ) , emailBuildTriggerNames : listVar ( " " , " " ) , emailBuildStatuses : listVar ( " " , " " ) , badgeBucket : strVar ( " " , " " ) , badgeReports : boolVar ( " " , " " ) , } if firstErr != nil { return nil , firstErr } var err error if v := strVar ( " " , " " ) ; v != " " { if cfg . emailFrom , err = mail . ParseAddress ( v ) ; err != nil { return nil , fmt . Errorf ( " " , err ) } } if v := strVar ( " " , " " ) ; v != " " { if cfg . emailRecipients , err = mail . ParseAddressList ( v ) ; err != nil { return nil , fmt . Errorf ( " " , err ) } } if cfg . emailTimeZone , err = time . LoadLocation ( strVar ( " " , " " ) ) ; err != nil { return nil , fmt . Errorf ( " " , err ) } for s := range cfg . emailBuildStatuses { if _ , ok := cbpb . Build_Status_value [ s ] ; ! ok { return nil , fmt . Errorf ( " " , s ) } } if cfg . badgeReports && cfg . badgeBucket == " " { return nil , errors . New ( " " ) } return & cfg , nil }
loadConfig constructs a new Config object from environment variables.
loadConfig constructs a new Config object from environment variables .
// Parse simple fields. // Parse email addresses. // Load and validate time zone. // Validate build statuses.