template/cmd/app/config.go
djmil 7bc91b0890 pkg/logger: replace NewDevelopment with NewCLI(level, debugFile)
Two modes for interactive CLI use — driven by debugFile presence:
- Normal (debugFile=""): human text on screen, INFO and above.
- Debug  (debugFile set): same screen + full JSON trace to file.

Auto-detects TTY; falls back to 12-factor JSON when piped/redirected.
IsInteractive() exposes TTY detection for call sites that need it.

Terminal format: INFO has no prefix (program's normal voice);
WARN prints "warning: …"; ERROR prints "error: …"; DEBUG "debug: …".

Breaking: NewDevelopment removed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-03 16:48:34 +00:00

63 lines
1.5 KiB
Go

package main
import (
"flag"
)
// Config is the root configuration object. Add sub-structs as the app grows.
type Config struct {
App AppConfig
Logger LoggerConfig
Greeter GreeterConfig
}
// AppConfig holds generic application settings.
type AppConfig struct {
Port int
Env string // dev | staging | prod
}
// LoggerConfig controls logging behavior.
type LoggerConfig struct {
Level string // debug | info | warn | error
DebugFile string // non-empty enables debug mode: writes full JSON trace to this path
}
// Greeter config for internal/greeter/Service.
type GreeterConfig struct {
Name string
}
// parseArgs parses application configuration from command-line flags.
// Defaults are defined here; override at runtime with flags:
//
// ./app -port 9090 -env prod -log-level warn 2 > log.log
//
// Usage:
//
// cfg := config.parseArgs()
// fmt.Println(cfg.App.Port)
func parseArgs() *Config {
name := flag.String("name", "Gopher", "application name")
port := flag.Int("port", 8080, "listen port")
env := flag.String("env", "dev", "environment: dev | staging | prod")
level := flag.String("log-level", "info", "log level: debug | info | warn | error")
debugLog := flag.String("debug-log", "", "write full debug trace to file (enables debug mode)")
flag.Parse()
return &Config{
App: AppConfig{
Port: *port,
Env: *env,
},
Logger: LoggerConfig{
Level: *level,
DebugFile: *debugLog,
},
Greeter: GreeterConfig{
Name: *name,
},
}
}