Giter Club home page Giter Club logo

config-loader's Introduction

Config Loader

This Golang package helps to load values from configuration files, most specifically json and yaml types. For .env, .properties or toml extensions, please checkout for another great packages to solve your context:

Technologies

  • Golang 1.21

Config Loader

Config Loader are the main structure in the package. It will provide the Parse() function to read values from config file, returning the map[string]interface{}.

type FileProperties struct {
	Name_          string   `json:"name"`
	Type_          string   `json:"type"`
	PathLocations_ []string `json:"path_locations"`
	FinalPath      string   `json:"final_path"`
}
Property Description
Name Name of config file
Type Specifies the type of file (yaml or json)
PathLocations List of paths according to the context (local environment, container, CI/CD etc.)
FinalPath After parse, this properties will have the definitive config file path location

Usage

example.yaml

AppName: "my-app"
Version: "1.0.0"
LogLevel: 0

Basic implementation

import(
	// another imports here
	configloader "github.com/eviccari/config-loader"
)

func main() {
	cl := configLoader.New().
		Name("app").
		FileType(filehandlers.YML).
		PathLocations(".", "./configs"). // assuming that file location is . or ./configs
		Build()

	configs, err := cl.Parse()
	if err != nil {
		panic(err)
	}
	fmt.Println(configs["AppName"])
	fmt.Println(fp.FinalPath)
}

The output:

$ go run main.go 
my-app
./configs

Also, you can bind configs into another more complex struct using golang json package:

example2.yaml

app_name: "my-app"
app_version: "1.0.0"
log_level: 0
api:
	port: 3000
	path: "/"
import(
	// another imports here
	configloader "github.com/eviccari/config-loader"
)

type MyConf struct {
	AppName    string `json:"app_name"`
	AppVersion string `json:"app_version"`
	LogLevel   int    `json:"log_level"`
	API        struct {
		Port int    `json:"port"`
		Path string `json:"path"`
	} `json:"api"`
}

func main() { // Error handling are ignored to simplify, do not that in your app ;)
	cl := configloader.New().
		Name("example2").
		FileType(configloader.YAML).
		PathLocations(".", "./configs-test").
		Build()

	configs, _ := cl.Parse()
	configsAsBytes, _ := json.Marshal(configs)
	myConf := MyConf{}
	json.Unmarshal(configsAsBytes, &myConf)
	fmt.Println(myConf.API.Port)
}

The output:

$ go run main.go
3000

Thank you! Enjoy!

config-loader's People

Contributors

eviccari avatar

Stargazers

 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.