jsonflag

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 8 Imported by: 0

README

jsonflag

Go Report Card GoDoc

Use JSON (json5) configs and environmental variables in conjunction with Go's flag package.

See the godocs for documentation and a working example.

Example

Example config.json5 file:

{
"flag1": "jsonFlag1",
"flag2": "jsonFlag2",
"flag3": 3,  // Comments and trailing commas in json config are recommended.  
}
// Config struct name (tag should) match the json key.  
type Config struct {
	Flag1 string
	Flag2 string
	Flag3 int
}

func main(){
	var config Config
	// `flag` is still from the standard library.
	flag.StringVar(&config.Flag1, "flag1name", "defaultFlag1", "flag1Desc")
	flag.StringVar(&config.Flag2, "flag2name", "defaultFlag2", "flag2Desc")
	flag.IntVar(&config.Flag3, "flag3name", 1, "flag3Desc")

	// Instead of `flag.parse`, use `jsonflag.Parse(&config)`
	// This is the only line that must be different from using `flag` normally.  
	jsonflag.Parse(&config)
}

Environmental variables and cli flags give further flexibility. See the documentation for order of precedence.

`FLAG1=flag1EnvValue go run --flag2=flag2CliValue`

Documentation

Overview

Example

Example prints out values

package main

import (
	"flag"
	"fmt"
)

// Create a new config.
var tc Config
var tc2 Config2

// Config's values must be exported
type Config struct {
	Flag1 string // Set by flag default, json, and CLI - CLI precedence
	Flag2 string // Set by json only - json precedence
	Flag3 string // Set by flag default only - default precedence
	Flag4 string // Set by json and flag default - json precedence
	Flag5 int    // json only int - json precedence
	Flag6 int    `json:"flagsix"` // json with flag default int - json precedence.  Tests also tags.
	Flag7 string // Test environmental variable retrieval and json overwritting.  No default, or CLI. - Env precedence
	Flag8 string // Test environmental value expansion from value in json config.
	Flag9 string // Test expanding the default flag value ($FLAG7) with a variable to an environmental variable."
}

type Config2 struct {
	Flag10 string // Test EnvPrefix.  CLI only.
}

// flags holds all flag definitions for CLI and application set.
// Flag 2 and 5 are missing to test jsons values which will still populate.
func flags() {
	flag.StringVar(&tc.Flag1, "flag1", "defaultFlag1", "flag1Desc")
	flag.StringVar(&tc.Flag3, "flag3", "defaultFlag3", "flag3Desc")
	flag.StringVar(&tc.Flag4, "flag4", "defaultFlag4", "flag4Desc")
	flag.IntVar(&tc.Flag6, "flag6", 1, "flag6Desc") // Set default value to something other than 6 for testing.
	flag.StringVar(&tc.Flag7, "flag7", "defaultFlag7", "Flag7's value comes from environmental variable.")
	flag.StringVar(&tc.Flag8, "flag8", "defaultFlag8", "Flag8 tests environmental expansion.")
	flag.StringVar(&tc.Flag9, "flag9", "$FLAG7", "Flag9's value comes from expanding the default flag value ($FLAG7) with a variable to an environmental variable.")
	Parse(&tc)

	EnvPrefix = "JSONFLAG_"
	flag.StringVar(&tc2.Flag10, "flag10", "", "Flag10 tests prefixing the EnvPrefix to env vars")
	Parse(&tc2)
}

// Example prints out values
func main() {
	fmt.Println(tc)
	fmt.Println(tc2)
}
Output:

{cliFlag1 jsonFlag2 defaultFlag3 jsonFlag4 5 6 FLAG7VALUE Flag8Env FLAG7VALUE}
{FLAG10VALUE}

Index

Examples

Constants

This section is empty.

Variables

View Source
var EnvPrefix = ""

EnvPrefix will be prepended to flag names if set. Example, flag with the name "flag1" and a prefix of "MYAPP_" will become "MYAPP_FLAG1"

View Source
var Path = flag.String("config", "config.json5", "Path to json config file.")

Path defines default path. This will be relative to pwd.

Functions

func Parse

func Parse(c interface{})

Parse reads config file and parses cli flags into c by calling flag.Parse()

Types

This section is empty.

Jump to

Keyboard shortcuts

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