github.com/gogf/gf@v1.16.9/os/genv/genv.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package genv provides operations for environment variables of system.
     8  package genv
     9  
    10  import (
    11  	"github.com/gogf/gf/container/gvar"
    12  	"github.com/gogf/gf/os/gcmd"
    13  	"os"
    14  	"strings"
    15  )
    16  
    17  // All returns a copy of strings representing the environment,
    18  // in the form "key=value".
    19  func All() []string {
    20  	return os.Environ()
    21  }
    22  
    23  // Map returns a copy of strings representing the environment as a map.
    24  func Map() map[string]string {
    25  	m := make(map[string]string)
    26  	i := 0
    27  	for _, s := range os.Environ() {
    28  		i = strings.IndexByte(s, '=')
    29  		m[s[0:i]] = s[i+1:]
    30  	}
    31  	return m
    32  }
    33  
    34  // Get returns the value of the environment variable named by the <key>.
    35  // It returns given <def> if the variable does not exist in the environment.
    36  func Get(key string, def ...string) string {
    37  	v, ok := os.LookupEnv(key)
    38  	if !ok && len(def) > 0 {
    39  		return def[0]
    40  	}
    41  	return v
    42  }
    43  
    44  // GetVar creates and returns a Var with the value of the environment variable
    45  // named by the <key>. It uses the given <def> if the variable does not exist
    46  // in the environment.
    47  func GetVar(key string, def ...interface{}) *gvar.Var {
    48  	v, ok := os.LookupEnv(key)
    49  	if !ok && len(def) > 0 {
    50  		return gvar.New(def[0])
    51  	}
    52  	return gvar.New(v)
    53  }
    54  
    55  // Set sets the value of the environment variable named by the <key>.
    56  // It returns an error, if any.
    57  func Set(key, value string) error {
    58  	return os.Setenv(key, value)
    59  }
    60  
    61  // SetMap sets the environment variables using map.
    62  func SetMap(m map[string]string) error {
    63  	for k, v := range m {
    64  		if err := os.Setenv(k, v); err != nil {
    65  			return err
    66  		}
    67  	}
    68  	return nil
    69  }
    70  
    71  // Contains checks whether the environment variable named <key> exists.
    72  func Contains(key string) bool {
    73  	_, ok := os.LookupEnv(key)
    74  	return ok
    75  }
    76  
    77  // Remove deletes one or more environment variables.
    78  func Remove(key ...string) error {
    79  	var err error
    80  	for _, v := range key {
    81  		err = os.Unsetenv(v)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  // GetWithCmd returns the environment value specified <key>.
    90  // If the environment value does not exist, then it retrieves and returns the value from command line options.
    91  // It returns the default value <def> if none of them exists.
    92  //
    93  // Fetching Rules:
    94  // 1. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
    95  // 2. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
    96  func GetWithCmd(key string, def ...interface{}) *gvar.Var {
    97  	value := interface{}(nil)
    98  	if len(def) > 0 {
    99  		value = def[0]
   100  	}
   101  	envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
   102  	if v := os.Getenv(envKey); v != "" {
   103  		value = v
   104  	} else {
   105  		cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
   106  		if v := gcmd.GetOpt(cmdKey); v != "" {
   107  			value = v
   108  		}
   109  	}
   110  	return gvar.New(value)
   111  }
   112  
   113  // Build builds a map to a environment variable slice.
   114  func Build(m map[string]string) []string {
   115  	array := make([]string, len(m))
   116  	index := 0
   117  	for k, v := range m {
   118  		array[index] = k + "=" + v
   119  		index++
   120  	}
   121  	return array
   122  }