forked from beego/beego
-
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.
Merge pull request beego#2485 from astaxie/develop
beego 1.8.0
- Loading branch information
Showing
50 changed files
with
3,339 additions
and
303 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
.vscode | ||
.DS_Store | ||
*.swp | ||
*.swo | ||
|
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
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
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
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
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
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,85 @@ | ||
// Copyright 2014 beego Author. All Rights Reserved. | ||
// Copyright 2017 Faissal Elamraoui. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package env | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/astaxie/beego/utils" | ||
) | ||
|
||
var env *utils.BeeMap | ||
|
||
func init() { | ||
env = utils.NewBeeMap() | ||
for _, e := range os.Environ() { | ||
splits := strings.Split(e, "=") | ||
env.Set(splits[0], os.Getenv(splits[0])) | ||
} | ||
} | ||
|
||
// Get returns a value by key. | ||
// If the key does not exist, the default value will be returned. | ||
func Get(key string, defVal string) string { | ||
if val := env.Get(key); val != nil { | ||
return val.(string) | ||
} | ||
return defVal | ||
} | ||
|
||
// MustGet returns a value by key. | ||
// If the key does not exist, it will return an error. | ||
func MustGet(key string) (string, error) { | ||
if val := env.Get(key); val != nil { | ||
return val.(string), nil | ||
} | ||
return "", fmt.Errorf("no env variable with %s", key) | ||
} | ||
|
||
// Set sets a value in the ENV copy. | ||
// This does not affect the child process environment. | ||
func Set(key string, value string) { | ||
env.Set(key, value) | ||
} | ||
|
||
// MustSet sets a value in the ENV copy and the child process environment. | ||
// It returns an error in case the set operation failed. | ||
func MustSet(key string, value string) error { | ||
err := os.Setenv(key, value) | ||
if err != nil { | ||
return err | ||
} | ||
env.Set(key, value) | ||
return nil | ||
} | ||
|
||
// GetAll returns all keys/values in the current child process environment. | ||
func GetAll() map[string]string { | ||
items := env.Items() | ||
envs := make(map[string]string, env.Count()) | ||
|
||
for key, val := range items { | ||
switch key := key.(type) { | ||
case string: | ||
switch val := val.(type) { | ||
case string: | ||
envs[key] = val | ||
} | ||
} | ||
} | ||
return envs | ||
} |
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,75 @@ | ||
// Copyright 2014 beego Author. All Rights Reserved. | ||
// Copyright 2017 Faissal Elamraoui. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package env | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestEnvGet(t *testing.T) { | ||
gopath := Get("GOPATH", "") | ||
if gopath != os.Getenv("GOPATH") { | ||
t.Error("expected GOPATH not empty.") | ||
} | ||
|
||
noExistVar := Get("NOEXISTVAR", "foo") | ||
if noExistVar != "foo" { | ||
t.Errorf("expected NOEXISTVAR to equal foo, got %s.", noExistVar) | ||
} | ||
} | ||
|
||
func TestEnvMustGet(t *testing.T) { | ||
gopath, err := MustGet("GOPATH") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if gopath != os.Getenv("GOPATH") { | ||
t.Errorf("expected GOPATH to be the same, got %s.", gopath) | ||
} | ||
|
||
_, err = MustGet("NOEXISTVAR") | ||
if err == nil { | ||
t.Error("expected error to be non-nil") | ||
} | ||
} | ||
|
||
func TestEnvSet(t *testing.T) { | ||
Set("MYVAR", "foo") | ||
myVar := Get("MYVAR", "bar") | ||
if myVar != "foo" { | ||
t.Errorf("expected MYVAR to equal foo, got %s.", myVar) | ||
} | ||
} | ||
|
||
func TestEnvMustSet(t *testing.T) { | ||
err := MustSet("FOO", "bar") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
fooVar := os.Getenv("FOO") | ||
if fooVar != "bar" { | ||
t.Errorf("expected FOO variable to equal bar, got %s.", fooVar) | ||
} | ||
} | ||
|
||
func TestEnvGetAll(t *testing.T) { | ||
envMap := GetAll() | ||
if len(envMap) == 0 { | ||
t.Error("expected environment not empty.") | ||
} | ||
} |
Oops, something went wrong.