Coder Social home page Coder Social logo

go-config-parser's Introduction

go-config-parser Go Reference

go-config-parser package provides a simple parser for reading/writing .ini configuration files.

Features

This package supports reading/writing the .ini files as well as:

  • Reading/writing duplicate section names (ex: MySQL NDB engine's config.ini)
  • Options without values (ex: can be used to group a set of hostnames)
  • Options without a named section (ex: a simple option=value file)
  • Find sections with regexp pattern matching on section names, ex: dc1.east.webservers where regexp is .webservers
  • # or ; as comment delimiter
  • = or : as value delimiter

Example

package example

import (
	"fmt"
	"log"
	"testing"

	"github.com/teocci/go-config-parser/core/parser"
)

const (
	configFileName      = "config.ini"
	hostnameOption      = "HostName"
	sectionNameWB       = "dc1.webservers"
	sectionNameMYSQLD   = "MYSQLD DEFAULT"
	sectionNameToDelete = "NDB_MGMD DEFAULT"
	sectionNameRegexp   = ".webservers$"
)

// TestExample Example Read and modify a configuration file
func TestExample(t *testing.T) {
	parser.SetDelimiter(parser.EqualDelimiter) // default delimiter

	config, err := parser.Read(configFileName)
	if err != nil {
		log.Fatal(err)
	}
	// Print the full configuration
	fmt.Println(config)

	// get a section
	section, err := config.Section(sectionNameMYSQLD)
	if err != nil {
		log.Fatal(err)
	} else {
		fmt.Printf("TotalSendBufferMemory=%s\n", section.ValueOf("TotalSendBufferMemory"))

		// set new value
		var oldValue = section.SetValueFor("TotalSendBufferMemory", "256M")
		fmt.Printf("TotalSendBufferMemory=%s, old value=%s\n", section.ValueOf("TotalSendBufferMemory"), oldValue)

		// delete option
		oldValue = section.Delete("DefaultOperationRedoProblemAction")
		fmt.Println("Deleted DefaultOperationRedoProblemAction: " + oldValue)

		// add new options
		section.Add("innodb_buffer_pool_size", "64G")
		section.Add("innodb_buffer_pool_instances", "8")
	}

	// add a new section and options
	section = config.NewSection("NDBD MGM")
	section.Add("NodeId", "2")
	section.Add("HostName", "10.10.10.10")
	section.Add("PortNumber", "1186")
	section.Add("ArbitrationRank", "1")

	// find all sections ending with .webservers
	sections, err := config.Find(sectionNameRegexp)
	if err != nil {
		log.Fatal(err)
	}
	for _, section := range sections {
		fmt.Print(section)
	}
	// or
	config.PrintSection(sectionNameWB)

	sections, err = config.Delete(sectionNameToDelete)
	if err != nil {
		log.Fatal(err)
	}
	// deleted sections
	for _, section := range sections {
		fmt.Print(section)
	}

	options := section.Options()
	fmt.Println(options[hostnameOption])

	// save the new config. the original will be renamed to /etc/config.ini
	err = parser.Save(config, configFileName)
	if err != nil {
		log.Fatal(err)
	}
}

go-config-parser's People

Contributors

teocci avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.