mirror of
https://github.com/aykhans/bsky-feedgen.git
synced 2025-07-17 21:34:00 +00:00
🦋
This commit is contained in:
50
pkg/utils/env.go
Normal file
50
pkg/utils/env.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// GetEnv retrieves the environment variable named by the key envName,
|
||||
// and attempts to parse its value into the specified type T.
|
||||
//
|
||||
// It returns the parsed value of type T and a nil error on success.
|
||||
// It returns a zero value of type T and an error if the environment
|
||||
// variable is not set, or if parsing fails using the ParseValue function.
|
||||
func GetEnv[T any](envName string) (T, error) {
|
||||
var zero T
|
||||
|
||||
envStr := os.Getenv(envName)
|
||||
if envStr == "" {
|
||||
return zero, fmt.Errorf("environment variable %s is not set", envName)
|
||||
}
|
||||
|
||||
parsedEnv, err := ParseString[T](envStr)
|
||||
if err != nil {
|
||||
return zero, fmt.Errorf("failed to parse environment variable %s: %s", envName, err)
|
||||
}
|
||||
|
||||
return parsedEnv, nil
|
||||
}
|
||||
|
||||
// GetEnvOr retrieves the environment variable named by the key envName,
|
||||
// and attempts to parse its value into the specified type T.
|
||||
//
|
||||
// It returns the parsed value of type T and a nil error on success.
|
||||
// If the environment variable is not set, it returns the provided default value
|
||||
// and a nil error. If parsing fails, it returns a zero value of type T and an error.
|
||||
func GetEnvOr[T any](envName string, defaultValue T) (T, error) {
|
||||
var zero T
|
||||
|
||||
envStr := os.Getenv(envName)
|
||||
if envStr == "" {
|
||||
return defaultValue, nil
|
||||
}
|
||||
|
||||
parsedEnv, err := ParseString[T](envStr)
|
||||
if err != nil {
|
||||
return zero, fmt.Errorf("failed to parse environment variable %s: %s", envName, err)
|
||||
}
|
||||
|
||||
return parsedEnv, nil
|
||||
}
|
Reference in New Issue
Block a user