Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
93 lines (93 loc) · 2.63 KB
/
Copy pathdoc.go
File metadata and controls
93 lines (93 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package cli provides utilities for building CLI applications with Cobra and Viper.
//
// It simplifies the creation of command-line applications by integrating Cobra's command
// framework with Viper's configuration management, providing sensible defaults for:
//
// - Configuration loading from files, environment variables, and command-line flags
// - Type-safe command execution using Go generics
// - Structured logging via zerolog
// - Path management with Docker support
// - Context propagation for application metadata
//
// # Basic Usage
//
// Create a configuration struct:
//
// type ServerConfig struct {
// Port int `mapstructure:"port"`
// Host string `mapstructure:"host"`
// }
//
// Create a command handler:
//
// func serveCommand(ctx context.Context, cfg *ServerConfig) error {
// fmt.Printf("Starting server on %s:%d\n", cfg.Host, cfg.Port)
// return nil
// }
//
// Register the command with NewCommand:
//
// cfg := &ServerConfig{Port: 8080, Host: "localhost"}
// cmd := cli.NewCommand(
// &cobra.Command{
// Use: "serve",
// Short: "Start the server",
// },
// serveCommand,
// cfg,
// cli.WithConfigFlag("config.yaml"),
// )
//
// # Configuration File Format
//
// Configuration files are YAML with support for multiple sections:
//
// port: 8080
// host: localhost
// log:
// level: debug
// file: "~/.app/logs/app.log"
// max-size: 100
// compress: true
//
// # Configuration Loading Order
//
// InitConfig searches for a config file at /etc/{org}/{app}/... or
// $HOME/.config/{org}/{app}/..., unless --config points at an explicit file.
// Whichever file is found (if any) is merged with flags and environment
// variables using Viper's standard precedence (highest to lowest):
//
// 1. Command-line flags
// 2. Environment variables with prefix {APPNAME}_
// 3. Config file (explicit --config file, or the first standard location found)
// 4. Defaults
//
// # Context Management
//
// Store application metadata in context for propagation through command execution:
//
// ctx := cli.Context(
// context.Background(),
// cli.SetOrgName("myorg"),
// cli.SetAppName("myapp"),
// )
// cmd.SetContext(ctx)
//
// # Advanced Features
//
// Path helpers detect and adapt to Docker environments:
//
// configPath, err := cli.DefaultConfigPath("myorg", "myapp")
// // Returns /config in Docker, $HOME/.config/myorg/myapp otherwise
//
// Error handling patterns follow Go best practices with wrapped errors:
//
// err := cli.InitViperConfig("org", "app", cfg)
// if err != nil {
// return fmt.Errorf("initialization failed: %w", err)
// }
//
// # See Also
//
// Package logging: provides structured logging configuration via zerolog.
package cli