-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kyle McCullough <[email protected]>
- Loading branch information
0 parents
commit 3eb7e14
Showing
19 changed files
with
1,254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
on: pull_request | ||
name: Check Commit | ||
env: | ||
GOPRIVATE: github.com/OpsHelmInc/* | ||
SSH_AUTH_SOCK: /tmp/ssh_agent.sock | ||
jobs: | ||
check_commit: | ||
name: Check Commit | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: 'latest' | ||
args: --timeout=5m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: CI | ||
|
||
on: [push] | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go: [ '1.21', '1.20' ] | ||
os: [ ubuntu-latest, macOS-latest, windows-latest ] | ||
name: ${{ matrix.os }} Go ${{ matrix.go }} Tests | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Setup go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
- run: go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
on: | ||
push: | ||
tags: | ||
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 | ||
|
||
name: Upload Release Assets | ||
|
||
jobs: | ||
build: | ||
name: Upload Release Assets | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Generate build files | ||
uses: thatisuday/go-cross-build@v1 | ||
with: | ||
platforms: 'linux/amd64, linux/ppc64le, darwin/amd64, windows/amd64' | ||
package: 'cmd/godotenv' | ||
name: 'godotenv' | ||
compress: 'true' | ||
dest: 'dist' | ||
- name: Publish Binaries | ||
uses: svenstaro/upload-release-action@v2 | ||
with: | ||
repo_token: ${{ secrets.GITHUB_TOKEN }} | ||
release_name: Release ${{ github.ref }} | ||
tag: ${{ github.ref }} | ||
file: dist/* | ||
file_glob: true | ||
overwrite: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
linters: | ||
disable-all: false | ||
# https://golangci-lint.run/usage/linters/#enabled-by-default | ||
enable: | ||
- gofmt | ||
- bodyclose | ||
- errname | ||
- errorlint | ||
- exportloopref | ||
- goimports | ||
- goprintffuncname | ||
- gosec | ||
- misspell | ||
- nilerr | ||
- nilnil | ||
- predeclared | ||
- stylecheck | ||
- thelper | ||
- tparallel | ||
- unparam | ||
- whitespace | ||
- gochecknoglobals | ||
- staticcheck | ||
- govet | ||
- forcetypeassert | ||
- goconst | ||
- exhaustive | ||
linters-settings: | ||
gofmt: | ||
rewrite-rules: | ||
- pattern: 'interface{}' | ||
replacement: 'any' | ||
goimports: | ||
local-prefixes: github.com/OpsHelmInc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Copyright (c) 2013 John Barton | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
# GoDotEnv ![CI](https://github.com/OpsHelmInc/godotenv/workflows/CI/badge.svg) | ||
|
||
A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) | ||
|
||
From the original Library: | ||
|
||
> 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. | ||
> | ||
> 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. | ||
It can be used as a library (for loading in env for your own daemons etc) or as a bin command. | ||
|
||
There is test coverage and CI for both linuxish and windows environments, but I make no guarantees about the bin version working on windows. | ||
|
||
## Installation | ||
|
||
As a library | ||
|
||
```shell | ||
go get github.com/OpsHelmInc/godotenv | ||
``` | ||
|
||
or if you want to use it as a bin command | ||
```shell | ||
go get github.com/OpsHelmInc/godotenv/cmd/godotenv | ||
``` | ||
|
||
## Usage | ||
|
||
Add your application configuration to your `.env` file in the root of your project: | ||
|
||
```shell | ||
S3_BUCKET=YOURS3BUCKET | ||
SECRET_KEY=YOURSECRETKEYGOESHERE | ||
``` | ||
|
||
Then in your Go app you can do something like | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/OpsHelmInc/godotenv" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
|
||
s3Bucket := os.Getenv("S3_BUCKET") | ||
secretKey := os.Getenv("SECRET_KEY") | ||
|
||
// now do something with s3 or whatever | ||
} | ||
``` | ||
|
||
If you're even lazier than that, you can just take advantage of the autoload package which will read in `.env` on import | ||
|
||
```go | ||
import _ "github.com/OpsHelmInc/godotenv/autoload" | ||
``` | ||
|
||
While `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit | ||
|
||
```go | ||
_ = godotenv.Load("somerandomfile") | ||
_ = godotenv.Load("filenumberone.env", "filenumbertwo.env") | ||
``` | ||
|
||
If you want to be really fancy with your env file you can do comments and exports (below is a valid env file) | ||
|
||
```shell | ||
# I am a comment and that is OK | ||
SOME_VAR=someval | ||
FOO=BAR # comments at line end are OK too | ||
export BAR=BAZ | ||
``` | ||
|
||
Or finally you can do YAML(ish) style | ||
|
||
```yaml | ||
FOO: bar | ||
BAR: baz | ||
``` | ||
as a final aside, if you don't want godotenv munging your env you can just get a map back instead | ||
```go | ||
var myEnv map[string]string | ||
myEnv, err := godotenv.Read() | ||
|
||
s3Bucket := myEnv["S3_BUCKET"] | ||
``` | ||
|
||
... or from an `io.Reader` instead of a local file | ||
|
||
```go | ||
reader := getRemoteFile() | ||
myEnv, err := godotenv.Parse(reader) | ||
``` | ||
|
||
... or from a `string` if you so desire | ||
|
||
```go | ||
content := getRemoteFileContent() | ||
myEnv, err := godotenv.Unmarshal(content) | ||
``` | ||
|
||
### Precedence & Conventions | ||
|
||
Existing envs take precedence of envs that are loaded later. | ||
|
||
The [convention](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use) | ||
for managing multiple environments (i.e. development, test, production) | ||
is to create an env named `{YOURAPP}_ENV` and load envs in this order: | ||
|
||
```go | ||
env := os.Getenv("FOO_ENV") | ||
if "" == env { | ||
env = "development" | ||
} | ||
|
||
godotenv.Load(".env." + env + ".local") | ||
if "test" != env { | ||
godotenv.Load(".env.local") | ||
} | ||
godotenv.Load(".env." + env) | ||
godotenv.Load() // The Original .env | ||
``` | ||
|
||
If you need to, you can also use `godotenv.Overload()` to defy this convention | ||
and overwrite existing envs instead of only supplanting them. Use with caution. | ||
|
||
### Command Mode | ||
|
||
Assuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH` | ||
|
||
``` | ||
godotenv -f /some/path/to/.env some_command with some args | ||
``` | ||
|
||
If you don't specify `-f` it will fall back on the default of loading `.env` in `PWD` | ||
|
||
### Writing Env Files | ||
|
||
Godotenv can also write a map representing the environment to a correctly-formatted and escaped file | ||
|
||
```go | ||
env, err := godotenv.Unmarshal("KEY=value") | ||
err := godotenv.Write(env, "./.env") | ||
``` | ||
|
||
... or to a string | ||
|
||
```go | ||
env, err := godotenv.Unmarshal("KEY=value") | ||
content, err := godotenv.Marshal(env) | ||
``` | ||
|
||
## Contributing | ||
|
||
Contributions are most welcome! The parser itself is pretty stupidly naive and I wouldn't be surprised if it breaks with edge cases. | ||
|
||
*code changes without tests will not be accepted* | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Added some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request | ||
|
||
## Releases | ||
|
||
Releases should follow [Semver](http://semver.org/) though the first couple of releases are `v1` and `v1.1`. | ||
|
||
Use [annotated tags for all releases](https://github.com/OpsHelmInc/godotenv/issues/30). Example `git tag -a v1.2.1` | ||
|
||
|
||
## Who? | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package autoload | ||
|
||
/* | ||
You can just read the .env file on import just by doing | ||
import _ "github.com/OpsHelmInc/godotenv/autoload" | ||
And bob's your mother's brother | ||
*/ | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/OpsHelmInc/godotenv" | ||
) | ||
|
||
func init() { | ||
if err := godotenv.Load(); err != nil { | ||
log.Printf("error loading environment file: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/OpsHelmInc/godotenv" | ||
) | ||
|
||
func main() { | ||
var showHelp bool | ||
flag.BoolVar(&showHelp, "h", false, "show help") | ||
var rawEnvFilenames string | ||
flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files") | ||
|
||
flag.Parse() | ||
|
||
usage := ` | ||
Run a process with an env setup from a .env file | ||
godotenv [-f ENV_FILE_PATHS] COMMAND_ARGS | ||
ENV_FILE_PATHS: comma separated paths to .env files | ||
COMMAND_ARGS: command and args you want to run | ||
example | ||
godotenv -f /path/to/something/.env,/another/path/.env fortune | ||
` | ||
// if no args or -h flag | ||
// print usage and return | ||
args := flag.Args() | ||
if showHelp || len(args) == 0 { | ||
fmt.Println(usage) | ||
return | ||
} | ||
|
||
// load env | ||
var envFilenames []string | ||
if rawEnvFilenames != "" { | ||
envFilenames = strings.Split(rawEnvFilenames, ",") | ||
} | ||
|
||
// take rest of args and "exec" them | ||
cmd := args[0] | ||
cmdArgs := args[1:] | ||
|
||
err := godotenv.Exec(envFilenames, cmd, cmdArgs) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export OPTION_A='postgres://localhost:5432/database?sslmode=disable' | ||
|
Oops, something went wrong.