github.com/blend/go-sdk@v1.20220411.3/configutil/env.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/env"
    15  )
    16  
    17  var (
    18  	_ StringSource   = (*EnvVars)(nil)
    19  	_ BoolSource     = (*EnvVars)(nil)
    20  	_ IntSource      = (*EnvVars)(nil)
    21  	_ Float64Source  = (*EnvVars)(nil)
    22  	_ DurationSource = (*EnvVars)(nil)
    23  )
    24  
    25  // Env returns a new environment value provider.
    26  func Env(key string) EnvVars {
    27  	return EnvVars{
    28  		Key: key,
    29  	}
    30  }
    31  
    32  // EnvVars is a value provider where the string represents the environment variable name.
    33  // It can be used with *any* config.Set___ type.
    34  type EnvVars struct {
    35  	Key string
    36  }
    37  
    38  // String returns a given environment variable as a string.
    39  func (e EnvVars) String(ctx context.Context) (*string, error) {
    40  	vars := e.vars(ctx)
    41  	if vars.Has(e.Key) {
    42  		value := vars.String(e.Key)
    43  		return &value, nil
    44  	}
    45  	return nil, nil
    46  }
    47  
    48  // Strings returns a given environment variable as strings.
    49  func (e EnvVars) Strings(ctx context.Context) ([]string, error) {
    50  	vars := e.vars(ctx)
    51  
    52  	if vars.Has(e.Key) {
    53  		return vars.CSV(e.Key), nil
    54  	}
    55  	return nil, nil
    56  }
    57  
    58  // Bool returns a given environment variable as a bool.
    59  func (e EnvVars) Bool(ctx context.Context) (*bool, error) {
    60  	vars := e.vars(ctx)
    61  
    62  	if vars.Has(e.Key) {
    63  		value := vars.Bool(e.Key)
    64  		return &value, nil
    65  	}
    66  	return nil, nil
    67  }
    68  
    69  // Int returns a given environment variable as an int.
    70  func (e EnvVars) Int(ctx context.Context) (*int, error) {
    71  	vars := e.vars(ctx)
    72  
    73  	if vars.Has(e.Key) {
    74  		value, err := vars.Int(e.Key)
    75  		if err != nil {
    76  			return nil, err
    77  		}
    78  		return &value, nil
    79  	}
    80  	return nil, nil
    81  }
    82  
    83  // Int32 returns a given environment variable as an int32.
    84  func (e EnvVars) Int32(ctx context.Context) (*int32, error) {
    85  	vars := e.vars(ctx)
    86  
    87  	if vars.Has(e.Key) {
    88  		value, err := vars.Int32(e.Key)
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		return &value, nil
    93  	}
    94  	return nil, nil
    95  }
    96  
    97  // Int64 returns a given environment variable as an int64.
    98  func (e EnvVars) Int64(ctx context.Context) (*int64, error) {
    99  	vars := e.vars(ctx)
   100  
   101  	if vars.Has(e.Key) {
   102  		value, err := vars.Int64(e.Key)
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  		return &value, nil
   107  	}
   108  	return nil, nil
   109  }
   110  
   111  // Float64 returns a given environment variable as a float64.
   112  func (e EnvVars) Float64(ctx context.Context) (*float64, error) {
   113  	vars := e.vars(ctx)
   114  
   115  	if vars.Has(e.Key) {
   116  		value, err := vars.Float64(e.Key)
   117  		if err != nil {
   118  			return nil, err
   119  		}
   120  		return &value, nil
   121  	}
   122  	return nil, nil
   123  }
   124  
   125  // Duration returns a given environment variable as a time.Duration.
   126  func (e EnvVars) Duration(ctx context.Context) (*time.Duration, error) {
   127  	vars := e.vars(ctx)
   128  
   129  	if vars.Has(e.Key) {
   130  		value, err := vars.Duration(e.Key)
   131  		if err != nil {
   132  			return nil, err
   133  		}
   134  		return &value, nil
   135  	}
   136  	return nil, nil
   137  }
   138  
   139  // vars resolves the env vars
   140  func (e EnvVars) vars(ctx context.Context) env.Vars {
   141  	if vars := env.GetVars(ctx); vars != nil {
   142  		return vars
   143  	}
   144  	return env.Env()
   145  }