parameters

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 12 Imported by: 0

README

Parameters

The parameters package provides a possibility to define and parse parameters from flags,
environment variables, and the AWS Secrets Manager service.

parameters.Parse() will panic() if required parameters wasn't provided.

Options

The Parameters package can be setup by providing options structure as last parameter.

Example:

parameters.Add("EXAMPLE_SECRET", "", "An example parameter from AWS", true, parameters.Options{SecretsManagerEnable: true})

The options structure has following fields:

type Options struct {
	SecretsManagerEnable bool
	StageSensitive       bool
}
  • SecretsManagerEnable - Searches a parameter value inside AWS Secrets Manager.
  • StageSensitive - Searches a parameter value inside AWS Secrets Manager with a prefix with a value of the STAGE parameter.

Here is an example of searching a parameter value with theStageSensitive option:

parameters.Add("STAGE", "DEV", "A stage parameter.", true)
parameters.AddDatabase("EXAMPLE_DATABASE", database.Database{}, "An example database parameter from AWS", true, parameters.Options{SecretsManagerEnable: true, StageSensitive: true})
myParams := parameters.Parse()
go run main.go --STAGE=DEV
  1. The STAGE value is DEV.
  2. The parameters package will search for EXAMPLE_DATABASE flag and env.
  3. The parameters package will search for EXAMPLE_DATABASE_DEV value inside AWS Secrets Manager.
  4. The value of the EXAMPLE_DATABASE parameter can be found by the EXAMPLE_DATABASE key in both cases.

Examples

All examples are stored inside the /examples/parameters folder.

With AWS Secrets Manager

Path - examples/parameters/with-aws-secretsmanager/main.go

It reads parameters from the flags, environment variables, and the AWS Secrets Manager and prints them to the console.
You should have access to the AWS Secrets Manager and the following secrets should exist:

  • EXAMPLE_SECRET - the plain text secret.
  • EXAMPLE_SECRET_JSON - the key-value pair.

EXAMPLE_SECRET_JSON have the following structure:

{ 
    "title": "some title",   
    "value": "some secret data"  
}  
How to run

You can run the example by following commands:

make run

make run-flags

make run-env

or

go run main.go

go run main.go --STAGE=prod --HOST="some host" --PORT=1234 --DATABASE=database_name --LOCAL=true

STAGE=DEV HOST="some host" PORT=1234 DATABASE="database_name" LOCAL=true go run main.go

Without AWS Secrets Manager

Path - examples/parameters/without-aws-secretsmanager/main.go

It reads parameters from the flags and environment variables and prints them to the console.

How to run

You can run the example by following commands:

make run

make run-flags

make run-env

or

go run main.go

go run main.go --STAGE=prod --HOST="some host" --PORT=1234 --DATABASE=database_name --LOCAL=true

STAGE=DEV HOST="some host" PORT=1234 DATABASE="database_name" LOCAL=true go run main.go

Documentation

Index

Constants

View Source
const AwsRegionSecrets = "AWS-REGION-SECRETS"

AwsRegionSecrets is the constant name of AwsRegionSecrets flag or env variable

View Source
const StageParameter = "STAGE"

StageParameter is the constant name of stage flag or env variable

Variables

This section is empty.

Functions

func Add

func Add(name string, value string, usage string, required bool, options ...Options)

Add is alias for AddString

func AddBool

func AddBool(name string, value bool, usage string, required bool, options ...Options)

AddBool defines a bool Parameter with specified name, default value, and usage string.

func AddDatabase

func AddDatabase(name string, value database.Database, usage string, required bool, options ...Options)

AddDatabase defines a database Parameter with specified name, default value, and usage string.

func AddFloat64

func AddFloat64(name string, value float64, usage string, required bool, options ...Options)

AddFloat64 defines a float64 Parameter with specified name, default value, and usage string.

func AddInt

func AddInt(name string, value int, usage string, required bool, options ...Options)

AddInt defines a int Parameter with specified name, default value, and usage string.

func AddInt64

func AddInt64(name string, value int64, usage string, required bool, options ...Options)

AddInt64 defines a int64 Parameter with specified name, default value, and usage string.

func AddString

func AddString(name string, value string, usage string, required bool, options ...Options)

AddString defines a string Parameter with specified name, default value, and usage string.

func AddUint

func AddUint(name string, value uint, usage string, required bool, options ...Options)

