refactor to go-restful
This commit is contained in:
cmd/dkl-local-server
go.modgo.sumpkg/apiutils
vendor
github.com
PuerkitoBio
emicklei
go-restful-openapi
.gitignore.travis.ymlCHANGES.mdLICENSEREADME.mdbuild_definitions.gobuild_path.goconfig.godefinition_builder.golookup.goproperty_ext.gospec_resource.go
go-restful
.gitignore.travis.ymlCHANGES.mdLICENSEMakefileREADME.mdSrcfilebench_test.shcompress.gocompressor_cache.gocompressor_pools.gocompressors.goconstants.gocontainer.gocors_filter.gocoverage.shcurly.gocurly_route.godoc.goentity_accessors.gofilter.gojson.gojsoniter.gojsr311.go
log
logger.gomime.gooptions_filter.goparameter.gopath_expression.gopath_processor.gorequest.goresponse.goroute.goroute_builder.gorouter.goservice_error.goweb_service.goweb_service_container.gogo-openapi
jsonpointer
jsonreference
spec
.editorconfig.gitignore.golangci.yml.travis.ymlCODE_OF_CONDUCT.mdLICENSEREADME.mdbindata.gocontact_info.godebug.goexpander.goexternal_docs.gogo.modgo.sumheader.goinfo.goitems.golicense.gooperation.goparameter.gopath_item.gopaths.goref.goresponse.goresponses.goschema.gosecurity_scheme.gospec.goswagger.gotag.goxml_object.go
swag
gobuffalo
envy
.env.gitignore.gometalinter.json.travis.ymlLICENSE.txtMakefileREADME.mdenvy.gogo.modgo.sumshoulders.mdversion.go
packd
.gitignore.gometalinter.json.travis.ymlLICENSEMakefileREADME.mdfile.gofile_info.gogo.modgo.suminterfaces.gomemory_box.goskip_walker.goversion.go
packr
.codeclimate.yml.gitignore.gometalinter.json.goreleaser.yml.goreleaser.yml.plush.travis.ymlLICENSE.txtMakefileREADME.mdSHOULDERS.mdbox.goenv.gofile.gogo.modgo.sumpackr.gotravis.shversion.gowalk.go
syncx
joho
json-iterator
go
.codecov.yml.gitignore.travis.ymlGopkg.lockGopkg.tomlLICENSEREADME.mdadapter.goany.goany_array.goany_bool.goany_float.goany_int32.goany_int64.goany_invalid.goany_nil.goany_number.goany_object.goany_str.goany_uint32.goany_uint64.gobuild.shconfig.gofuzzy_mode_convert_table.mditer.goiter_array.goiter_float.goiter_int.goiter_object.goiter_skip.goiter_skip_sloppy.goiter_skip_strict.goiter_str.gojsoniter.gopool.goreflect.goreflect_array.goreflect_dynamic.goreflect_extension.goreflect_json_number.goreflect_json_raw_message.goreflect_map.goreflect_marshaler.goreflect_native.goreflect_optional.goreflect_slice.goreflect_struct_decoder.goreflect_struct_encoder.gostream.gostream_float.gostream_int.gostream_str.gotest.sh
mailru
easyjson
mcluseau
modern-go
concurrent
.gitignore.travis.ymlLICENSEREADME.mdexecutor.gogo_above_19.gogo_below_19.golog.gotest.shunbounded_executor.go
reflect2
.gitignore.travis.ymlGopkg.lockGopkg.tomlLICENSEREADME.mdgo_above_17.gogo_above_19.gogo_below_17.gogo_below_19.goreflect2.goreflect2_amd64.sreflect2_kind.gorelfect2_386.srelfect2_amd64p32.srelfect2_arm.srelfect2_arm64.srelfect2_mips64x.srelfect2_mipsx.srelfect2_ppc64x.srelfect2_s390x.ssafe_field.gosafe_map.gosafe_slice.gosafe_struct.gosafe_type.gotest.shtype_map.gounsafe_array.gounsafe_eface.gounsafe_field.gounsafe_iface.gounsafe_link.gounsafe_map.gounsafe_ptr.gounsafe_slice.gounsafe_struct.gounsafe_type.go
oklog
ulid
pkg
rogpeppe
golang.org
x
crypto
net
text
AUTHORSCONTRIBUTORSLICENSEPATENTS
secure
transform
unicode
bidi
bidi.gobracket.gocore.gogen.gogen_ranges.gogen_trieval.goprop.gotables10.0.0.gotables9.0.0.gotrieval.go
norm
width
258
vendor/github.com/gobuffalo/envy/envy.go
generated
vendored
Normal file
258
vendor/github.com/gobuffalo/envy/envy.go
generated
vendored
Normal file
@ -0,0 +1,258 @@
|
||||
/*
|
||||
package envy makes working with ENV variables in Go trivial.
|
||||
|
||||
* Get ENV variables with default values.
|
||||
* Set ENV variables safely without affecting the underlying system.
|
||||
* Temporarily change ENV vars; useful for testing.
|
||||
* Map all of the key/values in the ENV.
|
||||
* Loads .env files (by using [godotenv](https://github.com/joho/godotenv/))
|
||||
* More!
|
||||
*/
|
||||
package envy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/rogpeppe/go-internal/modfile"
|
||||
)
|
||||
|
||||
var gil = &sync.RWMutex{}
|
||||
var env = map[string]string{}
|
||||
|
||||
// GO111MODULE is ENV for turning mods on/off
|
||||
const GO111MODULE = "GO111MODULE"
|
||||
|
||||
func init() {
|
||||
Load()
|
||||
loadEnv()
|
||||
}
|
||||
|
||||
// Load the ENV variables to the env map
|
||||
func loadEnv() {
|
||||
gil.Lock()
|
||||
defer gil.Unlock()
|
||||
|
||||
if os.Getenv("GO_ENV") == "" {
|
||||
// if the flag "test.v" is *defined*, we're running as a unit test. Note that we don't care
|
||||
// about v.Value (verbose test mode); we just want to know if the test environment has defined
|
||||
// it. It's also possible that the flags are not yet fully parsed (i.e. flag.Parsed() == false),
|
||||
// so we could not depend on v.Value anyway.
|
||||
//
|
||||
if v := flag.Lookup("test.v"); v != nil {
|
||||
env["GO_ENV"] = "test"
|
||||
}
|
||||
}
|
||||
|
||||
// set the GOPATH if using >= 1.8 and the GOPATH isn't set
|
||||
if os.Getenv("GOPATH") == "" {
|
||||
out, err := exec.Command("go", "env", "GOPATH").Output()
|
||||
if err == nil {
|
||||
gp := strings.TrimSpace(string(out))
|
||||
os.Setenv("GOPATH", gp)
|
||||
}
|
||||
}
|
||||
|
||||
for _, e := range os.Environ() {
|
||||
pair := strings.Split(e, "=")
|
||||
env[pair[0]] = os.Getenv(pair[0])
|
||||
}
|
||||
}
|
||||
|
||||
func Mods() bool {
|
||||
return Get(GO111MODULE, "off") == "on"
|
||||
}
|
||||
|
||||
// Reload the ENV variables. Useful if
|
||||
// an external ENV manager has been used
|
||||
func Reload() {
|
||||
env = map[string]string{}
|
||||
loadEnv()
|
||||
}
|
||||
|
||||
// Load .env files. Files will be loaded in the same order that are received.
|
||||
// Redefined vars will override previously existing values.
|
||||
// IE: envy.Load(".env", "test_env/.env") will result in DIR=test_env
|
||||
// If no arg passed, it will try to load a .env file.
|
||||
func Load(files ...string) error {
|
||||
|
||||
// If no files received, load the default one
|
||||
if len(files) == 0 {
|
||||
err := godotenv.Overload()
|
||||
if err == nil {
|
||||
Reload()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// We received a list of files
|
||||
for _, file := range files {
|
||||
|
||||
// Check if it exists or we can access
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
// It does not exist or we can not access.
|
||||
// Return and stop loading
|
||||
return err
|
||||
}
|
||||
|
||||
// It exists and we have permission. Load it
|
||||
if err := godotenv.Overload(file); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload the env so all new changes are noticed
|
||||
Reload()
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get a value from the ENV. If it doesn't exist the
|
||||
// default value will be returned.
|
||||
func Get(key string, value string) string {
|
||||
gil.RLock()
|
||||
defer gil.RUnlock()
|
||||
if v, ok := env[key]; ok {
|
||||
return v
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// Get a value from the ENV. If it doesn't exist
|
||||
// an error will be returned
|
||||
func MustGet(key string) (string, error) {
|
||||
gil.RLock()
|
||||
defer gil.RUnlock()
|
||||
if v, ok := env[key]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return "", fmt.Errorf("could not find ENV var with %s", key)
|
||||
}
|
||||
|
||||
// Set a value into the ENV. This is NOT permanent. It will
|
||||
// only affect values accessed through envy.
|
||||
func Set(key string, value string) {
|
||||
gil.Lock()
|
||||
defer gil.Unlock()
|
||||
env[key] = value
|
||||
}
|
||||
|
||||
// MustSet the value into the underlying ENV, as well as envy.
|
||||
// This may return an error if there is a problem setting the
|
||||
// underlying ENV value.
|
||||
func MustSet(key string, value string) error {
|
||||
gil.Lock()
|
||||
defer gil.Unlock()
|
||||
err := os.Setenv(key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
env[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// Map all of the keys/values set in envy.
|
||||
func Map() map[string]string {
|
||||
gil.RLock()
|
||||
defer gil.RUnlock()
|
||||
cp := map[string]string{}
|
||||
for k, v := range env {
|
||||
cp[k] = v
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
// Temp makes a copy of the values and allows operation on
|
||||
// those values temporarily during the run of the function.
|
||||
// At the end of the function run the copy is discarded and
|
||||
// the original values are replaced. This is useful for testing.
|
||||
// Warning: This function is NOT safe to use from a goroutine or
|
||||
// from code which may access any Get or Set function from a goroutine
|
||||
func Temp(f func()) {
|
||||
oenv := env
|
||||
env = map[string]string{}
|
||||
for k, v := range oenv {
|
||||
env[k] = v
|
||||
}
|
||||
defer func() { env = oenv }()
|
||||
f()
|
||||
}
|
||||
|
||||
func GoPath() string {
|
||||
return Get("GOPATH", "")
|
||||
}
|
||||
|
||||
func GoBin() string {
|
||||
return Get("GO_BIN", "go")
|
||||
}
|
||||
|
||||
// GoPaths returns all possible GOPATHS that are set.
|
||||
func GoPaths() []string {
|
||||
gp := Get("GOPATH", "")
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.Split(gp, ";") // Windows uses a different separator
|
||||
}
|
||||
return strings.Split(gp, ":")
|
||||
}
|
||||
|
||||
func importPath(path string) string {
|
||||
path = strings.TrimPrefix(path, "/private")
|
||||
for _, gopath := range GoPaths() {
|
||||
srcpath := filepath.Join(gopath, "src")
|
||||
rel, err := filepath.Rel(srcpath, path)
|
||||
if err == nil {
|
||||
return filepath.ToSlash(rel)
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to trim
|
||||
rel := strings.TrimPrefix(path, filepath.Join(GoPath(), "src"))
|
||||
rel = strings.TrimPrefix(rel, string(filepath.Separator))
|
||||
return filepath.ToSlash(rel)
|
||||
}
|
||||
|
||||
// CurrentModule will attempt to return the module name from `go.mod` if
|
||||
// modules are enabled.
|
||||
// If modules are not enabled it will fallback to using CurrentPackage instead.
|
||||
func CurrentModule() (string, error) {
|
||||
if !Mods() {
|
||||
return CurrentPackage(), nil
|
||||
}
|
||||
moddata, err := ioutil.ReadFile("go.mod")
|
||||
if err != nil {
|
||||
return "", errors.New("go.mod cannot be read or does not exist while go module is enabled")
|
||||
}
|
||||
packagePath := modfile.ModulePath(moddata)
|
||||
if packagePath == "" {
|
||||
return "", errors.New("go.mod is malformed")
|
||||
}
|
||||
return packagePath, nil
|
||||
}
|
||||
|
||||
// CurrentPackage attempts to figure out the current package name from the PWD
|
||||
// Use CurrentModule for a more accurate package name.
|
||||
func CurrentPackage() string {
|
||||
if Mods() {
|
||||
}
|
||||
pwd, _ := os.Getwd()
|
||||
return importPath(pwd)
|
||||
}
|
||||
|
||||
func Environ() []string {
|
||||
gil.RLock()
|
||||
defer gil.RUnlock()
|
||||
var e []string
|
||||
for k, v := range env {
|
||||
e = append(e, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
return e
|
||||
}
|
Reference in New Issue
Block a user