landonia / simplegoconfiguration

A package for reading and writing basic json configuration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

simplegoconfiguration

A simple configuration helper written using the Go Language.

Overview

The Simple Configuration makes reading and writing JSON configuration files easy. It is able to create configurations and save them to disk or read an existing configuration from disk. It makes handling configurations extremely easy. Think properties in Java.

It was originally written for the simplegoblog project but as it is extremely useful to other projects I extracted it into an independent project.

Installation

With a healthy Go Language installed, simply run go get github.com/landonia/simplegoconfiguration/configuration

Example

package main

import (
	"github.com/landonia/simplegoconfiguration/configuration"
)

func main() {	
	// -------- IN MEMORY STORE --------------------
	
	// Create an empty configuration
	configuration := configuration.Empty()
	configuration.SetString("assetsdir", "/var/www/website/assets")
	configuration.SetInt("count", 10)
	
	// ------- READ FROM FILE ----------------------
	
	// Create new configuration from a file
	configuration := configuration.NewFromFile("../config.json")
	err := configuration.ReadFromDisk()
	if err == nil {
		assetdir := configuration.GetString("assetsdir")
		count := configuration.GetInt("count")
	}
	
	// ------ SAVE TO FILE -------------------------
	
	// Create a configuration from a file
	configuration := configuration.NewFromFile("../config.json")
	
	// Or use an empty one if using an in memory configuration
	configuration := configuration.Empty()
	
	// You can change the location where to save the file
	configuration.UpdateFilePath("../config.json")
	
	// Set some properties
	configuration.SetString("assetsdir", "/var/www/website/assets")
	configuration.SetInt("count", 10)
	
	// Save this configuration to disk
	err := configuration.SaveToDisk()
	if err !=  nil {
		// Handle the error
	}
}

Future

Allow the configuration to be saved to multiple data stores such as disk or database. It should also allow you to specify the configuration type such as JSON or XML.

About

simplegoconfiguration was written by Landon Wainwright | GitHub.

Follow me on Twitter @landotube! Although I don't really tweet much tbh.

About

A package for reading and writing basic json configuration

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Go 100.0%