initial
This commit is contained in:
139
pkg/config/example_test.go
Normal file
139
pkg/config/example_test.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.eeqj.de/sneak/webhooker/pkg/config"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// Set the environment explicitly
|
||||
config.SetEnvironment("dev")
|
||||
|
||||
// Get configuration values
|
||||
baseURL := config.GetString("baseURL")
|
||||
timeout := config.GetInt("timeout", 30)
|
||||
debugMode := config.GetBool("debugMode", false)
|
||||
|
||||
fmt.Printf("Base URL: %s\n", baseURL)
|
||||
fmt.Printf("Timeout: %d\n", timeout)
|
||||
fmt.Printf("Debug Mode: %v\n", debugMode)
|
||||
|
||||
// Get secret values
|
||||
apiKey := config.GetSecretString("api_key")
|
||||
if apiKey != "" {
|
||||
fmt.Printf("API Key: %s...\n", apiKey[:8])
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleSetEnvironment() {
|
||||
// Your application determines which environment to use
|
||||
// This could come from command line args, env vars, etc.
|
||||
environment := os.Getenv("APP_ENV")
|
||||
if environment == "" {
|
||||
environment = "development"
|
||||
}
|
||||
|
||||
// Set the environment explicitly
|
||||
config.SetEnvironment(environment)
|
||||
|
||||
// Now use configuration throughout your application
|
||||
fmt.Printf("Environment: %s\n", environment)
|
||||
fmt.Printf("App Name: %s\n", config.GetString("app_name"))
|
||||
}
|
||||
|
||||
func ExampleGetString() {
|
||||
config.SetEnvironment("prod")
|
||||
|
||||
// Get a string configuration value with a default
|
||||
baseURL := config.GetString("baseURL", "http://localhost:8080")
|
||||
fmt.Printf("Base URL: %s\n", baseURL)
|
||||
}
|
||||
|
||||
func ExampleGetInt() {
|
||||
config.SetEnvironment("prod")
|
||||
|
||||
// Get an integer configuration value with a default
|
||||
port := config.GetInt("port", 8080)
|
||||
fmt.Printf("Port: %d\n", port)
|
||||
}
|
||||
|
||||
func ExampleGetBool() {
|
||||
config.SetEnvironment("dev")
|
||||
|
||||
// Get a boolean configuration value with a default
|
||||
debugMode := config.GetBool("debugMode", false)
|
||||
fmt.Printf("Debug Mode: %v\n", debugMode)
|
||||
}
|
||||
|
||||
func ExampleGetSecretString() {
|
||||
config.SetEnvironment("prod")
|
||||
|
||||
// Get a secret string value
|
||||
apiKey := config.GetSecretString("api_key")
|
||||
if apiKey != "" {
|
||||
// Be careful not to log the full secret!
|
||||
fmt.Printf("API Key configured: yes\n")
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleLoadFile() {
|
||||
// Load configuration from a specific file
|
||||
if err := config.LoadFile("/path/to/config.yaml"); err != nil {
|
||||
log.Printf("Failed to load config: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
config.SetEnvironment("staging")
|
||||
fmt.Printf("Loaded configuration from custom file\n")
|
||||
}
|
||||
|
||||
func ExampleReload() {
|
||||
config.SetEnvironment("dev")
|
||||
|
||||
// Get initial value
|
||||
oldValue := config.GetString("some_key")
|
||||
|
||||
// ... config file might have been updated ...
|
||||
|
||||
// Reload configuration from file
|
||||
if err := config.Reload(); err != nil {
|
||||
log.Printf("Failed to reload config: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Get potentially updated value
|
||||
newValue := config.GetString("some_key")
|
||||
fmt.Printf("Value changed: %v\n", oldValue != newValue)
|
||||
}
|
||||
|
||||
// Example config.yaml structure:
|
||||
/*
|
||||
environments:
|
||||
development:
|
||||
config:
|
||||
baseURL: http://localhost:8000
|
||||
debugMode: true
|
||||
port: 8000
|
||||
secrets:
|
||||
api_key: dev-key-12345
|
||||
|
||||
production:
|
||||
config:
|
||||
baseURL: https://api.example.com
|
||||
debugMode: false
|
||||
port: 443
|
||||
GCPProject: my-project-123
|
||||
AWSRegion: us-west-2
|
||||
secrets:
|
||||
api_key: $GSM:prod-api-key
|
||||
db_password: $ASM:prod/db/password
|
||||
|
||||
configDefaults:
|
||||
app_name: My Application
|
||||
timeout: 30
|
||||
log_level: INFO
|
||||
port: 8080
|
||||
*/
|
||||
Reference in New Issue
Block a user