configurable

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 12 Imported by: 0

README

Configurable Package

The Configurable package provides a simple and flexible way to handle configuration data in your Go projects. It allows you to define and manage various types of configuration variables, such as integers, strings, booleans, durations, and more. This package supports configuration parsing from JSON, YAML, and INI files, as well as environment variables.

Installation

To use the Configurable package in your project, you need to have Go installed and set up. Then, you can install the package by running the following command in your terminal:

go get -u github.com/andreimerlescu/configurable

Usage

To use the Configurable package in your Go code, you need to import it:

import "github.com/andreimerlescu/configurable"
Creating a Configurable Instance

To get started, you need to create an instance of the Configurable struct by calling the NewConfigurable() function:

config := configurable.New()
Defining Configuration Variables

The Configurable package provides several methods to define different types of configuration variables. Each method takes a name, default value, and usage description as parameters and returns a pointer to the respective variable:

port := config.NewInt("port", 8080, "The port number to listen on")
timeout := config.NewDuration("timeout", time.Second * 5, "The timeout duration for requests")
debug := config.NewBool("debug", false, "Enable debug mode")
Loading Configuration from Files

You can load configuration data from JSON, YAML, and INI files using the LoadFile() method:

err := config.LoadFile("config.json")
if err != nil {
    // Handle error
}

The package automatically parses the file based on its extension. Make sure to place the file in the correct format in the specified location.

Parsing Command-Line Arguments

The Configurable package also allows you to parse command-line arguments. Call the Parse() method to parse the arguments after defining your configuration variables:

err := config.Parse("")
if err != nil {
    // Handle error
}

Passing an empty string to Parse() means it will only parse the command-line arguments and not load any file.

Accessing Configuration Values

You can access the values of your configuration variables using the respective getter methods:

fmt.Println("Port:", *port)
fmt.Println("Timeout:", *timeout)
fmt.Println("Debug mode:", *debug)
Environment Variables

The Configurable package supports setting configuration values through environment variables. If an environment variable with the same name as a configuration variable exists, the package will automatically assign its value to the respective variable. Ensure that the environment variables are in uppercase and match the configuration variable names.

Displaying Usage Information

To generate a usage string with information about your configuration variables, use the Usage() method:

usage := config.Usage()
fmt.Println(usage)

The generated usage string includes information about each configuration variable, including its name, default value, description, and the source from which it was set (flag, environment, JSON, YAML, or INI).

License

This package is distributed under the MIT License. See the LICENSE file for more information.

Contributing

Contributions to this package are welcome. If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

Enjoy using the Configurable package in your projects!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Configurable

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

func (*Configurable) Bool

func (c *Configurable) Bool(name string) *bool

func (*Configurable) Duration

func (c *Configurable) Duration(name string) *time.Duration

func (*Configurable) Err

func (c *Configurable) Err() error

func (*Configurable) Float64

func (c *Configurable) Float64(name string) *float64

func (*Configurable) Int

func (c *Configurable) Int(name string) *int

func (*Configurable) Int64

func (c *Configurable) Int64(name string) *int64

func (*Configurable) LoadFile

func (c *Configurable) LoadFile(filename string) error

func (*Configurable) NewBool

func (c *Configurable) NewBool(name string, value bool, usage string) *bool

func (*Configurable) NewDuration

func (c *Configurable) NewDuration(name string, value time.Duration, usage string) *time.Duration

func (*Configurable) NewFloat64

func (c *Configurable) NewFloat64(name string, value float64, usage string) *float64

func (*Configurable) NewInt

func (c *Configurable) NewInt(name string, value int, usage string) *int

func (*Configurable) NewInt64

func (c *Configurable) NewInt64(name string, value int64, usage string) *int64

func (*Configurable) NewString

func (c *Configurable) NewString(name string, value string, usage string) *string

func (*Configurable) Parse

func (c *Configurable) Parse(filename string) error

func (*Configurable) String

func (c *Configurable) String(name string) *string

func (*Configurable) Usage

func (c *Configurable) Usage() string

func (*Configurable) Value

func (c *Configurable) Value(name string) interface{}

type IConfigurable

type IConfigurable interface {
	Int(name string) *int
	NewInt(name string, value int, usage string) *int

	Int64(name string) *int64
	NewInt64(name string, value int64, usage string) *int64

	Float64(name string) *float64
	NewFloat64(name string, value float64, usage string) *float64

	String(name string) *string
	NewString(name, value, usage string) *string

	Bool(name string) *bool
	NewBool(name string, value bool, usage string) *bool

	Duration(name string) *time.Duration
	NewDuration(name string, value time.Duration, usage string) *time.Duration

	LoadFile(filename string) error
	Parse(filename string) error

	Usage() string
}

func New

func New() IConfigurable

Jump to

Keyboard shortcuts

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