Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
helper function to parse boolean env variables
Browse files Browse the repository at this point in the history
Adds a new helper function to parse boolean environment variables with a
fallback to the provided default value. Mainly for reduced code
duplication and readability.
  • Loading branch information
lime008 authored and nate-droid committed Jun 16, 2021
1 parent 3cfa917 commit 5519bfd
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,32 @@ const (
DefaultNATSAddr = "localhost:4223"
)

func defaultEnv(key, defaultValue string) string {
// defaultEnvStr reads a string value from the environment and falls back to the
// default value if not provided.
func defaultEnvStr(key, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}

// defaultEnvBool reads a boolean value from the environment and falls back to the
// default value if not provided.
func defaultEnvBool(key string, defaultValue bool) bool {
if value, ok := os.LookupEnv(key); ok {
val, _ := strconv.ParseBool(defaultEnvStr(key, value))
return val
}
return defaultValue
}

// DefaultServerConfig creates a ServerConfig struct from defaults
// or, preferentially, from provided environment variables.
func DefaultServerConfig() *ServerConfig {
return &ServerConfig{
Protocol: defaultEnv("BUBBLY_PROTOCOL", DefaultAPIServerProtocol),
Host: defaultEnv("BUBBLY_HOST", DefaultAPIServerHost),
Port: defaultEnv("BUBBLY_PORT", DefaultAPIServerPort),
Protocol: defaultEnvStr("BUBBLY_PROTOCOL", DefaultAPIServerProtocol),
Host: defaultEnvStr("BUBBLY_HOST", DefaultAPIServerHost),
Port: defaultEnvStr("BUBBLY_PORT", DefaultAPIServerPort),
}
}

Expand All @@ -85,17 +97,17 @@ func DefaultServerConfig() *ServerConfig {
func DefaultStoreConfig() *StoreConfig {
return &StoreConfig{
// Default provider
Provider: StoreProviderType(defaultEnv("BUBBLY_STORE_PROVIDER", DefaultStoreProvider)),
Provider: StoreProviderType(defaultEnvStr("BUBBLY_STORE_PROVIDER", DefaultStoreProvider)),
// Default configuration for Postgres
PostgresAddr: defaultEnv("POSTGRES_ADDR", DefaultPostgresAddr),
PostgresUser: defaultEnv("POSTGRES_USER", DefaultPostgresUser),
PostgresPassword: defaultEnv("POSTGRES_PASSWORD", DefaultPostgresPassword),
PostgresDatabase: defaultEnv("POSTGRES_DATABASE", DefaultPostgresDatabase),
PostgresAddr: defaultEnvStr("POSTGRES_ADDR", DefaultPostgresAddr),
PostgresUser: defaultEnvStr("POSTGRES_USER", DefaultPostgresUser),
PostgresPassword: defaultEnvStr("POSTGRES_PASSWORD", DefaultPostgresPassword),
PostgresDatabase: defaultEnvStr("POSTGRES_DATABASE", DefaultPostgresDatabase),
// Default configuration for CockroachDB
CockroachAddr: defaultEnv("COCKROACH_ADDR", defaultCockroachAddr),
CockroachUser: defaultEnv("COCKROACH_USER", defaultCockroachUser),
CockroachPassword: defaultEnv("COCKROACH_PASSWORD", defaultCockroachPassword),
CockroachDatabase: defaultEnv("COCKROACH_DATABASE", defaultCockroachDatabase),
CockroachAddr: defaultEnvStr("COCKROACH_ADDR", defaultCockroachAddr),
CockroachUser: defaultEnvStr("COCKROACH_USER", defaultCockroachUser),
CockroachPassword: defaultEnvStr("COCKROACH_PASSWORD", defaultCockroachPassword),
CockroachDatabase: defaultEnvStr("COCKROACH_DATABASE", defaultCockroachDatabase),

// Default retry configs, so retry every 1 second up to 5 times
RetrySleep: DefaultRetrySleep,
Expand All @@ -113,22 +125,18 @@ func DefaultAgentConfig() *AgentConfig {
return &AgentConfig{
NATSServerConfig: DefaultNATSServerConfig(),
EnabledComponents: DefaultAgentComponentsEnabled(),
DeploymentType: AgentDeploymentType(defaultEnv("AGENT_DEPLOYMENT_TYPE", DefaultDeploymentType.String())),
DeploymentType: AgentDeploymentType(defaultEnvStr("AGENT_DEPLOYMENT_TYPE", DefaultDeploymentType.String())),
}
}

// DefaultAgentComponentsEnabled creates an AgentComponentsToggle struct
// instance with all components disabled
func DefaultAgentComponentsEnabled() *AgentComponentsToggle {
apiServerToggle, _ := strconv.ParseBool(defaultEnv("AGENT_API_SERVER_TOGGLE", strconv.FormatBool(DefaultAPIServerToggle)))
dataStoreToggle, _ := strconv.ParseBool(defaultEnv("AGENT_DATA_STORE_TOGGLE", strconv.FormatBool(DefaultDataStoreToggle)))
workerToggle, _ := strconv.ParseBool(defaultEnv("AGENT_WORKER_TOGGLE", strconv.FormatBool(DefaultWorkerToggle)))
natsServerToggle, _ := strconv.ParseBool(defaultEnv("AGENT_NATS_SERVER_TOGGLE", strconv.FormatBool(DefaultNATSServerToggle)))
return &AgentComponentsToggle{
APIServer: apiServerToggle,
DataStore: dataStoreToggle,
Worker: workerToggle,
NATSServer: natsServerToggle,
APIServer: defaultEnvBool("AGENT_API_SERVER_TOGGLE", DefaultAPIServerToggle),
DataStore: defaultEnvBool("AGENT_DATA_STORE_TOGGLE", DefaultDataStoreToggle),
Worker: defaultEnvBool("AGENT_WORKER_TOGGLE", DefaultWorkerToggle),
NATSServer: defaultEnvBool("AGENT_NATS_SERVER_TOGGLE", DefaultNATSServerToggle),
}
}

Expand All @@ -137,12 +145,10 @@ func DefaultAgentComponentsEnabled() *AgentComponentsToggle {
// ###########################################

func DefaultAuthConfig() *AuthConfig {
authentication, _ := strconv.ParseBool(defaultEnv("BUBBLY_AUTHENTICATION", "false"))
multiTenancy, _ := strconv.ParseBool(defaultEnv("BUBBLY_MULTITENANCY", "false"))
return &AuthConfig{
Authentication: authentication,
MultiTenancy: multiTenancy,
AuthAddr: defaultEnv("BUBBLY_AUTH_API", "http://bubbly-auth:1323/api/v1"),
Authentication: defaultEnvBool("BUBBLY_AUTHENTICATION", false),
MultiTenancy: defaultEnvBool("BUBBLY_MULTITENANCY", false),
AuthAddr: defaultEnvStr("BUBBLY_AUTH_API", "http://bubbly-auth:1323/api/v1"),
}
}

Expand All @@ -154,10 +160,10 @@ func DefaultAuthConfig() *AuthConfig {
// or, preferentially, from provided environment variables.
func DefaultNATSServerConfig() *NATSServerConfig {
httpPort, _ := strconv.Atoi(
defaultEnv("NATS_SERVER_HTTP_PORT", DefaultNATSServerHTTPPort),
defaultEnvStr("NATS_SERVER_HTTP_PORT", DefaultNATSServerHTTPPort),
)
port, _ := strconv.Atoi(
defaultEnv("NATS_SERVER_PORT", DefaultNATSServerPort),
defaultEnvStr("NATS_SERVER_PORT", DefaultNATSServerPort),
)
return &NATSServerConfig{
HTTPPort: httpPort,
Expand All @@ -172,9 +178,9 @@ func DefaultNATSServerConfig() *NATSServerConfig {
func DefaultClientConfig() *ClientConfig {
return &ClientConfig{
ClientType: HTTPClientType,
AuthToken: defaultEnv("BUBBLY_TOKEN", DefaultClientAuthToken),
BubblyAddr: defaultEnv("BUBBLY_ADDR", DefaultBubblyAddr),
NATSAddr: defaultEnv("BUBBLY_NATS_ADDR", DefaultNATSAddr),
AuthToken: defaultEnvStr("BUBBLY_TOKEN", DefaultClientAuthToken),
BubblyAddr: defaultEnvStr("BUBBLY_ADDR", DefaultBubblyAddr),
NATSAddr: defaultEnvStr("BUBBLY_NATS_ADDR", DefaultNATSAddr),
}
}

Expand All @@ -183,8 +189,7 @@ func DefaultClientConfig() *ClientConfig {
// ###########################################

func DefaultCLIConfig() *CLIConfig {
color, _ := strconv.ParseBool(defaultEnv("COLOR", strconv.FormatBool(DefaultCLIColorToggle)))
return &CLIConfig{
Color: color,
Color: defaultEnvBool("COLOR", DefaultCLIColorToggle),
}
}

0 comments on commit 5519bfd

Please sign in to comment.