mirror of
https://github.com/mudler/luet.git
synced 2025-09-01 15:18:28 +00:00
update vendor/
This commit is contained in:
3
vendor/github.com/otiai10/copy/.gitignore
generated
vendored
Normal file
3
vendor/github.com/otiai10/copy/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
testdata.copy
|
||||
coverage.txt
|
||||
vendor
|
11
vendor/github.com/otiai10/copy/.travis.yml
generated
vendored
Normal file
11
vendor/github.com/otiai10/copy/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.9
|
||||
- tip
|
||||
before_script:
|
||||
- go get -t ./...
|
||||
script:
|
||||
- go test ./... -v
|
||||
- go test -race -coverprofile=coverage.txt -covermode=atomic
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
21
vendor/github.com/otiai10/copy/LICENSE
generated
vendored
Normal file
21
vendor/github.com/otiai10/copy/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 otiai10
|
||||
|
||||
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.
|
14
vendor/github.com/otiai10/copy/README.md
generated
vendored
Normal file
14
vendor/github.com/otiai10/copy/README.md
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# copy
|
||||
|
||||
[](https://travis-ci.org/otiai10/copy)
|
||||
[](https://codecov.io/gh/otiai10/copy)
|
||||
[](https://godoc.org/github.com/otiai10/copy)
|
||||
[](https://goreportcard.com/report/github.com/otiai10/copy)
|
||||
|
||||
`copy` copies directories recursively.
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
err := Copy("your/directory", "your/directory.copy")
|
||||
```
|
106
vendor/github.com/otiai10/copy/copy.go
generated
vendored
Normal file
106
vendor/github.com/otiai10/copy/copy.go
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
package copy
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
// tmpPermissionForDirectory makes the destination directory writable,
|
||||
// so that stuff can be copied recursively even if any original directory is NOT writable.
|
||||
// See https://github.com/otiai10/copy/pull/9 for more information.
|
||||
tmpPermissionForDirectory = os.FileMode(0755)
|
||||
)
|
||||
|
||||
// Copy copies src to dest, doesn't matter if src is a directory or a file
|
||||
func Copy(src, dest string) error {
|
||||
info, err := os.Lstat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return copy(src, dest, info)
|
||||
}
|
||||
|
||||
// copy dispatches copy-funcs according to the mode.
|
||||
// Because this "copy" could be called recursively,
|
||||
// "info" MUST be given here, NOT nil.
|
||||
func copy(src, dest string, info os.FileInfo) error {
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return lcopy(src, dest, info)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return dcopy(src, dest, info)
|
||||
}
|
||||
return fcopy(src, dest, info)
|
||||
}
|
||||
|
||||
// fcopy is for just a file,
|
||||
// with considering existence of parent directory
|
||||
// and file permission.
|
||||
func fcopy(src, dest string, info os.FileInfo) error {
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err = os.Chmod(f.Name(), info.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
_, err = io.Copy(f, s)
|
||||
return err
|
||||
}
|
||||
|
||||
// dcopy is for a directory,
|
||||
// with scanning contents inside the directory
|
||||
// and pass everything to "copy" recursively.
|
||||
func dcopy(srcdir, destdir string, info os.FileInfo) error {
|
||||
|
||||
originalMode := info.Mode()
|
||||
|
||||
// Make dest dir with 0755 so that everything writable.
|
||||
if err := os.MkdirAll(destdir, tmpPermissionForDirectory); err != nil {
|
||||
return err
|
||||
}
|
||||
// Recover dir mode with original one.
|
||||
defer os.Chmod(destdir, originalMode)
|
||||
|
||||
contents, err := ioutil.ReadDir(srcdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, content := range contents {
|
||||
cs, cd := filepath.Join(srcdir, content.Name()), filepath.Join(destdir, content.Name())
|
||||
if err := copy(cs, cd, content); err != nil {
|
||||
// If any error, exit immediately
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// lcopy is for a symlink,
|
||||
// with just creating a new symlink by replicating src symlink.
|
||||
func lcopy(src, dest string, info os.FileInfo) error {
|
||||
src, err := os.Readlink(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Symlink(src, dest)
|
||||
}
|
5
vendor/github.com/otiai10/copy/go.mod
generated
vendored
Normal file
5
vendor/github.com/otiai10/copy/go.mod
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/otiai10/copy
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/otiai10/mint v1.3.0
|
3
vendor/github.com/otiai10/copy/go.sum
generated
vendored
Normal file
3
vendor/github.com/otiai10/copy/go.sum
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
|
||||
github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc=
|
||||
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
7
vendor/github.com/spf13/viper/.travis.yml
generated
vendored
7
vendor/github.com/spf13/viper/.travis.yml
generated
vendored
@@ -4,10 +4,13 @@ language: go
|
||||
|
||||
env:
|
||||
global:
|
||||
- GO111MODULE="on"
|
||||
- GO111MODULE="on"
|
||||
- GOFLAGS="-mod=readonly"
|
||||
|
||||
go:
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- tip
|
||||
|
||||
os:
|
||||
@@ -27,5 +30,3 @@ script:
|
||||
after_success:
|
||||
- go get -u -d github.com/spf13/hugo
|
||||
- cd $GOPATH/src/github.com/spf13/hugo && make && ./hugo -s docs && cd -
|
||||
|
||||
sudo: false
|
||||
|
77
vendor/github.com/spf13/viper/README.md
generated
vendored
77
vendor/github.com/spf13/viper/README.md
generated
vendored
@@ -15,6 +15,10 @@ Many Go projects are built using Viper including:
|
||||
|
||||
[](https://travis-ci.org/spf13/viper) [](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://godoc.org/github.com/spf13/viper)
|
||||
|
||||
## Install
|
||||
```console
|
||||
go get -u github.com/spf13/viper
|
||||
```
|
||||
|
||||
## What is Viper?
|
||||
|
||||
@@ -23,7 +27,7 @@ to work within an application, and can handle all types of configuration needs
|
||||
and formats. It supports:
|
||||
|
||||
* setting defaults
|
||||
* reading from JSON, TOML, YAML, HCL, and Java properties config files
|
||||
* reading from JSON, TOML, YAML, HCL, envfile and Java properties config files
|
||||
* live watching and re-reading of config files (optional)
|
||||
* reading from environment variables
|
||||
* reading from remote config systems (etcd or Consul), and watching changes
|
||||
@@ -42,7 +46,7 @@ Viper is here to help with that.
|
||||
|
||||
Viper does the following for you:
|
||||
|
||||
1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats.
|
||||
1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, envfile or Java properties formats.
|
||||
2. Provide a mechanism to set default values for your different
|
||||
configuration options.
|
||||
3. Provide a mechanism to set override values for options specified through
|
||||
@@ -83,7 +87,7 @@ viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "cat
|
||||
### Reading Config Files
|
||||
|
||||
Viper requires minimal configuration so it knows where to look for config files.
|
||||
Viper supports JSON, TOML, YAML, HCL, and Java Properties files. Viper can search multiple paths, but
|
||||
Viper supports JSON, TOML, YAML, HCL, envfile and Java Properties files. Viper can search multiple paths, but
|
||||
currently a single Viper instance only supports a single configuration file.
|
||||
Viper does not default to any configuration search paths leaving defaults decision
|
||||
to an application.
|
||||
@@ -103,6 +107,42 @@ if err != nil { // Handle errors reading the config file
|
||||
}
|
||||
```
|
||||
|
||||
You can handle the specific case where no config file is found like this:
|
||||
|
||||
```go
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// Config file not found; ignore error if desired
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
}
|
||||
}
|
||||
|
||||
// Config file found and successfully parsed
|
||||
```
|
||||
|
||||
### Writing Config Files
|
||||
|
||||
Reading from config files is useful, but at times you want to store all modifications made at run time.
|
||||
For that, a bunch of commands are available, each with its own purpose:
|
||||
|
||||
* WriteConfig - writes the current viper configuration to the predefined path, if exists. Errors if no predefined path. Will overwrite the current config file, if it exists.
|
||||
* SafeWriteConfig - writes the current viper configuration to the predefined path. Errors if no predefined path. Will not overwrite the current config file, if it exists.
|
||||
* WriteConfigAs - writes the current viper configuration to the given filepath. Will overwrite the given file, if it exists.
|
||||
* SafeWriteConfigAs - writes the current viper configuration to the given filepath. Will not overwrite the given file, if it exists.
|
||||
|
||||
As a rule of the thumb, everything marked with safe won't overwrite any file, but just create if not existent, whilst the default behavior is to create or truncate.
|
||||
|
||||
A small examples section:
|
||||
|
||||
```go
|
||||
viper.WriteConfig() // writes current config to predefined path set by 'viper.AddConfigPath()' and 'viper.SetConfigName'
|
||||
viper.SafeWriteConfig()
|
||||
viper.WriteConfigAs("/path/to/my/.config")
|
||||
viper.SafeWriteConfigAs("/path/to/my/.config") // will error since it has already been written
|
||||
viper.SafeWriteConfigAs("/path/to/my/.other_config")
|
||||
```
|
||||
|
||||
### Watching and re-reading config files
|
||||
|
||||
Viper supports the ability to have your application live read a config file while running.
|
||||
@@ -186,7 +226,7 @@ with ENV:
|
||||
* `BindEnv(string...) : error`
|
||||
* `SetEnvPrefix(string)`
|
||||
* `SetEnvKeyReplacer(string...) *strings.Replacer`
|
||||
* `AllowEmptyEnvVar(bool)`
|
||||
* `AllowEmptyEnv(bool)`
|
||||
|
||||
_When working with ENV variables, it’s important to recognize that Viper
|
||||
treats ENV variables as case sensitive._
|
||||
@@ -199,9 +239,9 @@ prefix.
|
||||
`BindEnv` takes one or two parameters. The first parameter is the key name, the
|
||||
second is the name of the environment variable. The name of the environment
|
||||
variable is case sensitive. If the ENV variable name is not provided, then
|
||||
Viper will automatically assume that the key name matches the ENV variable name,
|
||||
but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV
|
||||
variable name, it **does not** automatically add the prefix.
|
||||
Viper will automatically assume that the ENV variable matches the following format: prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV variable name (the second parameter),
|
||||
it **does not** automatically add the prefix. For example if the second parameter is "id",
|
||||
Viper will look for the ENV variable "ID".
|
||||
|
||||
One important thing to recognize when working with ENV variables is that the
|
||||
value will be read each time it is accessed. Viper does not fix the value when
|
||||
@@ -346,7 +386,7 @@ package:
|
||||
|
||||
`import _ "github.com/spf13/viper/remote"`
|
||||
|
||||
Viper will read a config string (as JSON, TOML, YAML or HCL) retrieved from a path
|
||||
Viper will read a config string (as JSON, TOML, YAML, HCL or envfile) retrieved from a path
|
||||
in a Key/Value store such as etcd or Consul. These values take precedence over
|
||||
default values, but are overridden by configuration values retrieved from disk,
|
||||
flags, or environment variables.
|
||||
@@ -381,7 +421,7 @@ how to use Consul.
|
||||
#### etcd
|
||||
```go
|
||||
viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
|
||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
|
||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
|
||||
err := viper.ReadRemoteConfig()
|
||||
```
|
||||
|
||||
@@ -409,7 +449,7 @@ fmt.Println(viper.Get("hostname")) // myhostname.com
|
||||
|
||||
```go
|
||||
viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg")
|
||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
|
||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
|
||||
err := viper.ReadRemoteConfig()
|
||||
```
|
||||
|
||||
@@ -420,7 +460,7 @@ err := viper.ReadRemoteConfig()
|
||||
var runtime_viper = viper.New()
|
||||
|
||||
runtime_viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001", "/config/hugo.yml")
|
||||
runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
|
||||
runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
|
||||
|
||||
// read from remote config the first time.
|
||||
err := runtime_viper.ReadRemoteConfig()
|
||||
@@ -456,6 +496,7 @@ The following functions and methods exist:
|
||||
* `GetBool(key string) : bool`
|
||||
* `GetFloat64(key string) : float64`
|
||||
* `GetInt(key string) : int`
|
||||
* `GetIntSlice(key string) : []int`
|
||||
* `GetString(key string) : string`
|
||||
* `GetStringMap(key string) : map[string]interface{}`
|
||||
* `GetStringMapString(key string) : map[string]string`
|
||||
@@ -611,15 +652,17 @@ type config struct {
|
||||
|
||||
var C config
|
||||
|
||||
err := Unmarshal(&C)
|
||||
err := viper.Unmarshal(&C)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to decode into struct, %v", err)
|
||||
}
|
||||
```
|
||||
|
||||
Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default.
|
||||
|
||||
### Marshalling to string
|
||||
|
||||
You may need to marhsal all the settings held in viper into a string rather than write them to a file.
|
||||
You may need to marshal all the settings held in viper into a string rather than write them to a file.
|
||||
You can use your favorite format's marshaller with the config returned by `AllSettings()`.
|
||||
|
||||
```go
|
||||
@@ -630,11 +673,11 @@ import (
|
||||
|
||||
func yamlStringSettings() string {
|
||||
c := viper.AllSettings()
|
||||
bs, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to marshal config to YAML: %v", err)
|
||||
bs, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to marshal config to YAML: %v", err)
|
||||
}
|
||||
return string(bs)
|
||||
return string(bs)
|
||||
}
|
||||
```
|
||||
|
||||
|
2
vendor/github.com/spf13/viper/flags.go
generated
vendored
2
vendor/github.com/spf13/viper/flags.go
generated
vendored
@@ -36,7 +36,7 @@ type pflagValue struct {
|
||||
flag *pflag.Flag
|
||||
}
|
||||
|
||||
// HasChanges returns whether the flag has changes or not.
|
||||
// HasChanged returns whether the flag has changes or not.
|
||||
func (p pflagValue) HasChanged() bool {
|
||||
return p.flag.Changed
|
||||
}
|
||||
|
7
vendor/github.com/spf13/viper/go.mod
generated
vendored
7
vendor/github.com/spf13/viper/go.mod
generated
vendored
@@ -1,5 +1,7 @@
|
||||
module github.com/spf13/viper
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
|
||||
github.com/coreos/bbolt v1.3.2 // indirect
|
||||
@@ -18,7 +20,7 @@ require (
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.9.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0
|
||||
github.com/jonboulle/clockwork v0.1.0 // indirect
|
||||
github.com/magiconair/properties v1.8.0
|
||||
github.com/magiconair/properties v1.8.1
|
||||
github.com/mitchellh/mapstructure v1.1.2
|
||||
github.com/pelletier/go-toml v1.2.0
|
||||
github.com/prometheus/client_golang v0.9.3 // indirect
|
||||
@@ -28,6 +30,7 @@ require (
|
||||
github.com/spf13/jwalterweatherman v1.0.0
|
||||
github.com/spf13/pflag v1.0.3
|
||||
github.com/stretchr/testify v1.2.2
|
||||
github.com/subosito/gotenv v1.2.0
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
|
||||
github.com/ugorji/go v1.1.4 // indirect
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
|
||||
@@ -39,5 +42,5 @@ require (
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
||||
google.golang.org/grpc v1.21.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
gopkg.in/yaml.v2 v2.2.4
|
||||
)
|
||||
|
10
vendor/github.com/spf13/viper/go.sum
generated
vendored
10
vendor/github.com/spf13/viper/go.sum
generated
vendored
@@ -71,8 +71,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
|
||||
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
@@ -115,6 +115,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
|
||||
@@ -173,6 +175,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
84
vendor/github.com/spf13/viper/viper.go
generated
vendored
84
vendor/github.com/spf13/viper/viper.go
generated
vendored
@@ -3,7 +3,7 @@
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Viper is a application configuration system.
|
||||
// Viper is an application configuration system.
|
||||
// It believes that applications can be configured a variety of ways
|
||||
// via flags, ENVIRONMENT variables, configuration files retrieved
|
||||
// from the file system, or a remote key/value store.
|
||||
@@ -45,6 +45,7 @@ import (
|
||||
"github.com/spf13/cast"
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/subosito/gotenv"
|
||||
)
|
||||
|
||||
// ConfigMarshalError happens when failing to marshal the configuration.
|
||||
@@ -225,12 +226,12 @@ func New() *Viper {
|
||||
return v
|
||||
}
|
||||
|
||||
// Intended for testing, will reset all to default settings.
|
||||
// Reset is intended for testing, will reset all to default settings.
|
||||
// In the public interface for the viper package so applications
|
||||
// can use it in their testing as well.
|
||||
func Reset() {
|
||||
v = New()
|
||||
SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
|
||||
SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env"}
|
||||
SupportedRemoteProviders = []string{"etcd", "consul"}
|
||||
}
|
||||
|
||||
@@ -269,7 +270,7 @@ type RemoteProvider interface {
|
||||
}
|
||||
|
||||
// SupportedExts are universally supported extensions.
|
||||
var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
|
||||
var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env"}
|
||||
|
||||
// SupportedRemoteProviders are universally supported remote providers.
|
||||
var SupportedRemoteProviders = []string{"etcd", "consul"}
|
||||
@@ -294,6 +295,7 @@ func (v *Viper) WatchConfig() {
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
initWG.Done()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -343,7 +345,7 @@ func (v *Viper) WatchConfig() {
|
||||
}
|
||||
}()
|
||||
watcher.Add(configDir)
|
||||
initWG.Done() // done initalizing the watch in this go routine, so the parent routine can move on...
|
||||
initWG.Done() // done initializing the watch in this go routine, so the parent routine can move on...
|
||||
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
|
||||
}()
|
||||
initWG.Wait() // make sure that the go routine above fully ended before returning
|
||||
@@ -705,6 +707,8 @@ func (v *Viper) Get(key string) interface{} {
|
||||
return cast.ToDuration(val)
|
||||
case []string:
|
||||
return cast.ToStringSlice(val)
|
||||
case []int:
|
||||
return cast.ToIntSlice(val)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -794,6 +798,12 @@ func (v *Viper) GetDuration(key string) time.Duration {
|
||||
return cast.ToDuration(v.Get(key))
|
||||
}
|
||||
|
||||
// GetIntSlice returns the value associated with the key as a slice of int values.
|
||||
func GetIntSlice(key string) []int { return v.GetIntSlice(key) }
|
||||
func (v *Viper) GetIntSlice(key string) []int {
|
||||
return cast.ToIntSlice(v.Get(key))
|
||||
}
|
||||
|
||||
// GetStringSlice returns the value associated with the key as a slice of strings.
|
||||
func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
|
||||
func (v *Viper) GetStringSlice(key string) []string {
|
||||
@@ -1012,6 +1022,11 @@ func (v *Viper) find(lcaseKey string) interface{} {
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return res
|
||||
case "intSlice":
|
||||
s := strings.TrimPrefix(flag.ValueString(), "[")
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return cast.ToIntSlice(res)
|
||||
default:
|
||||
return flag.ValueString()
|
||||
}
|
||||
@@ -1081,6 +1096,11 @@ func (v *Viper) find(lcaseKey string) interface{} {
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return res
|
||||
case "intSlice":
|
||||
s := strings.TrimPrefix(flag.ValueString(), "[")
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return cast.ToIntSlice(res)
|
||||
default:
|
||||
return flag.ValueString()
|
||||
}
|
||||
@@ -1123,8 +1143,8 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
|
||||
v.envKeyReplacer = r
|
||||
}
|
||||
|
||||
// Aliases provide another accessor for the same key.
|
||||
// This enables one to change a name without breaking the application
|
||||
// RegisterAlias creates an alias that provides another accessor for the same key.
|
||||
// This enables one to change a name without breaking the application.
|
||||
func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
|
||||
func (v *Viper) RegisterAlias(alias string, key string) {
|
||||
v.registerAlias(alias, strings.ToLower(key))
|
||||
@@ -1344,21 +1364,21 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
||||
if v.config == nil {
|
||||
v.config = make(map[string]interface{})
|
||||
}
|
||||
var flags int
|
||||
if force == true {
|
||||
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
||||
} else {
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
flags = os.O_WRONLY
|
||||
} else {
|
||||
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
|
||||
}
|
||||
flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
||||
if !force {
|
||||
flags |= os.O_EXCL
|
||||
}
|
||||
f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return v.marshalWriter(f, configType)
|
||||
defer f.Close()
|
||||
|
||||
if err := v.marshalWriter(f, configType); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Sync()
|
||||
}
|
||||
|
||||
// Unmarshal a Reader into a map.
|
||||
@@ -1382,7 +1402,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||
}
|
||||
|
||||
case "hcl":
|
||||
obj, err := hcl.Parse(string(buf.Bytes()))
|
||||
obj, err := hcl.Parse(buf.String())
|
||||
if err != nil {
|
||||
return ConfigParseError{err}
|
||||
}
|
||||
@@ -1400,6 +1420,15 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||
c[k] = v
|
||||
}
|
||||
|
||||
case "dotenv", "env":
|
||||
env, err := gotenv.StrictParse(buf)
|
||||
if err != nil {
|
||||
return ConfigParseError{err}
|
||||
}
|
||||
for k, v := range env {
|
||||
c[k] = v
|
||||
}
|
||||
|
||||
case "properties", "props", "prop":
|
||||
v.properties = properties.NewProperties()
|
||||
var err error
|
||||
@@ -1440,6 +1469,9 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||
|
||||
case "hcl":
|
||||
b, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
return ConfigMarshalError{err}
|
||||
}
|
||||
ast, err := hcl.Parse(string(b))
|
||||
if err != nil {
|
||||
return ConfigMarshalError{err}
|
||||
@@ -1465,6 +1497,18 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||
return ConfigMarshalError{err}
|
||||
}
|
||||
|
||||
case "dotenv", "env":
|
||||
lines := []string{}
|
||||
for _, key := range v.AllKeys() {
|
||||
envName := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
||||
val := v.Get(key)
|
||||
lines = append(lines, fmt.Sprintf("%v=%v", envName, val))
|
||||
}
|
||||
s := strings.Join(lines, "\n")
|
||||
if _, err := f.WriteString(s); err != nil {
|
||||
return ConfigMarshalError{err}
|
||||
}
|
||||
|
||||
case "toml":
|
||||
t, err := toml.TreeFromMap(c)
|
||||
if err != nil {
|
||||
@@ -1679,7 +1723,7 @@ func (v *Viper) AllKeys() []string {
|
||||
m = v.flattenAndMergeMap(m, v.defaults, "")
|
||||
|
||||
// convert set of paths to list
|
||||
a := []string{}
|
||||
a := make([]string, 0, len(m))
|
||||
for x := range m {
|
||||
a = append(a, x)
|
||||
}
|
||||
@@ -1728,7 +1772,7 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
|
||||
func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool {
|
||||
// scan keys
|
||||
outer:
|
||||
for k, _ := range m {
|
||||
for k := range m {
|
||||
path := strings.Split(k, v.keyDelim)
|
||||
// scan intermediate paths
|
||||
var parentKey string
|
||||
|
1
vendor/github.com/subosito/gotenv/.env
generated
vendored
Normal file
1
vendor/github.com/subosito/gotenv/.env
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
HELLO=world
|
1
vendor/github.com/subosito/gotenv/.env.invalid
generated
vendored
Normal file
1
vendor/github.com/subosito/gotenv/.env.invalid
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lol$wut
|
3
vendor/github.com/subosito/gotenv/.gitignore
generated
vendored
Normal file
3
vendor/github.com/subosito/gotenv/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*.test
|
||||
*.out
|
||||
annotate.json
|
10
vendor/github.com/subosito/gotenv/.travis.yml
generated
vendored
Normal file
10
vendor/github.com/subosito/gotenv/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.x
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
script:
|
||||
- go test -test.v -coverprofile=coverage.out -covermode=count
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
47
vendor/github.com/subosito/gotenv/CHANGELOG.md
generated
vendored
Normal file
47
vendor/github.com/subosito/gotenv/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
# Changelog
|
||||
|
||||
## [1.2.0] - 2019-08-03
|
||||
|
||||
### Added
|
||||
|
||||
- Add `Must` helper to raise an error as panic. It can be used with `Load` and `OverLoad`.
|
||||
- Add more tests to be 100% coverage.
|
||||
- Add CHANGELOG
|
||||
- Add more OS for the test: OSX and Windows
|
||||
|
||||
### Changed
|
||||
|
||||
- Reduce complexity and improve source code for having `A+` score in [goreportcard](https://goreportcard.com/report/github.com/subosito/gotenv).
|
||||
- Updated README with mentions to all available functions
|
||||
|
||||
### Removed
|
||||
|
||||
- Remove `ErrFormat`
|
||||
- Remove `MustLoad` and `MustOverload`, replaced with `Must` helper.
|
||||
|
||||
## [1.1.1] - 2018-06-05
|
||||
|
||||
### Changed
|
||||
|
||||
- Replace `os.Getenv` with `os.LookupEnv` to ensure that the environment variable is not set, by [radding](https://github.com/radding)
|
||||
|
||||
## [1.1.0] - 2017-03-20
|
||||
|
||||
### Added
|
||||
|
||||
- Supports carriage return in env
|
||||
- Handle files with UTF-8 BOM
|
||||
|
||||
### Changed
|
||||
|
||||
- Whitespace handling
|
||||
|
||||
### Fixed
|
||||
|
||||
- Incorrect variable expansion
|
||||
- Handling escaped '$' characters
|
||||
|
||||
## [1.0.0] - 2014-10-05
|
||||
|
||||
First stable release.
|
||||
|
21
vendor/github.com/subosito/gotenv/LICENSE
generated
vendored
Normal file
21
vendor/github.com/subosito/gotenv/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Alif Rachmawadi
|
||||
|
||||
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.
|
131
vendor/github.com/subosito/gotenv/README.md
generated
vendored
Normal file
131
vendor/github.com/subosito/gotenv/README.md
generated
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
# gotenv
|
||||
|
||||
[](https://travis-ci.org/subosito/gotenv)
|
||||
[](https://ci.appveyor.com/project/subosito/gotenv/branch/master)
|
||||
[](https://codecov.io/gh/subosito/gotenv)
|
||||
[](https://goreportcard.com/report/github.com/subosito/gotenv)
|
||||
[](https://godoc.org/github.com/subosito/gotenv)
|
||||
|
||||
Load environment variables dynamically in Go.
|
||||
|
||||
## Usage
|
||||
|
||||
Put the gotenv package on your `import` statement:
|
||||
|
||||
```go
|
||||
import "github.com/subosito/gotenv"
|
||||
```
|
||||
|
||||
To modify your app environment variables, `gotenv` expose 2 main functions:
|
||||
|
||||
- `gotenv.Load`
|
||||
- `gotenv.Apply`
|
||||
|
||||
By default, `gotenv.Load` will look for a file called `.env` in the current working directory.
|
||||
|
||||
Behind the scene, it will then load `.env` file and export the valid variables to the environment variables. Make sure you call the method as soon as possible to ensure it loads all variables, say, put it on `init()` function.
|
||||
|
||||
Once loaded you can use `os.Getenv()` to get the value of the variable.
|
||||
|
||||
Let's say you have `.env` file:
|
||||
|
||||
```
|
||||
APP_ID=1234567
|
||||
APP_SECRET=abcdef
|
||||
```
|
||||
|
||||
Here's the example of your app:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/subosito/gotenv"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gotenv.Load()
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println(os.Getenv("APP_ID")) // "1234567"
|
||||
log.Println(os.Getenv("APP_SECRET")) // "abcdef"
|
||||
}
|
||||
```
|
||||
|
||||
You can also load other than `.env` file if you wish. Just supply filenames when calling `Load()`. It will load them in order and the first value set for a variable will win.:
|
||||
|
||||
```go
|
||||
gotenv.Load(".env.production", "credentials")
|
||||
```
|
||||
|
||||
While `gotenv.Load` loads entries from `.env` file, `gotenv.Apply` allows you to use any `io.Reader`:
|
||||
|
||||
```go
|
||||
gotenv.Apply(strings.NewReader("APP_ID=1234567"))
|
||||
|
||||
log.Println(os.Getenv("APP_ID"))
|
||||
// Output: "1234567"
|
||||
```
|
||||
|
||||
Both `gotenv.Load` and `gotenv.Apply` **DO NOT** overrides existing environment variables. If you want to override existing ones, you can see section below.
|
||||
|
||||
### Environment Overrides
|
||||
|
||||
Besides above functions, `gotenv` also provides another functions that overrides existing:
|
||||
|
||||
- `gotenv.OverLoad`
|
||||
- `gotenv.OverApply`
|
||||
|
||||
|
||||
Here's the example of this overrides behavior:
|
||||
|
||||
```go
|
||||
os.Setenv("HELLO", "world")
|
||||
|
||||
// NOTE: using Apply existing value will be reserved
|
||||
gotenv.Apply(strings.NewReader("HELLO=universe"))
|
||||
fmt.Println(os.Getenv("HELLO"))
|
||||
// Output: "world"
|
||||
|
||||
// NOTE: using OverApply existing value will be overridden
|
||||
gotenv.OverApply(strings.NewReader("HELLO=universe"))
|
||||
fmt.Println(os.Getenv("HELLO"))
|
||||
// Output: "universe"
|
||||
```
|
||||
|
||||
### Throw a Panic
|
||||
|
||||
Both `gotenv.Load` and `gotenv.OverLoad` returns an error on something wrong occurred, like your env file is not exist, and so on. To make it easier to use, `gotenv` also provides `gotenv.Must` helper, to let it panic when an error returned.
|
||||
|
||||
```go
|
||||
err := gotenv.Load(".env-is-not-exist")
|
||||
fmt.Println("error", err)
|
||||
// error: open .env-is-not-exist: no such file or directory
|
||||
|
||||
gotenv.Must(gotenv.Load, ".env-is-not-exist")
|
||||
// it will throw a panic
|
||||
// panic: open .env-is-not-exist: no such file or directory
|
||||
```
|
||||
|
||||
### Another Scenario
|
||||
|
||||
Just in case you want to parse environment variables from any `io.Reader`, gotenv keeps its `Parse` and `StrictParse` function as public API so you can use that.
|
||||
|
||||
```go
|
||||
// import "strings"
|
||||
|
||||
pairs := gotenv.Parse(strings.NewReader("FOO=test\nBAR=$FOO"))
|
||||
// gotenv.Env{"FOO": "test", "BAR": "test"}
|
||||
|
||||
err, pairs = gotenv.StrictParse(strings.NewReader(`FOO="bar"`))
|
||||
// gotenv.Env{"FOO": "bar"}
|
||||
```
|
||||
|
||||
`Parse` ignores invalid lines and returns `Env` of valid environment variables, while `StrictParse` returns an error for invalid lines.
|
||||
|
||||
## Notes
|
||||
|
||||
The gotenv package is a Go port of [`dotenv`](https://github.com/bkeepers/dotenv) project with some additions made for Go. For general features, it aims to be compatible as close as possible.
|
9
vendor/github.com/subosito/gotenv/appveyor.yml
generated
vendored
Normal file
9
vendor/github.com/subosito/gotenv/appveyor.yml
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
build: off
|
||||
clone_folder: c:\gopath\src\github.com\subosito\gotenv
|
||||
environment:
|
||||
GOPATH: c:\gopath
|
||||
stack: go 1.10
|
||||
before_test:
|
||||
- go get -t
|
||||
test_script:
|
||||
- go test -v -cover -race
|
265
vendor/github.com/subosito/gotenv/gotenv.go
generated
vendored
Normal file
265
vendor/github.com/subosito/gotenv/gotenv.go
generated
vendored
Normal file
@@ -0,0 +1,265 @@
|
||||
// Package gotenv provides functionality to dynamically load the environment variables
|
||||
package gotenv
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Pattern for detecting valid line format
|
||||
linePattern = `\A\s*(?:export\s+)?([\w\.]+)(?:\s*=\s*|:\s+?)('(?:\'|[^'])*'|"(?:\"|[^"])*"|[^#\n]+)?\s*(?:\s*\#.*)?\z`
|
||||
|
||||
// Pattern for detecting valid variable within a value
|
||||
variablePattern = `(\\)?(\$)(\{?([A-Z0-9_]+)?\}?)`
|
||||
)
|
||||
|
||||
// Env holds key/value pair of valid environment variable
|
||||
type Env map[string]string
|
||||
|
||||
/*
|
||||
Load is a function to load a file or multiple files and then export the valid variables into environment variables if they do not exist.
|
||||
When it's called with no argument, it will load `.env` file on the current path and set the environment variables.
|
||||
Otherwise, it will loop over the filenames parameter and set the proper environment variables.
|
||||
*/
|
||||
func Load(filenames ...string) error {
|
||||
return loadenv(false, filenames...)
|
||||
}
|
||||
|
||||
/*
|
||||
OverLoad is a function to load a file or multiple files and then export and override the valid variables into environment variables.
|
||||
*/
|
||||
func OverLoad(filenames ...string) error {
|
||||
return loadenv(true, filenames...)
|
||||
}
|
||||
|
||||
/*
|
||||
Must is wrapper function that will panic when supplied function returns an error.
|
||||
*/
|
||||
func Must(fn func(filenames ...string) error, filenames ...string) {
|
||||
if err := fn(filenames...); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Apply is a function to load an io Reader then export the valid variables into environment variables if they do not exist.
|
||||
*/
|
||||
func Apply(r io.Reader) error {
|
||||
return parset(r, false)
|
||||
}
|
||||
|
||||
/*
|
||||
OverApply is a function to load an io Reader then export and override the valid variables into environment variables.
|
||||
*/
|
||||
func OverApply(r io.Reader) error {
|
||||
return parset(r, true)
|
||||
}
|
||||
|
||||
func loadenv(override bool, filenames ...string) error {
|
||||
if len(filenames) == 0 {
|
||||
filenames = []string{".env"}
|
||||
}
|
||||
|
||||
for _, filename := range filenames {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = parset(f, override)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parse and set :)
|
||||
func parset(r io.Reader, override bool) error {
|
||||
env, err := StrictParse(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for key, val := range env {
|
||||
setenv(key, val, override)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setenv(key, val string, override bool) {
|
||||
if override {
|
||||
os.Setenv(key, val)
|
||||
} else {
|
||||
if _, present := os.LookupEnv(key); !present {
|
||||
os.Setenv(key, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse is a function to parse line by line any io.Reader supplied and returns the valid Env key/value pair of valid variables.
|
||||
// It expands the value of a variable from the environment variable but does not set the value to the environment itself.
|
||||
// This function is skipping any invalid lines and only processing the valid one.
|
||||
func Parse(r io.Reader) Env {
|
||||
env, _ := StrictParse(r)
|
||||
return env
|
||||
}
|
||||
|
||||
// StrictParse is a function to parse line by line any io.Reader supplied and returns the valid Env key/value pair of valid variables.
|
||||
// It expands the value of a variable from the environment variable but does not set the value to the environment itself.
|
||||
// This function is returning an error if there are any invalid lines.
|
||||
func StrictParse(r io.Reader) (Env, error) {
|
||||
env := make(Env)
|
||||
scanner := bufio.NewScanner(r)
|
||||
|
||||
i := 1
|
||||
bom := string([]byte{239, 187, 191})
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
if i == 1 {
|
||||
line = strings.TrimPrefix(line, bom)
|
||||
}
|
||||
|
||||
i++
|
||||
|
||||
err := parseLine(line, env)
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
func parseLine(s string, env Env) error {
|
||||
rl := regexp.MustCompile(linePattern)
|
||||
rm := rl.FindStringSubmatch(s)
|
||||
|
||||
if len(rm) == 0 {
|
||||
return checkFormat(s, env)
|
||||
}
|
||||
|
||||
key := rm[1]
|
||||
val := rm[2]
|
||||
|
||||
// determine if string has quote prefix
|
||||
hdq := strings.HasPrefix(val, `"`)
|
||||
|
||||
// determine if string has single quote prefix
|
||||
hsq := strings.HasPrefix(val, `'`)
|
||||
|
||||
// trim whitespace
|
||||
val = strings.Trim(val, " ")
|
||||
|
||||
// remove quotes '' or ""
|
||||
rq := regexp.MustCompile(`\A(['"])(.*)(['"])\z`)
|
||||
val = rq.ReplaceAllString(val, "$2")
|
||||
|
||||
if hdq {
|
||||
val = strings.Replace(val, `\n`, "\n", -1)
|
||||
val = strings.Replace(val, `\r`, "\r", -1)
|
||||
|
||||
// Unescape all characters except $ so variables can be escaped properly
|
||||
re := regexp.MustCompile(`\\([^$])`)
|
||||
val = re.ReplaceAllString(val, "$1")
|
||||
}
|
||||
|
||||
rv := regexp.MustCompile(variablePattern)
|
||||
fv := func(s string) string {
|
||||
return varReplacement(s, hsq, env)
|
||||
}
|
||||
|
||||
val = rv.ReplaceAllStringFunc(val, fv)
|
||||
val = parseVal(val, env)
|
||||
|
||||
env[key] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseExport(st string, env Env) error {
|
||||
if strings.HasPrefix(st, "export") {
|
||||
vs := strings.SplitN(st, " ", 2)
|
||||
|
||||
if len(vs) > 1 {
|
||||
if _, ok := env[vs[1]]; !ok {
|
||||
return fmt.Errorf("line `%s` has an unset variable", st)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func varReplacement(s string, hsq bool, env Env) string {
|
||||
if strings.HasPrefix(s, "\\") {
|
||||
return strings.TrimPrefix(s, "\\")
|
||||
}
|
||||
|
||||
if hsq {
|
||||
return s
|
||||
}
|
||||
|
||||
sn := `(\$)(\{?([A-Z0-9_]+)\}?)`
|
||||
rn := regexp.MustCompile(sn)
|
||||
mn := rn.FindStringSubmatch(s)
|
||||
|
||||
if len(mn) == 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
v := mn[3]
|
||||
|
||||
replace, ok := env[v]
|
||||
if !ok {
|
||||
replace = os.Getenv(v)
|
||||
}
|
||||
|
||||
return replace
|
||||
}
|
||||
|
||||
func checkFormat(s string, env Env) error {
|
||||
st := strings.TrimSpace(s)
|
||||
|
||||
if (st == "") || strings.HasPrefix(st, "#") {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := parseExport(st, env); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("line `%s` doesn't match format", s)
|
||||
}
|
||||
|
||||
func parseVal(val string, env Env) string {
|
||||
if strings.Contains(val, "=") {
|
||||
if !(val == "\n" || val == "\r") {
|
||||
kv := strings.Split(val, "\n")
|
||||
|
||||
if len(kv) == 1 {
|
||||
kv = strings.Split(val, "\r")
|
||||
}
|
||||
|
||||
if len(kv) > 1 {
|
||||
val = kv[0]
|
||||
|
||||
for i := 1; i < len(kv); i++ {
|
||||
parseLine(kv[i], env)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
Reference in New Issue
Block a user