An open API service indexing awesome lists of open source software.

https://github.com/atomicgo/isprod

🔴 A minimalistic Go module to check if the current environment is running in production.
https://github.com/atomicgo/isprod

atomicgo go golang golang-library prod production

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

AtomicGo | isprod


Downloads


Latest Release


Tests


Coverage


Unit test count


License: MIT



Go report

---


Documentation
|
Contributing
|
Code of Conduct

---


AtomicGo


go get atomicgo.dev/isprod



# isprod

```go
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\(\) bool](<#Check>)
- [type Condition](<#Condition>)
- [func \(c Condition\) Check\(\) bool](<#Condition.Check>)
- [func \(c Condition\) String\(\) string](<#Condition.String>)
- [type Conditions](<#Conditions>)
- [func \(c \*Conditions\) Add\(condition Condition\)](<#Conditions.Add>)
- [func \(c Conditions\) Check\(\) bool](<#Conditions.Check>)
- [func \(c Conditions\) String\(\) string](<#Conditions.String>)


## func [Check]()

```go
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.

```go
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
```


## type [Condition]()

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

```go
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]()

```go
func (c Condition) Check() bool
```

Check checks if the condition is met.

```go
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
```


### func \(Condition\) [String]()

```go
func (c Condition) String() string
```


## type [Conditions]()

Conditions is a list of conditions.

```go
type Conditions []Condition
```

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

```go
var DefaultConditions Conditions
```


### func \(\*Conditions\) [Add]()

```go
func (c *Conditions) Add(condition Condition)
```

Add adds a condition to the list.

```go
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]()

```go
func (c Conditions) Check() bool
```

Check checks if any of the conditions is true.

```go
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]()

```go
func (c Conditions) String() string
```

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

Generated by [gomarkdoc]()

---

> [AtomicGo.dev](https://atomicgo.dev)  · 
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)