gonfig

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2023 License: MIT Imports: 8 Imported by: 0

README

Go Version Project status Last commit Go Report Card codecov MIT license GoDoc

Gonfig

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.

Installation

Gonfig can be installed using go get:

go get github.com/core49/gonfig

Usage

To use Gonfig, first create a repository instance with your desired options:

package main

import "github.com/core49/gonfig"

type YourConfigStruct struct {
	Var1 string
	var2 int
}

func main() {
	// Create a new repository instance with options
	// It will not use the os arguments and uses the provided file path
	conf, err := gonfig.New(
		gonfig.OptionDisableDefaultFlagConfiguration(true),
		gonfig.OptionSetConfigFilePathVariable("/path/to/config.json"),
	)

	if err != nil {
		// Handle error
	}

	var config YourConfigStruct

	// Check if the file exists or if the file is empty
	if exists, 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 file
	if err := conf.WriteSkeleton(&config); err != nil {
		// Handle error
	}

	// Load configuration data into a struct/model
	if err := conf.Load(&config); err != nil {
		// Handle error
	}
}

Options

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.

Contributing

Contributions to the Gonfig project are welcome and encouraged! Please see the contributing guidelines for more information.

License

Gonfig is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyConfigFilePath is an error indicating that a file path is empty.
	ErrEmptyConfigFilePath = errors.New("filepath is empty")
	// ErrInvalidConfigModel is an error indicating that a configuration model is invalid.
	ErrInvalidConfigModel = errors.New("model is empty or no valid struct")
	// ErrConfigFileExist is an error indicating that the configuration file exists and an empty skeleton can not be generated
	ErrConfigFileExist = errors.New("unable to generate skeleton. file already exists")
)

Functions

This section is empty.

Types

type Gonfig added in v0.3.0

type Gonfig interface {
	// Load loads configuration data from the file at the file path into the given model.
	Load(model interface{}) error
	// WriteSkeleton writes a JSON skeleton of the model to the file path.
	WriteSkeleton(model interface{}) error
	// IsEmpty checks if the config file is empty.  Returns `true` if the file does not exist or is empty.
	IsEmpty(model interface{}) (bool, error)
}

Gonfig interface contains all the functions that are available to public use.

func New

func New(options ...OptionFunc) (Gonfig, error)

New creates a new Repository with the given options. It applies the flag configuration, generates a default file path if not disabled, and returns the Repository

type OptionFunc

type OptionFunc func(r *Repository)

OptionFunc is a function that takes a Repository pointer and modifies it based on the function's implementation.

func OptionAppendFlagConfig

func OptionAppendFlagConfig(fc flagConfig) OptionFunc

OptionAppendFlagConfig appends the given `flagConfig` to the flag configuration.

func OptionDisableDefaultFlagConfiguration

func OptionDisableDefaultFlagConfiguration(v bool) OptionFunc

OptionDisableDefaultFlagConfiguration disables the default flag configuration.

func OptionSetConfigFilePathVariable

func OptionSetConfigFilePathVariable(path string) OptionFunc

OptionSetConfigFilePathVariable sets the file path and disables default path generation.

func OptionSetFileSystem

func OptionSetFileSystem(fs afero.Fs) OptionFunc

OptionSetFileSystem sets the file system.

func OptionSetFlagConfiguration

func OptionSetFlagConfiguration(fc flagConfiguration) OptionFunc

OptionSetFlagConfiguration sets the flag configuration to the given `flagConfiguration`.

func OptionSetOsArgs

func OptionSetOsArgs(osArgs []string) OptionFunc

OptionSetOsArgs sets the os arguments.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository struct is the initialized gonfig.

func (*Repository) IsEmpty added in v0.2.0

func (r *Repository) IsEmpty(model interface{}) (bool, error)

IsEmpty checks if the config file is empty. Returns `true` if the file does not exist or is empty.

func (*Repository) Load

func (r *Repository) Load(model interface{}) error

Load loads configuration data from the file at the file path into the given model.

func (*Repository) WriteSkeleton

func (r *Repository) WriteSkeleton(model interface{}) error

WriteSkeleton writes a JSON skeleton of the model to the file path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL