本文整理汇总了Golang中syscall.Getenv函数的典型用法代码示例。如果您正苦于以下问题:Golang Getenv函数的具体用法?Golang Getenv怎么用?Golang Getenv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getenv函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: safeName
func safeName(name string) string {
travis, haveTravis := syscall.Getenv("TRAVIS")
buildId, haveBuildId := syscall.Getenv("TRAVIS_JOB_NUMBER")
if !haveTravis || !haveBuildId || travis != "true" {
return name
}
return name + "_travis-" + buildId
}
示例2: main
func main() {
es_access_key, ok := syscall.Getenv("ES_ACCESS_KEY")
if ok != true {
fmt.Printf("You must set the environment variable: ES_ACCESS_KEY\n")
os.Exit(1)
}
es_secret_key, ok := syscall.Getenv("ES_SECRET_KEY")
if ok != true {
fmt.Printf("You must set the environment variable: ES_SECRET_KEY\n")
os.Exit(1)
}
url := "https://api.enstratus.com/api/enstratus/2012-06-15/geography/Cloud"
request, _ := http.NewRequest("GET", url, nil)
request.Header.Add("accept", "application/json")
request.Header.Add("x-esauth-access", es_access_key)
request.Header.Add("x-esauth-timestamp", string(GetTimeString()))
request.Header.Add("x-es-details", "basic")
request.Header.Add("user-agent", ES_UA)
sig := SignRequest(es_access_key, es_secret_key,
ES_UA, "GET",
"geography/Cloud")
request.Header.Add("x-esauth-signature", sig)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
switch {
case response.StatusCode >= 400 && response.StatusCode <= 599:
e := &EnstratusError{}
err := json.Unmarshal([]byte(contents), &e)
if err != nil {
fmt.Printf("JSON Decoding error: %s\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", e.Error.Message)
os.Exit(1)
}
clouds := &CloudList{}
jsonerr := json.Unmarshal([]byte(contents), &clouds)
if jsonerr != nil {
fmt.Printf("JSON Decoding error: %s\n", jsonerr)
os.Exit(1)
}
spew.Dump(clouds)
}
}
示例3: getEnv
func getEnv(e1, e2, d string) string {
if r, found := syscall.Getenv(e1); found {
return r
}
if r, found := syscall.Getenv(e2); found {
return r
}
return d
}
示例4: initLocal
func initLocal() {
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
// $TZ="" means use UTC.
// $TZ="foo" means use /usr/share/zoneinfo/foo.
tz, ok := syscall.Getenv("TZ")
switch {
case !ok:
z, err := loadZoneFile("", "/etc/localtime")
if err == nil {
localLoc = *z
localLoc.name = "Local"
return
}
case tz != "" && tz != "UTC":
if z, err := loadLocation(tz); err == nil {
localLoc = *z
return
}
}
// Fall back to UTC.
localLoc.name = "UTC"
}
示例5: ServerMode
// ServerMode returns the Gin server mode as per environment or default.
func ServerMode() string {
mode, ok := syscall.Getenv("SERVER_MODE")
if !ok {
mode = DefaultServerMode
}
return mode
}
示例6: ApplyWithError
// ApplyWithError populates the flag given the flag set and environment
func (f BoolTFlag) ApplyWithError(set *flag.FlagSet) error {
val := true
if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") {
envVar = strings.TrimSpace(envVar)
if envVal, ok := syscall.Getenv(envVar); ok {
if envVal == "" {
val = false
break
}
envValBool, err := strconv.ParseBool(envVal)
if err != nil {
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err)
}
val = envValBool
break
}
}
}
eachName(f.Name, func(name string) {
if f.Destination != nil {
set.BoolVar(f.Destination, name, val, f.Usage)
return
}
set.Bool(name, val, f.Usage)
})
return nil
}
示例7: main
func main() {
flag.Parse()
dir := flag.Arg(0) // new directory x | xx | xxx
pwd, _ := syscall.Getenv("PWD") // ~/aaa/bbbb/xxx/yyy/zzzzz
tb := str.Split(pwd, "/")
newPath := ""
isPresent := false
for i := 0; i < len(tb); i++ {
newPath += tb[i] + "/"
if str.HasPrefix(tb[i], dir) {
isPresent = true
break
}
}
if !isPresent {
err := "Directory " + dir + " non trouvé dans path " + pwd
fmt.Println(err)
os.Exit(16) // echo $?
}
fmt.Println("cd " + newPath) // ~/aaa/bbbb/xxx/
}
示例8: TestFixedGOROOT
func TestFixedGOROOT(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skipf("skipping plan9, it is inconsistent by allowing GOROOT to be updated by Setenv")
}
// Restore both the real GOROOT environment variable, and runtime's copies:
if orig, ok := syscall.Getenv("GOROOT"); ok {
defer syscall.Setenv("GOROOT", orig)
} else {
defer syscall.Unsetenv("GOROOT")
}
envs := runtime.Envs()
oldenvs := append([]string{}, envs...)
defer runtime.SetEnvs(oldenvs)
// attempt to reuse existing envs backing array.
want := runtime.GOROOT()
runtime.SetEnvs(append(envs[:0], "GOROOT="+want))
if got := runtime.GOROOT(); got != want {
t.Errorf(`initial runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Setenv("GOROOT", "/os"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after setenv runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Unsetenv("GOROOT"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after unsetenv runtime.GOROOT()=%q, want %q`, got, want)
}
}
示例9: ServerConfig
// ServerConfig returns the Gin server config as per environment or default.
func ServerConfig() string {
addr, _ := syscall.Getenv("SERVER_ADDR")
var port string
if portStr, ok := syscall.Getenv("SERVER_PORT"); !ok {
port = DefaultPort
} else {
portUInt, err := strconv.ParseUint(portStr, 10, 16)
if err != nil || portUInt > 65535 {
log.Notice("Invalid port \"%s\", using %s instead.", portStr, DefaultPort)
port = DefaultPort
} else {
port = portStr
}
}
return fmt.Sprintf("%s:%s", addr, port)
}
示例10: init
func init() {
switch path, _ := syscall.Getenv("GOCOVOUT"); path {
case "":
// No tracing
case "-":
Default.Tracer = fdwriter(syscall.Stdout)
default:
// Add the process ID to the filename.
// TODO handle PID reuse by checking if file exists.
path += "." + itoa(syscall.Getpid())
mode := syscall.O_WRONLY | syscall.O_CREAT | syscall.O_TRUNC
fd, err := syscall.Open(path, mode, 0666)
if err != nil {
msg := "gocov: failed to create log file: "
msg += err.Error() + "\n"
write(fdwriter(syscall.Stderr), []byte(msg))
syscall.Exit(1)
}
Default.Tracer = fdwriter(int(fd))
}
// Remove GOCOVOUT from environ, to prevent noise from child processes.
// TODO Don't do this; append .pid to output filename.
syscall.Setenv("GOCOVOUT", "")
}
示例11: init
func init() {
log.EnableDebugLogging()
flag.StringVar(&add, "add", "", "Server to add to the connection manager")
flag.StringVar(&connect, "connect", "", "Stored server to connect to")
flag.StringVar(&domain, "domain", "", "AD domain")
flag.StringVar(&ip, "ip", "", "Server ip address")
flag.BoolVar(&list, "list", false, "List the stored servers")
flag.BoolVar(&nla, "nla", true, "Use network level authentication (yes/no)")
flag.BoolVar(&debug, "debug", false, "Enable debugging")
flag.StringVar(&remove, "remove", "", "Server to remove from the connection manager")
flag.StringVar(&username, "username", "", "AD username")
flag.StringVar(&app, "app", "", "Remote Application")
homePath, _ := syscall.Getenv("HOME")
configFile = path.Join(homePath, ".xfcm")
// check if the configuration file exists
filePathExists, err := pathExists(configFile, true)
// print if we encountered an error checking if the path existed
if err != nil {
log.Error(err)
}
// if the path didn't exist, create the file
if !filePathExists {
createConfigFile(configFile)
}
}
示例12: applyWithError
// applyWithError populates the flag given the flag set and environment
func (f UintFlag) applyWithError(set *flag.FlagSet) error {
if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") {
envVar = strings.TrimSpace(envVar)
if envVal, ok := syscall.Getenv(envVar); ok {
envValInt, err := strconv.ParseUint(envVal, 0, 64)
if err != nil {
return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err)
}
f.Value = uint(envValInt)
break
}
}
}
eachName(f.Name, func(name string) {
if f.Destination != nil {
set.UintVar(f.Destination, name, f.Value, f.Usage)
return
}
set.Uint(name, f.Value, f.Usage)
})
return nil
}
示例13: initLocal
func initLocal() { // 初始化location
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
// $TZ="" means use UTC.
// $TZ="foo" means use /usr/share/zoneinfo/foo.
tz, ok := syscall.Getenv("TZ") // 查询TZ变量
switch {
case !ok: // 如果不存在TZ变量
z, err := loadZoneFile("", "/etc/localtime") // 从etc载入localtime文件
if err == nil { // 如果没有错误
localLoc = *z // 设置localLoc
localLoc.name = "Local" // 设置location名
return
}
case tz != "" && tz != "UTC": // 存在tz变量,并且不等于空和UTC
if z, err := loadLocation(tz); err == nil {
localLoc = *z
return
}
}
// Fall back to UTC.
localLoc.name = "UTC" // 设置location为UTC
}
示例14: loadString
func loadString(name string, out *reflect.Value) {
data, found := syscall.Getenv(name)
if !found {
return
}
out.SetString(data)
}
示例15: initTest
func initTest(t *testing.T) (*Client, *Index) {
appID, haveAppID := syscall.Getenv("ALGOLIA_APPLICATION_ID")
apiKey, haveApiKey := syscall.Getenv("ALGOLIA_API_KEY")
if !haveApiKey || !haveAppID {
t.Fatalf("Need ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY")
}
client := NewClient(appID, apiKey)
client.SetTimeout(1000, 10000)
hosts := make([]string, 3)
hosts[0] = appID + "-1.algolia.net"
hosts[1] = appID + "-2.algolia.net"
hosts[2] = appID + "-3.algolia.net"
client = NewClientWithHosts(appID, apiKey, hosts)
index := client.InitIndex(safeName("àlgol?à-go"))
return client, index
}