Env - theNewDynamic/huge GitHub Wiki
HUGE/Env is responsible of everything environment. Its public functions are available to any user.
About Environment logic
HUGE/Env relies on its huge/env/Get function detailed below to determine the environment. As is, it's just a wrapper for hugo.Environment. huge/env/GetVar on the other hand is a helpful function to retrieve the value of an environment variable with a fall back on local environments.
Quick hugo.Environment aside
hugo.Environmentreturns the value of- the
--environment(or-e) flag set through Hugo CLI or, - if not set,
HUGO_ENVorHUGO_ENVIRONMENTenvironment variable. - If none of the above are set it returns
developmentin server mode, - and
productionin build mode.
In case of conflicts between HUGO_ENV/HUGO_ENVIRONMENT and the --environment flag, and contrary to Hugo, HUGE will always use the value of HUGO_ENV/HUGO_ENVIRONMENT.
TL;DR
| command | hugo.Environment |
huge/env/Get |
|---|---|---|
| huge server | development | development |
| hugo -e production | production | production |
| HUGO_ENV=production hugo | production | production |
| HUGO_ENV=production hugo -e cloudflare | cloudflare | production |
If the project relies on another logic than HUGE's to determine its environment, one can add its own partial at layouts/partials/huge/env/Get, it will overrides HUGE's.
Override example
Let's say the project relies on NODE_ENV for determining environment.
{{/* /layouts/partials/huge/env/Get */}}
{{ $env := hugo.Environment }}
{{ with getenv "NODE_ENV" }}
{{ $env = . }}
{{ end }}
{{ return $env }}
Now huge/env/Get and other HUGE/Env functions will also rely on NODE_ENV to evaluate.
Environment Variables
HUGE/Env also provides a helper function to retrieve environment variables which gracefully fallbacks on any key added to _huge/config/env.yaml if no "real" environment variable is found.
It is useful for working locally with a gitignored file similar to dotenv's .env logic:
#_huge/config/env.yaml
SECRET_API_KEY: 13derxxxxxxxxxx234
API_UR: https//api.here.com/
Functions
huge/env/Get
Lengthily discussed above.
Parameters
Any
Returns
A string representing the current environment. Logic is discussed above.
Example
{{ if eq (partial "huge/env/Get") "development" }}
{{ $debug = true }}
{{ end }}
huge/env/Is
Parameter
A string.
Returns
A boolean, true if the given string matches the current environment, false if not.
Example
{{ if partial "huge/env/Is" "production" }}
{{ $ga = true}}
{{ end }}
huge/env/IsNot
Parameter
A string.
Returns
A boolean, true if the given string does not match the current environment, false if it does.
Example
{{ if partial "huge/env/IsNot" "production" }}
{{ $debug = true }}
{{ end }}
huge/env/IsProduction
Parameter
Any
Returns
A boolean, true if the current environment matches production, false if not.
Example
{{ if partial "huge/env/IsProduction" "production" }}
{{ $debug = true }}
{{ end }}
huge/env/When
This is handy when you need to do something always or never or only in one or several given environment.
Parameter
A string which must be among a list of:
alwaysnever{environment}: A string representing one of the project's environments. It will be tested against the current environment.
OR a slice of strings whose strings will be tested against the current environment.
Returns
A boolean:
true if the given parameter is always or matches or contains the current environment.
false if the given parameter is never or do not match or do not contain the current environment.
huge/env/GetVar
Returns the value of the environment variable matching a given key. If none is found, falls back to the value of the given key as stored in _huge/config/env.yaml.
Parameters
String
Returns
A string representing the key of an environment variable.
Example
{{ with partial "huge/env/GetVar" "API_KEY" }}
{{ $data = resources.GetRemote (printf "https://api.endpoint/%s/data/" . }}
{{ end }}