https://github.com/croz-ltd/confident
Go configuration with confident!
https://github.com/croz-ltd/confident
Last synced: 7 months ago
JSON representation
Go configuration with confident!
- Host: GitHub
- URL: https://github.com/croz-ltd/confident
- Owner: croz-ltd
- Created: 2019-09-23T15:13:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-25T16:26:35.000Z (about 6 years ago)
- Last Synced: 2025-01-22T16:46:45.451Z (9 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Confident
Go configuration with confident!
## Install
```console
go get -u github.com/croz-ltd/confident
```
or as a module:
```
require (
github.com/croz-ltd/confident v0.0.2
)
```## What is Confident?
Confident is the configuration solution for short living Go applications like commander application (`kubectl`, `oc`, ...).
It supports JSON and YAML file types for now.
## Why Confident?
Confident is heavily inspired by [github.com/spf13/viper](https://github.com/spf13/viper).
While Viper is designed with long-running Go process execution in mind (like web servers)
his approach to handling configuration is not suitable for short living commander Go processes.Confident is developed with short-living Go processes in mind, meaning read at the beginning and persisting
at the end of execution.Another main difference with Viper is that Confident unmarshal configuration data into the provided structure.
All changes to the configuration are performed by modifying structure values and those changes will be persisted
in the configuration file. With this approach, you achieve compile-time verification that the configuration parameter
path exists.## Usage
First, you need to define the configuration structure:
```go
package configtype Configuration struct {
Core Core `json:"core" yaml:"core"`
Servers []Server `json:"servers,omitempty" yaml:"servers,omitempty"`
}type Core struct {
Editor string `json:"editor" yaml:"editor"`
}type Server struct {
Name string `json:"name" yaml:"name"`
Url string `json:"url" yaml:"url"`
}
```Next create configuration instance:
> NOTE: Provide configuration default values when creating configuration instance
```go
var Config = Configuration{
Core: Core{
Editor: "vi",
},
}
```Next create Confident instance and reference configuration instance for usage:
```go
var k *confident.Confidentfunc Bootstrap() {
k = confident.New()
k.WithConfiguration(&Config)
//
k.Name = "config"
k.Type = "json"
k.Path = "."
k.Permission = os.FileMode(0644)
//
k.Read()
}
```Modify configuration attributes:
```go
config.Config.Core.Editor = "vim"
```Persist changes to file by calling:
```go
func Persist(){
k.Persist()
}
```### Typical use case for commander applications
If you write commander application like `kubectl`, `oc` or similar, you can "wrap" your main
code with Confident initialization before, and Confident Persist at the end:
```go
func main() {
config.Bootstrap()
<...magic...>
config.Persist()
}
```
and in your magic code just read and/or modify your configuration instance.
If there is any changes to the configuration, Confident Persist will save it.