mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-15 10:41:09 +02:00
Shared Go utilities for all ManaCore Go services: - httputil: WriteJSON, WriteError, DecodeJSON - envutil: Get, GetInt, GetBool, GetSlice - 8 tests, all passing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
44 lines
1 KiB
Go
44 lines
1 KiB
Go
// Package envutil provides shared environment variable helpers for ManaCore Go services.
|
|
package envutil
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Get returns an environment variable value or a fallback default.
|
|
func Get(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
// GetInt returns an environment variable as int or a fallback default.
|
|
func GetInt(key string, fallback int) int {
|
|
if v := os.Getenv(key); v != "" {
|
|
if i, err := strconv.Atoi(v); err == nil {
|
|
return i
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
// GetBool returns an environment variable as bool or a fallback default.
|
|
func GetBool(key string, fallback bool) bool {
|
|
if v := os.Getenv(key); v != "" {
|
|
if b, err := strconv.ParseBool(v); err == nil {
|
|
return b
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
// GetSlice returns an environment variable as a comma-separated slice or a fallback default.
|
|
func GetSlice(key string, fallback []string) []string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return strings.Split(v, ",")
|
|
}
|
|
return fallback
|
|
}
|