1
0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-09-19 08:53:00 +00:00
Package rconfig implements a CLI configuration reader with struct-embedded defaults, environment variables and posix compatible flag parsing
Go to file
Knut Ahlers 4c83f34ecf
README: Fix test instruction
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-09-06 19:06:13 +02:00
.repo-runner.yaml Switch to repo-runner for tests 2021-09-06 18:58:12 +02:00
autoenv_test.go Add AutoEnv feature 2018-08-02 12:02:47 +02:00
autoenv.go Add AutoEnv feature 2018-08-02 12:02:47 +02:00
bool_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
config.go Add support for time.Time flags 2018-09-18 00:19:51 +02:00
duration_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
errors_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
example_test.go Initial version of rconfig 2015-07-12 12:36:18 +02:00
float_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
general_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
go.mod Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
go.sum Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
History.md prepare release v2.4.0 2021-09-06 18:59:45 +02:00
int_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
LICENSE Fix LICENSE file 2018-08-02 09:27:24 +02:00
precedence_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
README.md README: Fix test instruction 2021-09-06 19:06:13 +02:00
slice_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
sub-struct_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
time_test.go Add support for time.Time flags 2018-09-18 00:19:51 +02:00
uint_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00
vardefault_providers.go Added VarDefault functionality 2015-09-08 22:07:57 +02:00
vardefault_test.go Port tests to pure-Go-tests 2021-09-06 18:43:24 +02:00

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