github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/env/env.go (about)

     1  // Package env provides a convenient way to convert environment
     2  // variables into Go data. It is similar in design to package
     3  // flag.
     4  package env
     5  
     6  import (
     7  	"log"
     8  	"net/url"
     9  	"os"
    10  	"strconv"
    11  	"strings"
    12  	"time"
    13  )
    14  
    15  var funcs []func() bool
    16  
    17  // Int returns a new int pointer.
    18  // When Parse is called,
    19  // env var name will be parsed
    20  // and the resulting value
    21  // will be assigned to the returned location.
    22  func Int(name string, value int) *int {
    23  	p := new(int)
    24  	IntVar(p, name, value)
    25  	return p
    26  }
    27  
    28  // IntVar defines an int var with the specified
    29  // name and default value. The argument p points
    30  // to an int variable in which to store the
    31  // value of the environment var.
    32  func IntVar(p *int, name string, value int) {
    33  	*p = value
    34  	funcs = append(funcs, func() bool {
    35  		if s := os.Getenv(name); s != "" {
    36  			v, err := strconv.Atoi(s)
    37  			if err != nil {
    38  				log.Println(name, err)
    39  				return false
    40  			}
    41  			*p = v
    42  		}
    43  		return true
    44  	})
    45  }
    46  
    47  // Bool returns a new bool pointer.
    48  // When Parse is called,
    49  // env var name will be parsed
    50  // and the resulting value
    51  // will be assigned to the returned location.
    52  // Parsing uses strconv.ParseBool.
    53  func Bool(name string, value bool) *bool {
    54  	p := new(bool)
    55  	BoolVar(p, name, value)
    56  	return p
    57  }
    58  
    59  // BoolVar defines a bool var with the specified
    60  // name and default value. The argument p points
    61  // to a bool variable in which to store the value
    62  // of the environment variable.
    63  func BoolVar(p *bool, name string, value bool) {
    64  	*p = value
    65  	funcs = append(funcs, func() bool {
    66  		if s := os.Getenv(name); s != "" {
    67  			v, err := strconv.ParseBool(s)
    68  			if err != nil {
    69  				log.Println(name, err)
    70  				return false
    71  			}
    72  			*p = v
    73  		}
    74  		return true
    75  	})
    76  }
    77  
    78  // Duration returns a new time.Duration pointer.
    79  // When Parse is called,
    80  // env var name will be parsed
    81  // and the resulting value
    82  // will be assigned to the returned location.
    83  func Duration(name string, value time.Duration) *time.Duration {
    84  	p := new(time.Duration)
    85  	DurationVar(p, name, value)
    86  	return p
    87  }
    88  
    89  // DurationVar defines a time.Duration var with
    90  // the specified name and default value. The
    91  // argument p points to a time.Duration variable
    92  // in which to store the value of the environment
    93  // variable.
    94  func DurationVar(p *time.Duration, name string, value time.Duration) {
    95  	*p = value
    96  	funcs = append(funcs, func() bool {
    97  		if s := os.Getenv(name); s != "" {
    98  			v, err := time.ParseDuration(s)
    99  			if err != nil {
   100  				log.Println(name, err)
   101  				return false
   102  			}
   103  			*p = v
   104  		}
   105  		return true
   106  	})
   107  }
   108  
   109  // URL returns a new url.URL pointer.
   110  // When Parse is called,
   111  // env var name will be parsed
   112  // and the resulting value
   113  // will be assigned to the returned location.
   114  // URL panics if there is an error parsing
   115  // the given default value.
   116  func URL(name string, value string) *url.URL {
   117  	p := new(url.URL)
   118  	URLVar(p, name, value)
   119  	return p
   120  }
   121  
   122  // URLVar defines a url.URL variable with
   123  // the specified name ande default value.
   124  // The argument p points to a url.URL variable
   125  // in which to store the value of the environment
   126  // variable.
   127  func URLVar(p *url.URL, name string, value string) {
   128  	v, err := url.Parse(value)
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  	*p = *v
   133  	funcs = append(funcs, func() bool {
   134  		if s := os.Getenv(name); s != "" {
   135  			v, err := url.Parse(s)
   136  			if err != nil {
   137  				log.Println(name, err)
   138  				return false
   139  			}
   140  			*p = *v
   141  		}
   142  		return true
   143  	})
   144  }
   145  
   146  // String returns a new string pointer.
   147  // When Parse is called,
   148  // env var name will be assigned
   149  // to the returned location.
   150  func String(name string, value string) *string {
   151  	p := new(string)
   152  	StringVar(p, name, value)
   153  	return p
   154  }
   155  
   156  // StringVar defines a string with the
   157  // specified name and default value. The
   158  // argument p points to a string variable in
   159  // which to store the value of the environment
   160  // var.
   161  func StringVar(p *string, name string, value string) {
   162  	*p = value
   163  	funcs = append(funcs, func() bool {
   164  		if s := os.Getenv(name); s != "" {
   165  			*p = s
   166  		}
   167  		return true
   168  	})
   169  }
   170  
   171  // StringSlice returns a pointer to a slice
   172  // of strings. It expects env var name to
   173  // be a list of items delimited by commas.
   174  // If env var name is missing, StringSlice
   175  // returns a pointer to a slice of the value
   176  // strings.
   177  func StringSlice(name string, value ...string) *[]string {
   178  	p := new([]string)
   179  	StringSliceVar(p, name, value...)
   180  	return p
   181  }
   182  
   183  // StringSliceVar defines a new string slice
   184  // with the specified name. The argument p
   185  // points to a string slice variable in which
   186  // to store the value of the environment var.
   187  func StringSliceVar(p *[]string, name string, value ...string) {
   188  	*p = value
   189  	funcs = append(funcs, func() bool {
   190  		if s := os.Getenv(name); s != "" {
   191  			a := strings.Split(s, ",")
   192  			*p = a
   193  		}
   194  		return true
   195  	})
   196  }
   197  
   198  // Parse parses known env vars
   199  // and assigns the values to the variables
   200  // that were previously registered.
   201  // If any values cannot be parsed,
   202  // Parse prints an error message for each one
   203  // and exits the process with status 1.
   204  func Parse() {
   205  	ok := true
   206  	for _, f := range funcs {
   207  		ok = f() && ok
   208  	}
   209  	if !ok {
   210  		os.Exit(1)
   211  	}
   212  }