-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfg.go
194 lines (187 loc) · 6.26 KB
/
cfg.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gocfg
import (
"github.com/dsbasko/go-cfg/internal/env"
"github.com/dsbasko/go-cfg/internal/file"
"github.com/dsbasko/go-cfg/internal/flag"
)
// ReadEnv is a function that reads environment variables into the provided cfg structure.
// The cfg parameter should be a pointer to a struct where each field represents an environment variable.
// The function returns an error if the reading process fails.
//
// Example of how to use the ReadEnv function:
//
// Define a struct that represents your environment variables. For example:
//
// type Config struct {
// Mode string `env:"MODE"`
// HTTP struct {
// Host string `env:"HTTP_HOST"`
// Port int `env:"HTTP_PORT"`
// }
// }
//
// Then, create an instance of your struct and call the ReadEnv function:
//
// func main() {
// cfg := &Config{}
// if err := gocfg.ReadEnv(cfg); err != nil {
// log.Fatalf("failed to read environment variables: %v", err)
// }
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the MODE, REST_HOST and REST_PORT environment variables into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
func ReadEnv(cfg any) error {
return env.Read(cfg)
}
// MustReadEnv is similar to ReadEnv but panics if the reading process fails.
// This function is useful when the absence of environment variables should lead to a program termination.
//
// Example:
//
// type Config struct {
// Mode string `env:"MODE"`
// HTTP struct {
// Host string `env:"HTTP_HOST"`
// Port int `env:"HTTP_PORT"`
// }
// }
//
// func main() {
// cfg := &Config{}
// gocfg.MustReadEnv(cfg)
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the MODE, HTTP_HOST and HTTP_PORT environment variables into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
// If any of these environment variables are not set, the program will panic.
func MustReadEnv(cfg any) {
if err := ReadEnv(cfg); err != nil {
panic(err)
}
}
// ReadFlag reads command-line flags into the provided cfg structure.
// The cfg parameter should be a pointer to a struct where each field represents a command-line flag.
// This function returns an error if the parsing process fails.
//
// Example:
//
// type Config struct {
// Mode string `flag:"mode" s-flag:"m" description:"Application mode"`
// HTTP struct {
// Host string `flag:"http-host" s-flag:"hh" description:"HTTP host"`
// Port int `flag:"http-port" s-flag:"hp" description:"HTTP port"`
// }
// }
//
// func main() {
// cfg := &Config{}
// if err := gocfg.ReadFlag(cfg); err != nil {
// log.Fatalf("failed to read command-line flags: %v", err)
// }
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the command-line flags --mode, --http-host and --http-port (or -m, -hh and -hp respectively) into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
// If any of these flags are not set, the function will return an error.
func ReadFlag(cfg any) error {
return flag.Read(cfg)
}
// MustReadFlag is similar to ReadFlag but panics if the reading process fails.
// This function is useful when the absence of command-line flags should lead to a program termination.
//
// Example:
//
// type Config struct {
// Mode string `flag:"mode" s-flag:"m" description:"Application mode"`
// HTTP struct {
// Host string `flag:"http-host" s-flag:"hh" description:"HTTP host"`
// Port int `flag:"http-port" s-flag:"hp" description:"HTTP port"`
// }
// }
//
// func main() {
// cfg := &Config{}
// gocfg.MustReadFlag(cfg)
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the command-line flags --mode, --http-host and --http-port (or -m, -hh and -hp respectively) into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
// If any of these flags are not set, the program will panic.
func MustReadFlag(cfg any) {
if err := ReadFlag(cfg); err != nil {
panic(err)
}
}
// ReadFile reads configuration from a file into the provided cfg structure.
// The path parameter is the path to the configuration file.
// The cfg parameter should be a pointer to a struct where each field represents a configuration option.
// This function returns an error if the reading process fails.
//
// Example:
//
// type Config struct {
// Mode string `yaml:"mode"`
// HTTP struct {
// Host string `yaml:"http_host"`
// Port int `yaml:"http_port"`
// }
// }
//
// func main() {
// cfg := &Config{}
// if err := gocfg.ReadFile("config.yaml", cfg); err != nil {
// log.Fatalf("failed to read configuration file: %v", err)
// }
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the mode, http_host and http_port configuration options from the config.yaml file into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
// If any of these configuration options are not set in the file, the function will return an error.
func ReadFile(path string, cfg any) error {
return file.Read(path, cfg)
}
// MustReadFile is similar to ReadFile but panics if the reading process fails.
// This function is useful when the absence of a configuration file should lead to a program termination.
//
// Example:
//
// type Config struct {
// Mode string `yaml:"mode"`
// HTTP struct {
// Host string `yaml:"host"`
// Port int `yaml:"port"`
// } `yaml:"http"`
// }
//
// func main() {
// cfg := &Config{}
// gocfg.MustReadFile("config.yaml", cfg)
//
// fmt.Printf("Mode: %s\n", cfg.Mode)
// fmt.Printf("HTTP Host: %s\n", cfg.HTTP.Host)
// fmt.Printf("HTTP Port: %d\n", cfg.HTTP.Port)
// }
//
// This will read the mode, http_host and http_port configuration options from the config.yaml file into the Mode, HTTP.Host and HTTP.Port fields of the cfg variable.
// If any of these configuration options are not set in the file, the program will panic.
func MustReadFile(path string, cfg any) {
if err := ReadFile(path, cfg); err != nil {
panic(err)
}
}