Documentation ¶
Overview ¶
Package isprod is a simple package to check if the application is running in production or not.
It has default conditions that fit the most common cases, but you can also add your own conditions.
Default rules are:
If environment variable 'prod' is set and its value is not one of [false], consider it as production environment. If environment variable 'production' is set and its value is not one of [false], consider it as production environment. If environment variable 'staging' is set and its value is not one of [false], consider it as production environment. If environment variable 'live' is set and its value is not one of [false], consider it as production environment. If environment variable 'ci' is set and its value is not one of [false], consider it as production environment. If environment variable 'PROD' is set and its value is not one of [false], consider it as production environment. If environment variable 'PRODUCTION' is set and its value is not one of [false], consider it as production environment. If environment variable 'STAGING' is set and its value is not one of [false], consider it as production environment. If environment variable 'LIVE' is set and its value is not one of [false], consider it as production environment. If environment variable 'CI' is set and its value is not one of [false], consider it as production environment. If environment variable 'env' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment. If environment variable 'environment' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment. If environment variable 'mode' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment. If environment variable 'ENV' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment. If environment variable 'ENVIRONMENT' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment. If environment variable 'MODE' is set and its value is one of [prod, production, staging, live, ci, PROD, PRODUCTION, STAGING, LIVE, CI], consider it as production environment.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
func Check() bool
Check checks if the application is running in production or not. It uses the DefaultConditions. If you want to use your own conditions, use the Conditions.Check() method.
Example ¶
package main import ( "atomicgo.dev/isprod" "fmt" "os" ) func main() { os.Setenv("PRODUCTION", "true") // Many common names are supported. See DefaultConditions. fmt.Println(isprod.Check()) }
Output: true
Types ¶
type Condition ¶
type Condition struct { // EnvVarName is the name of the environment variable to check. EnvVarName string // AllowedValues is a list of values that are considered valid for the environment variable. AllowedValues []string // AllowAnyValue can be set to true if any value for the environment variable is allowed. AllowAnyValue bool // ExcludedValues is a list of values that are specifically not allowed, even if AllowAnyValue is set to true. ExcludedValues []string }
Condition is a condition that checks if the environment is production.
func (Condition) Check ¶
Check checks if the condition is met.
Example ¶
package main import ( "atomicgo.dev/isprod" "fmt" "os" ) func main() { os.Setenv("MY_ENV_VAR", "live") cond := isprod.Condition{ EnvVarName: "MY_ENV_VAR", AllowAnyValue: true, ExcludedValues: []string{"false"}, } fmt.Println(cond.Check()) }
Output: true
type Conditions ¶
type Conditions []Condition
Conditions is a list of conditions.
var DefaultConditions Conditions
DefaultConditions is a list of conditions that are used by default. It's initialized at package init.
func (*Conditions) Add ¶
func (c *Conditions) Add(condition Condition)
Add adds a condition to the list.
Example ¶
package main import ( "atomicgo.dev/isprod" "fmt" ) func main() { var conds isprod.Conditions cond1 := isprod.Condition{ EnvVarName: "ENV_VAR_1", AllowAnyValue: true, } cond2 := isprod.Condition{ EnvVarName: "ENV_VAR_2", AllowAnyValue: true, } conds.Add(cond1) conds.Add(cond2) fmt.Println(len(conds)) }
Output: 2
func (Conditions) Check ¶
func (c Conditions) Check() bool
Check checks if any of the conditions is true.
Example ¶
package main import ( "atomicgo.dev/isprod" "fmt" "os" ) func main() { os.Setenv("ENV_VAR_1", "true") var conds isprod.Conditions cond1 := isprod.Condition{ EnvVarName: "ENV_VAR_1", AllowAnyValue: true, } cond2 := isprod.Condition{ EnvVarName: "ENV_VAR_2", AllowAnyValue: true, } conds.Add(cond1) conds.Add(cond2) fmt.Println(conds.Check()) }
Output: true
func (Conditions) String ¶
func (c Conditions) String() string
String returns a string representation of the conditions in plain english.