github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/configutil/env.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package configutil 19 20 import ( 21 "os" 22 "strconv" 23 "time" 24 25 "github.com/pkg/errors" 26 27 "github.com/waldiirawan/apm-agent-go/v2/internal/wildcard" 28 ) 29 30 // ParseDurationEnv gets the value of the environment variable envKey 31 // and, if set, parses it as a duration. If the environment variable 32 // is unset, defaultDuration is returned. 33 func ParseDurationEnv(envKey string, defaultDuration time.Duration) (time.Duration, error) { 34 return ParseDurationEnvOptions(envKey, defaultDuration, DurationOptions{ 35 MinimumDurationUnit: time.Millisecond, 36 }) 37 } 38 39 // ParseDurationEnvOptions gets the value of the environment variable envKey 40 // and, if set, parses it as a duration. If the environment variable is unset, 41 // defaultDuration is returned. 42 func ParseDurationEnvOptions(envKey string, defaultDuration time.Duration, opts DurationOptions) (time.Duration, error) { 43 value := os.Getenv(envKey) 44 if value == "" { 45 return defaultDuration, nil 46 } 47 d, err := ParseDurationOptions(value, opts) 48 if err != nil { 49 return 0, errors.Wrapf(err, "failed to parse %s", envKey) 50 } 51 return d, nil 52 } 53 54 // ParseSizeEnv gets the value of the environment variable envKey 55 // and, if set, parses it as a size. If the environment variable 56 // is unset, defaultSize is returned. 57 func ParseSizeEnv(envKey string, defaultSize Size) (Size, error) { 58 value := os.Getenv(envKey) 59 if value == "" { 60 return defaultSize, nil 61 } 62 s, err := ParseSize(value) 63 if err != nil { 64 return 0, errors.Wrapf(err, "failed to parse %s", envKey) 65 } 66 return s, nil 67 } 68 69 // ParseBoolEnv gets the value of the environment variable envKey 70 // and, if set, parses it as a boolean. If the environment variable 71 // is unset, defaultValue is returned. 72 func ParseBoolEnv(envKey string, defaultValue bool) (bool, error) { 73 value := os.Getenv(envKey) 74 if value == "" { 75 return defaultValue, nil 76 } 77 b, err := strconv.ParseBool(value) 78 if err != nil { 79 return false, errors.Wrapf(err, "failed to parse %s", envKey) 80 } 81 return b, nil 82 } 83 84 // ParseListEnv gets the value of the environment variable envKey 85 // and, if set, parses it as a list separated by sep. If the environment 86 // variable is unset, defaultValue is returned. 87 func ParseListEnv(envKey, sep string, defaultValue []string) []string { 88 value := os.Getenv(envKey) 89 if value == "" { 90 return defaultValue 91 } 92 return ParseList(value, sep) 93 } 94 95 // ParseWildcardPatternsEnv gets the value of the environment variable envKey 96 // and, if set, parses it as a list of wildcard patterns. If the environment 97 // variable is unset, defaultValue is returned. 98 func ParseWildcardPatternsEnv(envKey string, defaultValue wildcard.Matchers) wildcard.Matchers { 99 value := os.Getenv(envKey) 100 if value == "" { 101 return defaultValue 102 } 103 return ParseWildcardPatterns(value) 104 }