Documentation | Contributing | Code of Conduct
import "atomicgo.dev/isprod"
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.
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.
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())
}
true
type Condition
Condition is a condition that checks if the environment is production.
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
}
func (Condition) Check
func (c Condition) Check() bool
Check checks if the condition is met.
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())
}
true
func (Condition) String
func (c Condition) String() string
type Conditions
Conditions is a list of conditions.
type Conditions []Condition
DefaultConditions is a list of conditions that are used by default. It's initialized at package init.
var DefaultConditions Conditions
func (*Conditions) Add
func (c *Conditions) Add(condition Condition)
Add adds a condition to the list.
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))
}
2
func (Conditions) Check
func (c Conditions) Check() bool
Check checks if any of the conditions is true.
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())
}
true
func (Conditions) String
func (c Conditions) String() string
String returns a string representation of the conditions in plain english.
Generated by gomarkdoc
AtomicGo.dev Β Β·Β with β€οΈ by @MarvinJWendt | MarvinJWendt.com