AddUint defines a uint Parameter with specified name, default value, and usage string.

func AddUint64

func AddUint64(name string, value uint64, usage string, required bool, options ...Options)

AddUint64 defines a uint64 Parameter with specified name, default value, and usage string.

func GetCollection

func GetCollection() map[string]Parameter

GetCollection returns a collection of parameters.

func Parsed added in v1.4.0

func Parsed() bool

Parsed Returns true if the parse function was used.

Types

type Options

type Options struct {
	SecretsManagerEnable bool
	StageSensitive       bool
}

Options is a struct defines an options for parameters package SecretsManagerEnable - search a parameter in AWS Secrets Manager StageSensitive - the parameter a stage sensitive e.g: NAME_STAGE, where STAGE is a value of STAGE parameter

type Parameter

type Parameter struct {
	Name         string
	DefaultValue interface{}
	Usage        string
	Required     bool
	Options      Options
	// contains filtered or unexported fields
}

Parameter is a struct defines a Parameter

type Results added in v1.4.0

type Results map[string]interface{}

Results is a structure for parsed parameters.

func GetResults added in v1.4.0

func GetResults() Results

GetResults returns a result that already has been parsed.

func Parse

func Parse() Results

Parse returns map of values of all defined parameters.

func (Results) GetBool added in v1.4.0

func (r Results) GetBool(key string) bool

GetBool returns a bool value from the results structure by key.

func (Results) GetBoolSafe added in v1.4.0

func (r Results) GetBoolSafe(key string) (bool, error)

GetBoolSafe returns a bool value from the results structure by key. Returns an error if the type of value is different than bool.

func (Results) GetDatabase added in v1.4.0

func (r Results) GetDatabase(key string) database.Database

GetDatabase returns a database value from the results structure by key.

func (Results) GetDatabaseSafe added in v1.4.0

func (r Results) GetDatabaseSafe(key string) (database.Database, error)

GetDatabaseSafe returns a database value from the results structure by key. Returns an error if the type of value is different than database.

func (Results) GetFloat64 added in v1.4.0

func (r Results) GetFloat64(key string) float64

GetFloat64 returns a float64 value from the results structure by key.

func (Results) GetFloat64Safe added in v1.4.0

func (r Results) GetFloat64Safe(key string) (float64, error)

GetFloat64Safe returns a float64 value from the results structure by key. Returns an error if the type of value is different than float64.

func (Results) GetInt added in v1.4.0

func (r Results) GetInt(key string) int

GetInt returns an int value from the results structure by key.

func (Results) GetInt64 added in v1.4.0

func (r Results) GetInt64(key string) int64

GetInt64 returns an int64 value from the results structure by key.

func (Results) GetInt64Safe added in v1.4.0

func (r Results) GetInt64Safe(key string) (int64, error)

GetInt64Safe returns an int64 value from the results structure by key. Returns an error if the type of value is different than int64.

func (Results) GetIntSafe added in v1.4.0

func (r Results) GetIntSafe(key string) (int, error)

GetIntSafe returns an int value from the results structure by key. Returns an error if the type of value is different than int.

func (Results) GetString added in v1.4.0

func (r Results) GetString(key string) string

GetString returns a string value from the results structure by key.

func (Results) GetStringSafe added in v1.4.0

func (r Results) GetStringSafe(key string) (string, error)

GetStringSafe returns a string value from the results structure by key. Returns an error if the type of value is different than string.

func (Results) GetUint added in v1.4.0

func (r Results) GetUint(key string) uint

GetUint returns an uint value from the results structure by key.

func (Results) GetUint64 added in v1.4.0

func (r Results) GetUint64(key string) uint64

GetUint64 returns an uint64 value from the results structure by key.

func (Results) GetUint64Safe added in v1.4.0

func (r Results) GetUint64Safe(key string) (uint64, error)

GetUint64Safe returns an uint64 value from the results structure by key. Returns an error if the type of value is different than uint64.

func (Results) GetUintSafe added in v1.4.0

func (r Results) GetUintSafe(key string) (uint, error)

GetUintSafe returns an uint value from the results structure by key. Returns an error if the type of value is different than uint.

Directories

Path Synopsis
Package flags is a wrapper for the default flag package.
Package flags is a wrapper for the default flag package.

Jump to

Keyboard shortcuts

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