github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/url/environment.go (about)

     1  package url
     2  
     3  import (
     4  	"os"
     5  )
     6  
     7  const (
     8  	// alphaSpecificEnvironmentVariablePrefix is the prefix to use when checking
     9  	// for alpha-specific environment variables.
    10  	alphaSpecificEnvironmentVariablePrefix = "MUTAGEN_ALPHA_"
    11  	// betaSpecificEnvironmentVariablePrefix is the prefix to use when checking
    12  	// for beta-specific environment variables.
    13  	betaSpecificEnvironmentVariablePrefix = "MUTAGEN_BETA_"
    14  	// sourceSpecificEnvironmentVariablePrefix is the prefix to use when
    15  	// checking for source-specific environment variables.
    16  	sourceSpecificEnvironmentVariablePrefix = "MUTAGEN_SOURCE_"
    17  	// destinationSpecificEnvironmentVariablePrefix is the prefix to use when
    18  	// checking for destination-specific environment variables.
    19  	destinationSpecificEnvironmentVariablePrefix = "MUTAGEN_DESTINATION_"
    20  )
    21  
    22  // lookupEnv is the environment variable lookup function to use. It is a
    23  // variable so that it can be swapped out during testing.
    24  var lookupEnv = os.LookupEnv
    25  
    26  // getEnvironmentVariable returns the value for the specified environment
    27  // variable, as well as whether or not it was found. Endpoint-specific variables
    28  // take precedence over non-specific variables.
    29  func getEnvironmentVariable(name string, kind Kind, first bool) (string, bool) {
    30  	// Validate the variable name.
    31  	if name == "" {
    32  		return "", false
    33  	}
    34  
    35  	// Check for an endpoint-specific variant.
    36  	var endpointSpecificName string
    37  	if kind == Kind_Synchronization {
    38  		if first {
    39  			endpointSpecificName = alphaSpecificEnvironmentVariablePrefix + name
    40  		} else {
    41  			endpointSpecificName = betaSpecificEnvironmentVariablePrefix + name
    42  		}
    43  	} else if kind == Kind_Forwarding {
    44  		if first {
    45  			endpointSpecificName = sourceSpecificEnvironmentVariablePrefix + name
    46  		} else {
    47  			endpointSpecificName = destinationSpecificEnvironmentVariablePrefix + name
    48  		}
    49  	} else {
    50  		panic("unhandled URL kind")
    51  	}
    52  	if value, ok := lookupEnv(endpointSpecificName); ok {
    53  		return value, true
    54  	}
    55  
    56  	// Check for the general variant.
    57  	return lookupEnv(name)
    58  }