Git Product home page Git Product logo

rconfig's Introduction

Go Report Card Documentation

Description

Package rconfig implements a CLI configuration reader with struct-embedded defaults, environment variables and posix compatible flag parsing using the pflag library.

Installation

Install by running:

go get -u github.com/Luzifer/rconfig/v2

Run tests by running in the checkout folder:

go test -v -race -cover ./...

Usage

A very simple usecase is to just configure a struct inside the vars section of your main.go and to parse the commandline flags from the main() function:

package main

import (
  "fmt"
  "github.com/Luzifer/rconfig/v2"
)

var (
  cfg = struct {
    Username string `default:"unknown" flag:"user" description:"Your name"`
    Details  struct {
      Age int `default:"25" flag:"age" env:"age" description:"Your age"`
    }
  }{}
)

func main() {
  rconfig.Parse(&cfg)

  fmt.Printf("Hello %s, happy birthday for your %dth birthday.",
    cfg.Username,
    cfg.Details.Age)
}

Provide variable defaults by using a file

Given you have a file ~/.myapp.yml containing some secrets or usernames (for the example below username is assumed to be "luzifer") as a default configuration for your application you can use this source code to load the defaults from that file using the vardefault tag in your configuration struct.

The order of the directives (lower number = higher precedence):

  1. Flags provided in command line
  2. Environment variables
  3. Variable defaults (vardefault tag in the struct)
  4. default tag in the struct
var cfg = struct {
  Username string `vardefault:"username" flag:"username" description:"Your username"`
}

func main() {
  rconfig.SetVariableDefaults(rconfig.VarDefaultsFromYAMLFile("~/.myapp.yml"))
  rconfig.Parse(&cfg)

  fmt.Printf("Username = %s", cfg.Username)
  // Output: Username = luzifer
}

More info

You can see the full reference documentation of the rconfig package at pkg.go.dev

rconfig's People

Contributors

luzifer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rconfig's Issues

Error while go getting

I'm not sure whether this is actually an issue of this project or if I'm doing something wrong. Since go get returns with exit code 1, I'm opening an issue for this:

$ go get gopkg.in/luzifer/rconfig.v1
package gopkg.in/luzifer/rconfig.v1: code in directory /Users/seiffert/Projects/src/gopkg.in/luzifer/rconfig.v1 expects import "github.com/Luzifer/rconfig"
$ echo $?
1

For me, it looks like there's an issue with the annotated canonical import path github.com/Luzifer/rconfig. Is it possible that the usage of gopgk requires packages to either not declare a custom import path or use the gopkg.in/... one?

no way to run go test -v

when running go test with the verbose flag -v, it's trying to parse this unknown option and raises an error. is there some known workaround for this? or some way to configure it to be more forgiving?

DB_PASSWORD=jimdo go test -v app/*.go
=== RUN TestPresign
unknown shorthand flag: 't' in -test.v=true
Usage of /var/folders/vg/_nm_gr392nzcd6cvcs_dnz1w0000gn/T/go-build663204920/command-line-arguments/_test/app.test:
      --db-database="xyz": The name of the MYSQL database
      [...]
exit status 2

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.