Gonfig is a Go library for loading, writing, and managing configuration data from a JSON file. It provides a variety of options for custom configuration based on the needs of the user.
Gonfig can be installed using go get:
go get github.com/core49/gonfigTo use Gonfig, first create a repository instance with your desired options:
package main
import"github.com/core49/gonfig"typeYourConfigStructstruct {
Var1stringvar2int
}
funcmain() {
// Create a new repository instance with options// It will not use the os arguments and uses the provided file pathconf, err:=gonfig.New(
gonfig.OptionDisableDefaultFlagConfiguration(true),
gonfig.OptionSetConfigFilePathVariable("/path/to/config.json"),
)
iferr!=nil {
// Handle error
}
varconfigYourConfigStruct// Check if the file exists or if the file is emptyifexists, err:=conf.IsEmpty(&config); err!=nil||exists {
// handle error or what you want to do if the file exists
}
// Write an empty JSON skeleton of the struct/model to the fileiferr:=conf.WriteSkeleton(&config); err!=nil {
// Handle error
}
// Load configuration data into a struct/modeliferr:=conf.Load(&config); err!=nil {
// Handle error
}
}The following options are available when creating a new repository instance:
OptionSetFileSystem(fs afero.Fs)- sets the file system to the given afero file system instance.OptionSetConfigFilePathVariable(path string)- sets the file path to the given string.OptionDisableDefaultFlagConfiguration(v bool)- disables the default flag configuration if set to true.OptionSetFlagConfiguration(fc gonfig.FlagConfiguration)- sets the flag configuration to the given flag configuration slice.OptionAppendFlagConfig(fc gonfig.FlagConfig)- appends the given flag configuration to the flag configuration slice.OptionSetOsArgs(osArgs []string)- sets the os arguments to the given string slice.
Contributions to the Gonfig project are welcome and encouraged! Please see the contributing guidelines for more information.
Gonfig is licensed under the MIT License.