-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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 #498 from bilibili/kratos/upgrade
upgrade kratos
- Loading branch information
Showing
11 changed files
with
106 additions
and
617 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
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,78 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
var envCache struct { | ||
once sync.Once | ||
m map[string]string | ||
} | ||
|
||
// EnvFile returns the name of the Go environment configuration file. | ||
func EnvFile() (string, error) { | ||
if file := os.Getenv("GOENV"); file != "" { | ||
if file == "off" { | ||
return "", fmt.Errorf("GOENV=off") | ||
} | ||
return file, nil | ||
} | ||
dir, err := os.UserConfigDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
if dir == "" { | ||
return "", fmt.Errorf("missing user-config dir") | ||
} | ||
return filepath.Join(dir, "go/env"), nil | ||
} | ||
|
||
func initEnvCache() { | ||
envCache.m = make(map[string]string) | ||
file, _ := EnvFile() | ||
if file == "" { | ||
return | ||
} | ||
data, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for len(data) > 0 { | ||
// Get next line. | ||
line := data | ||
i := bytes.IndexByte(data, '\n') | ||
if i >= 0 { | ||
line, data = line[:i], data[i+1:] | ||
} else { | ||
data = nil | ||
} | ||
|
||
i = bytes.IndexByte(line, '=') | ||
if i < 0 || line[0] < 'A' || 'Z' < line[0] { | ||
// Line is missing = (or empty) or a comment or not a valid env name. Ignore. | ||
// (This should not happen, since the file should be maintained almost | ||
// exclusively by "go env -w", but better to silently ignore than to make | ||
// the go command unusable just because somehow the env file has | ||
// gotten corrupted.) | ||
continue | ||
} | ||
key, val := line[:i], line[i+1:] | ||
envCache.m[string(key)] = string(val) | ||
} | ||
} | ||
|
||
// Getenv gets the value from env or configuration. | ||
func Getenv(key string) string { | ||
val := os.Getenv(key) | ||
if val != "" { | ||
return val | ||
} | ||
envCache.once.Do(initEnvCache) | ||
return envCache.m[key] | ||
} |
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
Oops, something went wrong.