You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"fmt""os"
)
// function as a type typegetEnvValidatefunc() (string, error)
// vars mapped to each ENV variable for future usevarmode, service, namespacestring// global err varvarerrerror// function to validate presence and obtain ENV keyfuncgetEnv(keystring) (string, error) {
value:=os.Getenv(key)
iflen(value) ==0 {
// just to print error before terminatingfmt.Println("Missing ENV variable:", key)
// exits at first missing ENVreturn"", fmt.Errorf("ENV %s not found", key)
/* // dumb check to see if we iterate for all described ENVs if error not returned return "", nil */
}
returnvalue, nil
}
// functions sharing same signature as type getEnvValidatefuncmodeEnv() (string, error) {
mode, err=getEnv("MODE")
iferr!=nil {
return"", err
}
returnmode, nil
}
funcserviceEnv() (string, error) {
service, err=getEnv("SERVICE")
iferr!=nil {
return"", err
}
returnservice, nil
}
funcnamespaceEnv() (string, error) {
namespace, err=getEnv("NAMESPACE")
iferr!=nil {
return"", err
}
returnnamespace, nil
}
/* add new functions for new ENVs here */// function taking variadic input & iterating over each fn to execute themfuncinitVar(f...getEnvValidate) error{
for_, fs:=rangef{
_, err:=fs()
iferr!=nil{
returnerr
}
}
returnnil
}
funcmain() {
// construct a slice of fns needed as part of validation// add new fns in the list heregetAndValidate:= []getEnvValidate{modeEnv,serviceEnv,namespaceEnv}
// call function to execute the individual fns initVar(getAndValidate...)
/* // Print out in positive case. We have defined this as global vars fmt.Println(mode) fmt.Println(service) fmt.Println(namespace) */
}
Is this a BUG REPORT or FEATURE REQUEST?
Choose one: BUG REPORT or FEATURE REQUEST
The text was updated successfully, but these errors were encountered: