template/cmd/app/config.go
djmil 4f55fcbef5 build stamping
- store build metadata (version, commit, date)
- multi-binary build: `make build` builds all packages from ./cmd
- devcontainer adds ./bin to the PATH by default
2026-06-13 20:32:24 +00:00

66 lines
1.6 KiB
Go

package main
import (
"flag"
)
// Config is the root configuration object. Add sub-structs as the app grows.
type Config struct {
Version bool
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
LogDump 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 {
version := flag.Bool("version", false, "print version information and exit")
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("log-dump", "", "write full debug trace to file (enables debug mode)")
flag.Parse()
return &Config{
Version: *version,
App: AppConfig{
Port: *port,
Env: *env,
},
Logger: LoggerConfig{
Level: *level,
LogDump: *debugLog,
},
Greeter: GreeterConfig{
Name: *name,
},
}
}