config

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 6 Imported by: 0

README

Config

License Build Status Go Report Card Go Reference Version

Config is a simple package to read configurations from JSON file, environment and flags.

Installation:

go get -u github.com/catamat/config

Example:

package main

import (
	"log"

	"github.com/catamat/config"
)

type configJSON struct {
	Word   string `json:"word"`
	Number int    `json:"number"`
	Check  bool   `json:"check"`
	Slice  []int  `json:"myslice"`
}

type configEnv struct {
	TmpDir      string `env:"TMPDIR"`
	HOME        string
	Shell       string `env:"SHELL"`
	User        string `env:"USER"`
	GoRoot      string `env:"GOROOT"`
	CgoCflags   string `env:"CGO_CFLAGS"`
	VscodePid   int    `env:"VSCODE_PID"`
	PipeLogging bool   `env:"PIPE_LOGGING"`
	Slice       []int  `env:"MY_SLICE" vsep:":"`
}

type configFlags struct {
	Word   string `flag:"-word"`
	Number int    `flag:"-number"`
	Check  bool   `flag:"-check"`
	Slice  []int  `flag:"-myslice" vsep:","`
}

func main() {
	/*
	    {
	        "word": "TestWord",
	        "number": 123456,
	        "check": true,
	        "myslice": [1,1,2,3,5,8]
	    }
	*/
	log.Println("JSON:")
	cfg1 := configJSON{}
	config.FromJSON(&cfg1, "config.json")
	log.Println(cfg1)

	/*
	    os.Setenv("MY_SLICE", "111:222:333")
	*/
	log.Println("Env:")
	cfg2 := configEnv{}
	config.FromEnv(&cfg2)
	log.Println(cfg2)

	/*
	    ./example -word=TestWord -number=123456 -check=true -myslice=1,1,2,3,5,8
	*/
	log.Println("Flags:")
	cfg3 := configFlags{}
	config.FromFlags(&cfg3)
	log.Println(cfg3)
}

Documentation

Overview

Package config is a simple package to read configurations from JSON file, environment and flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromEnv

func FromEnv(structure interface{}) error

FromEnv loads the configuration vars from the environment and tries to convert them into a structure.

func FromFlags

func FromFlags(structure interface{}) error

FromFlags loads the configuration flags from the command-line and tries to convert them into a structure.

func FromJSON

func FromJSON(structure interface{}, path string) error

FromJSON loads the JSON configuration file from the path and tries to convert it into a structure.

Types

This section is empty.

Jump to

Keyboard shortcuts

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