Giter Club home page Giter Club logo

gofigure's Introduction

gofigure

Gofigure is an opinionated configuration library for go projects

Install

go get github.com/nanvenomous/gofigure

Example

package main

import (
	"fmt"

	"github.com/nanvenomous/gofigure"
)

// name your project (to be used for directory naming)
const PROJECT_NAME = "my-gofigure-test-project"

// Define the structure of your config
type myGlobalConfigType struct {
	User struct {
		Name  string `yaml:"name"`
		Email string `yaml:"email"`
	} `yaml:"user"`
	VerbosityLevel uint8 `yaml:"verbosity_level"`
}

// Create a config entity, set default values (if desired)
var myGlobalConfig = &myGlobalConfigType{VerbosityLevel: 1}

// Define the explicit interface to your configuration
func SetUsername(unm string) {
	gofigure.CheckErr(gofigure.GlobalConfig) // check for file/parsing errors only when accessing config, or replace with your own check, you do you, boo
	myGlobalConfig.User.Name = unm
}
func Username() string {
	gofigure.CheckErr(gofigure.GlobalConfig)
	return myGlobalConfig.User.Name
}
func VerbosityLevel() uint8 {
	gofigure.CheckErr(gofigure.GlobalConfig)
	return myGlobalConfig.VerbosityLevel
}

func main() {
	var err error
	// initialize files, set defaults
	gofigure.Setup(PROJECT_NAME)
	gofigure.Register(gofigure.GlobalConfig, myGlobalConfig) // gofigure supports Global, Local, and Cache configurations

	// access the config data
	fmt.Println(Username())       // ""
	SetUsername("nanvenomous")    //
	fmt.Println(Username())       // "nanvenomous"
	fmt.Println(VerbosityLevel()) // 1

	// write the config to disk, so you can get it on next program run
	gofigure.WriteAll() // uses go standard lib to coose location by your project name and OS

	// prints the paths to all your registered config files (in case your user is wondering :)
	gofigure.Where() // output depends on your os & your project name

	// remove all configuration files (really nice for uninstall scripts)
	if err = gofigure.RemoveAll(); err != nil {
		panic(err)
	}
}

Why

  • Functional api (so the rest of your project only needs to consume the configuration)
  • Explicit yet flexible set/get methods
    • Handle errors... or don't
    • Return errors or return a zero value
    • Fail due to config only when config is needed
    • Essentially it's up to you
  • Static config
    • In-code, automatic defaults that ship with your binary
    • Config & file creation on start
    • No need to guess if values exist (unless you want to)
  • Convenience methods for anywhere in application lifecycle
    • Write everything to disk
    • List all registered configuration files
    • Remove all configuration files (nice for uninstall scripts)

gofigure's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.