cfgparser

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 5 Imported by: 0

README

Go Config Parser

A simple and extensible configuration parser written in Go. This library provides an easy way to read and write configuration files with support for different types including booleans, strings, integers, and floats.

Installation

go get github.com/0mlml/cfgparser

import "github.com/0mlml/cfgparser"

Example Configuration File

[bool]
# Enable debugging mode
debug=false
# Enable auto-restart
auto_restart=true

[string]
# Application name
app_name=MyApp
# Database connection string
db_connection_string=host=localhost;user=user;password=pass;db=mydb

[int]
# Maximum number of retries
max_retries=5
# Port number
port=8080

[float]
# Application version
version=1.2

Usage

Parsing Configuration
config := &cfgparser.Config{}
configPath := "example.cfg"
if err := config.From(configPath); err != nil {
	log.Fatalf("Error parsing config file: %v", err)
}
Writing Configuration
configPath := "example.cfg"
if err := config.To(configPath); err != nil {
	log.Fatalf("Error writing config file: %v", err)
}

Customizing Default Configuration

You can customize the default configuration by calling the SetDefaultConfig function. It will be used to backfill any missing values when parsing a configuration file.

defaultConfig := &cfgparser.Config{}
defaultConfig.Literal(
  map[string]bool{
    "debug":        false,
    "auto_restart": true,
  },
  map[string]string{
    "app_name":             "MyApp",
    "db_connection_string": "host=localhost;user=user;password=pass;db=mydb",
  },
  map[string]int{
    "max_retries": 3,
    "port":        8080,
  },
  map[string]float64{
    "version": 1.0,
  },
)

// Set the default configuration
cfgparser.SetDefaultConfig(defaultConfig)

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultConfig

func SetDefaultConfig(config *Config)

Types

type Config

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

func (*Config) Bool

func (config *Config) Bool(key string) bool

func (*Config) BoolKeys

func (config *Config) BoolKeys() []string

func (*Config) Default

func (config *Config) Default()

func (*Config) Float

func (config *Config) Float(key string) float64

func (*Config) FloatKeys

func (config *Config) FloatKeys() []string

func (*Config) From

func (config *Config) From(filename string) error

func (*Config) Int

func (config *Config) Int(key string) int

func (*Config) IntKeys

func (config *Config) IntKeys() []string

func (*Config) Literal

func (config *Config) Literal(
	boolOptions map[string]bool,
	stringOptions map[string]string,
	intOptions map[string]int,
	floatOptions map[string]float64)

func (*Config) SetBool

func (config *Config) SetBool(key string, value bool)

func (*Config) SetFloat

func (config *Config) SetFloat(key string, value float64)

func (*Config) SetInt

func (config *Config) SetInt(key string, value int)

func (*Config) SetString

func (config *Config) SetString(key string, value string)

func (*Config) String

func (config *Config) String(key string) string

func (*Config) StringKeys

func (config *Config) StringKeys() []string

func (*Config) To

func (config *Config) To(filename string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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