forked from coop-cloud-mirrors/godotenv
Some golint feedback from http://goreportcard.com/report/joho/godotenv
This commit is contained in:
parent
e1c92610d7
commit
19b5c2bf30
81
godotenv.go
81
godotenv.go
@ -1,18 +1,17 @@
|
|||||||
/*
|
// Package godotenv
|
||||||
A go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)
|
// A go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)
|
||||||
|
//
|
||||||
Examples/readme can be found on the github page at https://github.com/joho/godotenv
|
// Examples/readme can be found on the github page at https://github.com/joho/godotenv
|
||||||
|
//
|
||||||
The TL;DR is that you make a .env file that looks something like
|
// The TL;DR is that you make a .env file that looks something like
|
||||||
|
//
|
||||||
SOME_ENV_VAR=somevalue
|
// SOME_ENV_VAR=somevalue
|
||||||
|
//
|
||||||
and then in your go code you can call
|
// and then in your go code you can call
|
||||||
|
//
|
||||||
godotenv.Load()
|
// godotenv.Load()
|
||||||
|
//
|
||||||
and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR")
|
// and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR")
|
||||||
*/
|
|
||||||
package godotenv
|
package godotenv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -23,17 +22,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
// Load will read your env file(s) and load them into ENV for this process.
|
||||||
Call this function as close as possible to the start of your program (ideally in main)
|
//
|
||||||
|
// Call this function as close as possible to the start of your program (ideally in main)
|
||||||
If you call Load without any args it will default to loading .env in the current path
|
//
|
||||||
|
// If you call Load without any args it will default to loading .env in the current path
|
||||||
You can otherwise tell it which files to load (there can be more than one) like
|
//
|
||||||
|
// You can otherwise tell it which files to load (there can be more than one) like
|
||||||
godotenv.Load("fileone", "filetwo")
|
//
|
||||||
|
// godotenv.Load("fileone", "filetwo")
|
||||||
It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
|
//
|
||||||
*/
|
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
|
||||||
func Load(filenames ...string) (err error) {
|
func Load(filenames ...string) (err error) {
|
||||||
filenames = filenamesOrDefault(filenames)
|
filenames = filenamesOrDefault(filenames)
|
||||||
|
|
||||||
@ -46,10 +45,8 @@ func Load(filenames ...string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Read all env (with same file loading semantics as Load) but return values as
|
||||||
Read all env (with same file loading semantics as Load) but return values as
|
// a map rather than automatically writing values into env
|
||||||
a map rather than automatically writing values into env
|
|
||||||
*/
|
|
||||||
func Read(filenames ...string) (envMap map[string]string, err error) {
|
func Read(filenames ...string) (envMap map[string]string, err error) {
|
||||||
filenames = filenamesOrDefault(filenames)
|
filenames = filenamesOrDefault(filenames)
|
||||||
envMap = make(map[string]string)
|
envMap = make(map[string]string)
|
||||||
@ -70,15 +67,13 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Exec loads env vars from the specified filenames (empty map falls back to default)
|
||||||
Loads env vars from the specified filenames (empty map falls back to default)
|
// then executes the cmd specified.
|
||||||
then executes the cmd specified.
|
//
|
||||||
|
// Simply hooks up os.Stdin/err/out to the command and calls Run()
|
||||||
Simply hooks up os.Stdin/err/out to the command and calls Run()
|
//
|
||||||
|
// If you want more fine grained control over your command it's recommended
|
||||||
If you want more fine grained control over your command it's recommended
|
// that you use `Load()` or `Read()` and the `os/exec` package yourself.
|
||||||
that you use `Load()` or `Read()` and the `os/exec` package yourself.
|
|
||||||
*/
|
|
||||||
func Exec(filenames []string, cmd string, cmdArgs []string) error {
|
func Exec(filenames []string, cmd string, cmdArgs []string) error {
|
||||||
Load(filenames...)
|
Load(filenames...)
|
||||||
|
|
||||||
@ -97,10 +92,10 @@ func filenamesOrDefault(filenames []string) []string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadFile(filename string) (err error) {
|
func loadFile(filename string) error {
|
||||||
envMap, err := readFile(filename)
|
envMap, err := readFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range envMap {
|
for key, value := range envMap {
|
||||||
@ -109,7 +104,7 @@ func loadFile(filename string) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFile(filename string) (envMap map[string]string, err error) {
|
func readFile(filename string) (envMap map[string]string, err error) {
|
||||||
@ -149,7 +144,7 @@ func parseLine(line string) (key string, value string, err error) {
|
|||||||
if strings.Contains(line, "#") {
|
if strings.Contains(line, "#") {
|
||||||
segmentsBetweenHashes := strings.Split(line, "#")
|
segmentsBetweenHashes := strings.Split(line, "#")
|
||||||
quotesAreOpen := false
|
quotesAreOpen := false
|
||||||
segmentsToKeep := make([]string, 0)
|
var segmentsToKeep []string
|
||||||
for _, segment := range segmentsBetweenHashes {
|
for _, segment := range segmentsBetweenHashes {
|
||||||
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
|
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
|
||||||
if quotesAreOpen {
|
if quotesAreOpen {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user