https://github.com/cinar/config2
Config2 is a lightweight Golang module for managing and populating application configuration from JSON, command line, and env variables.
https://github.com/cinar/config2
configuration configuration-management golang golang-package
Last synced: 5 months ago
JSON representation
Config2 is a lightweight Golang module for managing and populating application configuration from JSON, command line, and env variables.
- Host: GitHub
- URL: https://github.com/cinar/config2
- Owner: cinar
- License: mit
- Created: 2021-06-06T21:37:33.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-10T19:29:28.000Z (over 4 years ago)
- Last Synced: 2025-10-24T21:39:42.795Z (8 months ago)
- Topics: configuration, configuration-management, golang, golang-package
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://godoc.org/github.com/cinar/config2)
[](https://opensource.org/licenses/MIT)
[](https://travis-ci.com/cinar/config2)
# Config2 Go
Config2 is a lightweight Golang module for managing and populating application configuration from JSON, command line, and environment variables.
## Usage
Install package.
```bash
go get github.com/cinar/config2
```
Import Config2.
```Golang
import (
"github.com/cinar/config2
)
```
Define a configuration structure as shown below.
```Golang
type Config struct {
Host string `usage:"Server hostname"`
Port int `usage:"Server port"`
Debug bool `usage:"Enable debug"`
}
```
Config2 provides the following field tags to define additional information for configuration variables.
Tag | Description | Example
--- | --- | ---
usage | Usage for command line help. | `usage:"Server hostname"
Set the default values for the configuration variables.
```Golang
config := &Config{Port: 8080}
```
### Read from command line arguments
Config2 can automatically generate a FlagSet for your configuration structure and parse the command line arguments.
Application can be launched with command line arguments.
```
./main -Debug -Host localhost -Port 9090
```
Use the [ParseCommandLine](https://pkg.go.dev/github.com/cinar/config2#ParseCommandLine) function to parse the command line arguments as shown below.
```Golang
flagSet := config2.ParseCommandLine(os.Args, config)
```
Validate the configuration variables. In case of an error, show the usage through the returned FlagSet as shown below.
```Golang
flagSet.PrintDefaults()
```
The command line arguments, their usage, and their defaults will be shown by the FlagSet as usual.
```
-Debug
Enable debug (default false)
-Host
Server hostname
-Port
Server port (default 8080)
```
### Read from environment variables
Config2 can read configuration from the environment variables.
Application can be launched with the environment variables set as shown below.
```bash
export test_Host=localhost
export test_Port=9090
export test_Debug=true
./main
```
Use the [ParseEnvironmentVariables](https://pkg.go.dev/github.com/cinar/config2#ParseEnvironmentVariables) function to parse the environment variables. The function takes a prefix for the environment variables. Please set it to empty string ("") if no prefix is needed.
```Golang
config2.ParseEnvironmentVariables("test_", config)
```
### Read from JSON file
Config2 can read configuration from a JSON file as well, such as the one below.
```JSON
{
"Host": "localhost",
"Port": 9090,
"Debug": true
}
```
Use the [ParseJson](https://pkg.go.dev/github.com/cinar/config2#ParseJson) function to parse the JSON file.
```Golang
err := config2.ParseJson("test.json", config)
if err != nil {
log.Fatal(err)
}
```
### Parse all
Config2 can parse the JSON file if it exists, parse the environment variables, and parse the command line arguments.
Use the [ParseAll](https://pkg.go.dev/github.com/cinar/config2#ParseAll) function as shown below.
```Golang
config2.ParseAll("test.json", "test_", config)
```
## License
The source code is provided under MIT License.