Coder Social home page Coder Social logo

copperhead's Introduction

Copperhead

A wrapper around spf13/viper and spf13/pflag with opinionted interface.

go.dev reference

Installation

Install by running:

go get -u github.com/vanhtuan0409/copperhead

Usage

Copperhead support unmarshal struct using values from

  • Command line args
  • Environment variables
  • Config file

Example usage:

package main

import (
	"encoding/json"
	"fmt"
	"reflect"
	"time"

	"github.com/vanhtuan0409/copperhead"
)

type Config struct {
	HttpPort int           `mapstructure:"http_port" cli:"port" default:"8080" description:"HTTP binding port"`
	Timeout  time.Duration `mapstructure:"timeout" default:"5s" description:"HTTP request timeout"`
}

func (c *Config) String() string {
	s, _ := json.MarshalIndent(c, "", "\t")
	return string(s)
}

func main() {
	cfg := Config{}
	copperhead.Unmarshal(&cfg, reflect.TypeOf(cfg), copperhead.ConfigOptions{})
	fmt.Println(cfg.String())
}

CLI Help:

$ go run main.go -h
Usage of /tmp/go-build191072962/b001/exe/main:
      --config string    Path to config file (default "config.yaml")
      --port string      HTTP binding port (default "8080")
      --timeout string   HTTP request timeout (default "5s")
pflag: help requested
exit status 2

Config

Copperhead provide 3 struct tags:

  • mapstructure: key for configuration. Example: mapstructure:"http_port" will be intepreted as env var HTTP_PORT and cli args --http-port
    • Enviroment variable will be translated as upper case
    • CLI args will be translated as strings.ReplaceAll(keyPath, "_", "-")
  • default: default value of this field. If cannot be coerce to target type, copperhead will panic
  • cli: name of CLI args. If not defined, CLI args will be intepreted from mapstructure
  • description: CLI args description

Nested struct

Nested struct will be key path will be intepreted with . for cli args and __ for env var

Example:

type Config struct {
	NestedStruct struct {
		MyNumber int `mapstructure:"my_number" default:"10"`
	} `mapstructure:"nested_struct"`
}

Will be intepreted as:

# Yaml file
nested_struct:
  my_number: 10
$ go run main.go -h
Usage of /tmp/go-build793598133/b001/exe/main:
      --config string                    Path to config file (default "config.yaml")
      --nested-struct.my-number string    (default "10")
pflag: help requested
exit status 2

And environment variable NESTED_STRUCT__MY_NUMBER

copperhead's People

Contributors

vanhtuan0409 avatar

Stargazers

 avatar  avatar

Watchers

 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.