diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-02-05 00:37:32 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-02-05 00:37:32 +0100 |
| commit | 6960aecc25400320adee1b8802a86839326e15b6 (patch) | |
| tree | 334f7ca9491080a5e6f9a9747da77281c4958ba2 /vendor/github.com/joho/godotenv/README.md | |
| download | hepi-6960aecc25400320adee1b8802a86839326e15b6.tar.gz | |
Engage!
Diffstat (limited to 'vendor/github.com/joho/godotenv/README.md')
| -rw-r--r-- | vendor/github.com/joho/godotenv/README.md | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/vendor/github.com/joho/godotenv/README.md b/vendor/github.com/joho/godotenv/README.md new file mode 100644 index 0000000..bfbe66a --- /dev/null +++ b/vendor/github.com/joho/godotenv/README.md | |||
| @@ -0,0 +1,202 @@ | |||
| 1 | # GoDotEnv  [](https://goreportcard.com/report/github.com/joho/godotenv) | ||
| 2 | |||
| 3 | A Go (golang) port of the Ruby [dotenv](https://github.com/bkeepers/dotenv) project (which loads env vars from a .env file). | ||
| 4 | |||
| 5 | From the original Library: | ||
| 6 | |||
| 7 | > Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables. | ||
| 8 | > | ||
| 9 | > But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped. | ||
| 10 | |||
| 11 | It can be used as a library (for loading in env for your own daemons etc.) or as a bin command. | ||
| 12 | |||
| 13 | There is test coverage and CI for both linuxish and Windows environments, but I make no guarantees about the bin version working on Windows. | ||
| 14 | |||
| 15 | ## Installation | ||
| 16 | |||
| 17 | As a library | ||
| 18 | |||
| 19 | ```shell | ||
| 20 | go get github.com/joho/godotenv | ||
| 21 | ``` | ||
| 22 | |||
| 23 | or if you want to use it as a bin command | ||
| 24 | |||
| 25 | go >= 1.17 | ||
| 26 | ```shell | ||
| 27 | go install github.com/joho/godotenv/cmd/godotenv@latest | ||
| 28 | ``` | ||
| 29 | |||
| 30 | go < 1.17 | ||
| 31 | ```shell | ||
| 32 | go get github.com/joho/godotenv/cmd/godotenv | ||
| 33 | ``` | ||
| 34 | |||
| 35 | ## Usage | ||
| 36 | |||
| 37 | Add your application configuration to your `.env` file in the root of your project: | ||
| 38 | |||
| 39 | ```shell | ||
| 40 | S3_BUCKET=YOURS3BUCKET | ||
| 41 | SECRET_KEY=YOURSECRETKEYGOESHERE | ||
| 42 | ``` | ||
| 43 | |||
| 44 | Then in your Go app you can do something like | ||
| 45 | |||
| 46 | ```go | ||
| 47 | package main | ||
| 48 | |||
| 49 | import ( | ||
| 50 | "log" | ||
| 51 | "os" | ||
| 52 | |||
| 53 | "github.com/joho/godotenv" | ||
| 54 | ) | ||
| 55 | |||
| 56 | func main() { | ||
| 57 | err := godotenv.Load() | ||
| 58 | if err != nil { | ||
| 59 | log.Fatal("Error loading .env file") | ||
| 60 | } | ||
| 61 | |||
| 62 | s3Bucket := os.Getenv("S3_BUCKET") | ||
| 63 | secretKey := os.Getenv("SECRET_KEY") | ||
| 64 | |||
| 65 | // now do something with s3 or whatever | ||
| 66 | } | ||
| 67 | ``` | ||
| 68 | |||
| 69 | If you're even lazier than that, you can just take advantage of the autoload package which will read in `.env` on import | ||
| 70 | |||
| 71 | ```go | ||
| 72 | import _ "github.com/joho/godotenv/autoload" | ||
| 73 | ``` | ||
| 74 | |||
| 75 | While `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit | ||
| 76 | |||
| 77 | ```go | ||
| 78 | godotenv.Load("somerandomfile") | ||
| 79 | godotenv.Load("filenumberone.env", "filenumbertwo.env") | ||
| 80 | ``` | ||
| 81 | |||
| 82 | If you want to be really fancy with your env file you can do comments and exports (below is a valid env file) | ||
| 83 | |||
| 84 | ```shell | ||
| 85 | # I am a comment and that is OK | ||
| 86 | SOME_VAR=someval | ||
| 87 | FOO=BAR # comments at line end are OK too | ||
| 88 | export BAR=BAZ | ||
| 89 | ``` | ||
| 90 | |||
| 91 | Or finally you can do YAML(ish) style | ||
| 92 | |||
| 93 | ```yaml | ||
| 94 | FOO: bar | ||
| 95 | BAR: baz | ||
| 96 | ``` | ||
| 97 | |||
| 98 | as a final aside, if you don't want godotenv munging your env you can just get a map back instead | ||
| 99 | |||
| 100 | ```go | ||
| 101 | var myEnv map[string]string | ||
| 102 | myEnv, err := godotenv.Read() | ||
| 103 | |||
| 104 | s3Bucket := myEnv["S3_BUCKET"] | ||
| 105 | ``` | ||
| 106 | |||
| 107 | ... or from an `io.Reader` instead of a local file | ||
| 108 | |||
| 109 | ```go | ||
| 110 | reader := getRemoteFile() | ||
| 111 | myEnv, err := godotenv.Parse(reader) | ||
| 112 | ``` | ||
| 113 | |||
| 114 | ... or from a `string` if you so desire | ||
| 115 | |||
| 116 | ```go | ||
| 117 | content := getRemoteFileContent() | ||
| 118 | myEnv, err := godotenv.Unmarshal(content) | ||
| 119 | ``` | ||
| 120 | |||
| 121 | ### Precedence & Conventions | ||
| 122 | |||
| 123 | Existing envs take precedence of envs that are loaded later. | ||
| 124 | |||
| 125 | The [convention](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use) | ||
| 126 | for managing multiple environments (i.e. development, test, production) | ||
| 127 | is to create an env named `{YOURAPP}_ENV` and load envs in this order: | ||
| 128 | |||
| 129 | ```go | ||
| 130 | env := os.Getenv("FOO_ENV") | ||
| 131 | if "" == env { | ||
| 132 | env = "development" | ||
| 133 | } | ||
| 134 | |||
| 135 | godotenv.Load(".env." + env + ".local") | ||
| 136 | if "test" != env { | ||
| 137 | godotenv.Load(".env.local") | ||
| 138 | } | ||
| 139 | godotenv.Load(".env." + env) | ||
| 140 | godotenv.Load() // The Original .env | ||
| 141 | ``` | ||
| 142 | |||
| 143 | If you need to, you can also use `godotenv.Overload()` to defy this convention | ||
| 144 | and overwrite existing envs instead of only supplanting them. Use with caution. | ||
| 145 | |||
| 146 | ### Command Mode | ||
| 147 | |||
| 148 | Assuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH` | ||
| 149 | |||
| 150 | ``` | ||
| 151 | godotenv -f /some/path/to/.env some_command with some args | ||
| 152 | ``` | ||
| 153 | |||
| 154 | If you don't specify `-f` it will fall back on the default of loading `.env` in `PWD` | ||
| 155 | |||
| 156 | By default, it won't override existing environment variables; you can do that with the `-o` flag. | ||
| 157 | |||
| 158 | ### Writing Env Files | ||
| 159 | |||
| 160 | Godotenv can also write a map representing the environment to a correctly-formatted and escaped file | ||
| 161 | |||
| 162 | ```go | ||
| 163 | env, err := godotenv.Unmarshal("KEY=value") | ||
| 164 | err := godotenv.Write(env, "./.env") | ||
| 165 | ``` | ||
| 166 | |||
| 167 | ... or to a string | ||
| 168 | |||
| 169 | ```go | ||
| 170 | env, err := godotenv.Unmarshal("KEY=value") | ||
| 171 | content, err := godotenv.Marshal(env) | ||
| 172 | ``` | ||
| 173 | |||
| 174 | ## Contributing | ||
| 175 | |||
| 176 | Contributions are welcome, but with some caveats. | ||
| 177 | |||
| 178 | This library has been declared feature complete (see [#182](https://github.com/joho/godotenv/issues/182) for background) and will not be accepting issues or pull requests adding new functionality or breaking the library API. | ||
| 179 | |||
| 180 | Contributions would be gladly accepted that: | ||
| 181 | |||
| 182 | * bring this library's parsing into closer compatibility with the mainline dotenv implementations, in particular [Ruby's dotenv](https://github.com/bkeepers/dotenv) and [Node.js' dotenv](https://github.com/motdotla/dotenv) | ||
| 183 | * keep the library up to date with the go ecosystem (ie CI bumps, documentation changes, changes in the core libraries) | ||
| 184 | * bug fixes for use cases that pertain to the library's purpose of easing development of codebases deployed into twelve factor environments | ||
| 185 | |||
| 186 | *code changes without tests and references to peer dotenv implementations will not be accepted* | ||
| 187 | |||
| 188 | 1. Fork it | ||
| 189 | 2. Create your feature branch (`git checkout -b my-new-feature`) | ||
| 190 | 3. Commit your changes (`git commit -am 'Added some feature'`) | ||
| 191 | 4. Push to the branch (`git push origin my-new-feature`) | ||
| 192 | 5. Create new Pull Request | ||
| 193 | |||
| 194 | ## Releases | ||
| 195 | |||
| 196 | Releases should follow [Semver](http://semver.org/) though the first couple of releases are `v1` and `v1.1`. | ||
| 197 | |||
| 198 | Use [annotated tags for all releases](https://github.com/joho/godotenv/issues/30). Example `git tag -a v1.2.1` | ||
| 199 | |||
| 200 | ## Who? | ||
| 201 | |||
| 202 | The original library [dotenv](https://github.com/bkeepers/dotenv) was written by [Brandon Keepers](http://opensoul.org/), and this port was done by [John Barton](https://johnbarton.co/) based off the tests/fixtures in the original library. | ||
