cenv uses comments in .env files to generate a schema used for checking the env files integrity. When working on a larger project, env files can change a lot and sometimes break your app if you have forgotten to add/edit/update certain fields. With cenv you mimize this risk by having a source of truth that makes sure your env is set up correctly.
Copy and run the following command. cenv will be put in $HOME/.local/bin. Make sure that the directory is in your $PATH.
curl -fsSL https://raw.githubusercontent.com/echo-webkom/cenv/refs/heads/main/install.sh | bashOnce installed, you can self-update with cenv upgrade.
You can also use cenv as a single-util package. See the package source.
go get github.com/echo-webkom/cenvfunc main() {
err := cenv.Load()
if err != nil {
log.Fatal(err)
}
...
}Add one or more of the following @tags above a field:
public: Marks the field as public. The value will be included in the schema. This is for required static values.required: Marks the field as required. The field has to be present, and have a non-empty value.length [number]: Requires a specified length for the fields value.default [value]: Set a default value. Runningcenv fixwill automatically fill this in if the field is empty.enum [value1] | [value2] | ...: Require that the field value is one of the given enum values, separated by|.format [format]: Requires a specified format for the value. Uses gokenizer patterns.
NOT_SO_IMPORTANT=123
# @required
API_KEY=foo-bar-baz
# @length 8
OTHER_KEY=abcdefgh
# Stacking multiple tags
# @required
# @length 4
# @format {number}
PIN_CODE=1234
# @enum user | guest | admin
# @default user
ROLE=userCreate a schema file from your .env:
# Creates a cenv.schema.json file
cenv updateCheck your .env after fetching the latest changes
# Compares your env with the existing cenv.schema.json
cenv checkThe fix command creates a .env file based on the schema, or fills in missing fields in an existing one. Any values already in your .env, like API keys will be kept, while any missing values will be added if a default or public one is provided.
cenv fixTo build the project, you need to have Go installed. Run the following command to build the project (make sure the bin directory exists):
go build -o bin/cenv app/main.goIf you want to overwrite the Version variable in main.go you have add the following flags:
go build -o bin/cenv -ldflags "-X 'github.com/echo-webkom/cenv/cmd.Version=<your-version>'" app/main.goThe cenv program also comes with a man page. Viewable by man cenv. You can install the man page locally with:
mkdir -p ~/.local/share/man/man1
cp cenv.1 ~/.local/share/man/man1