https://github.com/miguelpragier/microconfig
Simple Configuration File Engine
https://github.com/miguelpragier/microconfig
configuration configuration-files golang golang-package
Last synced: about 1 month ago
JSON representation
Simple Configuration File Engine
- Host: GitHub
- URL: https://github.com/miguelpragier/microconfig
- Owner: miguelpragier
- License: mit
- Created: 2018-10-17T12:57:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-30T14:13:05.000Z (over 6 years ago)
- Last Synced: 2025-01-27T23:37:47.760Z (3 months ago)
- Topics: configuration, configuration-files, golang, golang-package
- Language: Go
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MicroConfig
Simple Configuration File EngineThe goal is to load and set application config constants from a json file that respects our specific format.
Installation:
```bash
go get github.com/miguelpragier/microconfig
```## Here's the config file format, with some dummy sample pairs
```json
{
"pairs":[
{
"key": "Environment",
"value": "dev"
},
{
"key": "SomeKey",
"value": "Some Value"
},
{
"key": "MyAge",
"value": "43"
},
{
"key": "RandomFloatValue",
"value": "78.910"
},
{
"value": "DatabaseConnectionString",
"key": "databaseuser:a3232323bc4343d5454e656576876899f97@tcp(dbs.databaseserv.er)/mydatabase",
"osEnv":true
}
]
}
```When osEnv is present, the constant is loaded in OS ENVIRONMENT variables as well.
## Gross useCase example considering the above example config file
```golang
package mainimport (
"github.com/miguelpragier/microconfig"
"fmt"
"os"
"strings"
)func main(){
const myConfigFile = "./config.dev.json"
if err:=microconfig.Load(myConfigFile); err!=nil {
panic(err)
}
databaseConnectionString := os.GetEnv("DatabaseConnectionString")
if databaseConnectionString=="" {
panic("OMG! We have no database directions...")
}
appName, err := microconfig.GetString("SomeKey")
if err!=nil {
panic(err)
}
fmt.Printf("Expected: %v, Checked: %v", true, microconfig.Exists("Environment",false))
// Note that Exists will search with caseInsensitive==false, resulting in a notFound/negative return
fmt.Printf("Expected: %v, Checked: %v", false, microconfig.Exists("ENVIRONMENT",false))
myIntegerConfigVariable,_ := microconfig.GetInt("MyAge")
myFloat64ConfigVariable,_ := microconfig.GetFloat("RandomFloatValue")
}
```