Skip to content

Repository files navigation

AtomicGo



AtomicGo | isprod

DownloadsLatest ReleaseTestsCoverageUnit test countLicense: MITGo report


Documentation | Contributing | Code of Conduct


go get atomicgo.dev/isprod

isprod

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.

Index

func Check

funcCheck() 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 (
"fmt""os""atomicgo.dev/isprod"
)
funcmain() {
os.Setenv("PRODUCTION", "true") // Many common names are supported. See DefaultConditions.fmt.Println(isprod.Check())
}

Output

true

Condition is a condition that checks if the environment is production.

typeConditionstruct {
// EnvVarName is the name of the environment variable to check.EnvVarNamestring// 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.AllowAnyValuebool// ExcludedValues is a list of values that are specifically not allowed, even if AllowAnyValue is set to true.ExcludedValues []string
}

func (Condition) Check

func (cCondition) Check() bool

Check checks if the condition is met.

package main
import (
"fmt""os""atomicgo.dev/isprod"
)
funcmain() {
os.Setenv("MY_ENV_VAR", "live")
cond:= isprod.Condition{
EnvVarName: "MY_ENV_VAR",
AllowAnyValue: true,
ExcludedValues: []string{"false"},
}
fmt.Println(cond.Check())
}

Output

true

func (Condition) String

func (cCondition) String() string

Conditions is a list of conditions.

typeConditions []Condition

DefaultConditions is a list of conditions that are used by default. It's initialized at package init.

varDefaultConditionsConditions

func (*Conditions) Add

func (c*Conditions) Add(conditionCondition)

Add adds a condition to the list.

package main
import (
"fmt""atomicgo.dev/isprod"
)
funcmain() {
varconds isprod.Conditionscond1:= 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 (cConditions) Check() bool

Check checks if any of the conditions is true.

package main
import (
"fmt""os""atomicgo.dev/isprod"
)
funcmain() {
os.Setenv("ENV_VAR_1", "true")
varconds isprod.Conditionscond1:= 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 (cConditions) String() string

String returns a string representation of the conditions in plain english.

Generated by gomarkdoc


AtomicGo.dev · with ❤️ by @MarvinJWendt | mjw.dev

About

🔴 A minimalistic Go module to check if the current environment is running in production.

Topics

Resources

Code of conduct

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Generated from atomicgo/template