goconfigparser

package module
v0.0.0-...-05bd887 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 8 Imported by: 79

README

Build Status GoDoc Config File Parser (INI style)

This parser is build as a go equivalent of the Python ConfigParser module and is aimed for maximum compatibility for both the file format and the API. This should make it easy to use existing python style configuration files from go and also ease the porting of existing python code.

Example usage:

package main

import (
	"fmt"

	"github.com/mvo5/goconfigparser"
)

var cfgExample = `[service]
base: something
`

var cfgExample2 = `[service]
base=something
`

func main() {
	cfg := goconfigparser.New()
	cfg.ReadString(cfgExample)
	val, err := cfg.Get("service", "base")
	if err != nil {
		panic(err)
	}
	fmt.Printf("Got value %q\n", val)
}

It implements most of RawConfigParser (i.e. no interpolation) at this point.

Current Limitations:

  • no interpolation
  • no defaults
  • no write support
  • not all API is provided

Documentation

Overview

A go implementation in the spirit of the python ConfigParser

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigParser

type ConfigParser struct {

	// allow data with no ["section"] header via Get("", key)
	AllowNoSectionHeader bool
	// contains filtered or unexported fields
}

func New

func New() (cfg *ConfigParser)

Create a new empty ConfigParser

func (*ConfigParser) Get

func (c *ConfigParser) Get(section, option string) (val string, err error)

Return the option for the given section as string or an error

func (*ConfigParser) Getbool

func (c *ConfigParser) Getbool(section, option string) (val bool, err error)

Return the option for the given section as boolean or an error

func (*ConfigParser) Getfloat

func (c *ConfigParser) Getfloat(section, option string) (val float64, err error)

Return the option for the given section as float or an error

func (*ConfigParser) Getint

func (c *ConfigParser) Getint(section, option string) (val int, err error)

Return the option for the given section as integer or an error

func (*ConfigParser) Options

func (c *ConfigParser) Options(section string) (res []string, err error)

Return a string slice of the options available in the given section

func (*ConfigParser) Read

func (c *ConfigParser) Read(r io.Reader) (err error)

Attempt to parse the given io.Reader as a configuration It may return a error if the reading fails

func (*ConfigParser) ReadFile

func (c *ConfigParser) ReadFile(path string) (err error)

Attempt to parse the given file as a configuration It may return a error if the parsing fails

func (*ConfigParser) ReadString

func (c *ConfigParser) ReadString(s string) (err error)

Attempt to parse the given string as a configuration It may return a error if the parsing fails

func (*ConfigParser) Sections

func (c *ConfigParser) Sections() (res []string)

Return a string slice of the sections available

type NoOptionError

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

func (NoOptionError) Error

func (e NoOptionError) Error() string

type NoSectionError

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

func (NoSectionError) Error

func (e NoSectionError) Error() string

type Section

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

Jump to

Keyboard shortcuts

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