2021-01-17 21:46:25 +08:00
|
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// Package genv provides operations for environment variables of system.
|
2017-12-18 10:42:59 +08:00
|
|
|
|
package genv
|
|
|
|
|
|
2020-04-02 20:52:37 +08:00
|
|
|
|
import (
|
2021-11-15 20:26:31 +08:00
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
2021-12-22 22:22:42 +08:00
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-12-07 19:08:52 +08:00
|
|
|
|
"github.com/gogf/gf/v2/internal/command"
|
2021-10-21 18:22:47 +08:00
|
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2020-04-02 20:52:37 +08:00
|
|
|
|
)
|
2017-12-18 10:42:59 +08:00
|
|
|
|
|
2019-06-11 20:57:43 +08:00
|
|
|
|
// All returns a copy of strings representing the environment,
|
|
|
|
|
// in the form "key=value".
|
2017-12-18 10:42:59 +08:00
|
|
|
|
func All() []string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return os.Environ()
|
2017-12-18 10:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
|
// Map returns a copy of strings representing the environment as a map.
|
|
|
|
|
func Map() map[string]string {
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
|
i := 0
|
|
|
|
|
for _, s := range os.Environ() {
|
|
|
|
|
i = strings.IndexByte(s, '=')
|
|
|
|
|
m[s[0:i]] = s[i+1:]
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// Get creates and returns a Var with the value of the environment variable
|
|
|
|
|
// named by the `key`. It uses the given `def` if the variable does not exist
|
2020-04-02 20:52:37 +08:00
|
|
|
|
// in the environment.
|
2021-10-21 18:22:47 +08:00
|
|
|
|
func Get(key string, def ...interface{}) *gvar.Var {
|
2020-04-02 20:52:37 +08:00
|
|
|
|
v, ok := os.LookupEnv(key)
|
2021-10-21 18:22:47 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
if len(def) > 0 {
|
|
|
|
|
return gvar.New(def[0])
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2020-04-02 20:52:37 +08:00
|
|
|
|
}
|
|
|
|
|
return gvar.New(v)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// Set sets the value of the environment variable named by the `key`.
|
2019-06-11 20:57:43 +08:00
|
|
|
|
// It returns an error, if any.
|
2021-12-22 22:22:42 +08:00
|
|
|
|
func Set(key, value string) (err error) {
|
|
|
|
|
err = os.Setenv(key, value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Wrapf(err, `set environment key-value failed with key "%s", value "%s"`, key, value)
|
|
|
|
|
}
|
|
|
|
|
return
|
2017-12-18 10:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 00:06:03 +08:00
|
|
|
|
// SetMap sets the environment variables using map.
|
2021-12-22 22:22:42 +08:00
|
|
|
|
func SetMap(m map[string]string) (err error) {
|
2020-12-05 00:06:03 +08:00
|
|
|
|
for k, v := range m {
|
2021-12-22 22:22:42 +08:00
|
|
|
|
if err = Set(k, v); err != nil {
|
2020-12-05 00:06:03 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// Contains checks whether the environment variable named `key` exists.
|
2019-07-27 14:40:22 +08:00
|
|
|
|
func Contains(key string) bool {
|
|
|
|
|
_, ok := os.LookupEnv(key)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 22:32:07 +08:00
|
|
|
|
// Remove deletes one or more environment variables.
|
2021-12-22 22:22:42 +08:00
|
|
|
|
func Remove(key ...string) (err error) {
|
2020-01-10 22:32:07 +08:00
|
|
|
|
for _, v := range key {
|
2021-12-22 22:22:42 +08:00
|
|
|
|
if err = os.Unsetenv(v); err != nil {
|
|
|
|
|
err = gerror.Wrapf(err, `delete environment key failed with key "%s"`, v)
|
2020-01-10 22:32:07 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2020-12-04 23:44:54 +08:00
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// GetWithCmd returns the environment value specified `key`.
|
2020-12-04 23:44:54 +08:00
|
|
|
|
// If the environment value does not exist, then it retrieves and returns the value from command line options.
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// It returns the default value `def` if none of them exists.
|
2020-12-04 23:44:54 +08:00
|
|
|
|
//
|
|
|
|
|
// Fetching Rules:
|
|
|
|
|
// 1. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
|
|
|
|
|
// 2. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
|
|
|
|
|
func GetWithCmd(key string, def ...interface{}) *gvar.Var {
|
2021-10-21 18:22:47 +08:00
|
|
|
|
envKey := utils.FormatEnvKey(key)
|
2020-12-04 23:44:54 +08:00
|
|
|
|
if v := os.Getenv(envKey); v != "" {
|
2021-10-21 18:22:47 +08:00
|
|
|
|
return gvar.New(v)
|
2020-12-04 23:44:54 +08:00
|
|
|
|
}
|
2021-10-21 18:22:47 +08:00
|
|
|
|
cmdKey := utils.FormatCmdKey(key)
|
2021-12-07 19:08:52 +08:00
|
|
|
|
if v := command.GetOpt(cmdKey); v != "" {
|
|
|
|
|
return gvar.New(v)
|
2021-10-21 18:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
if len(def) > 0 {
|
|
|
|
|
return gvar.New(def[0])
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2020-12-04 23:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// Build builds a map to an environment variable slice.
|
2020-12-04 23:44:54 +08:00
|
|
|
|
func Build(m map[string]string) []string {
|
|
|
|
|
array := make([]string, len(m))
|
|
|
|
|
index := 0
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
array[index] = k + "=" + v
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
return array
|
|
|
|
|
}
|