configs

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 32 Imported by: 0

README

Go Config Client

This is the config client for Go projects.

  • File-Based Client - Fully Implemented
  • ETCD Client - TBD
  • AWS App Config Client - Fully Implemented

Project Versioning

Go Config Client uses semantic versioning. API should not change between patch and minor releases. New minor versions may add additional features to the API.

Installation

To install Go Config Client package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.13+ is required), then you can use the below Go command to install Go Config Client.
go get github.com/sinhashubham95/go-config-client
  1. Because this is a private repository, you will need to mark this in the Go env variables.
go env -w GOPRIVATE=github.com/sinhashubham95/*
  1. Also, follow this to generate a personal access token and add the following line to your $HOME/.netrc file.
machine github.com login ${USERNAME} password ${PERSONAL_ACCESS_TOKEN}
  1. Import it in your code:
import configs "github.com/sinhashubham95/go-config-client"

Usage

New Client
import configs "github.com/sinhashubham95/go-config-client"

fileBasedClient, _ := configs.New(configs.Options{
    Provider: configs.FileBased,
    Params: map[string]interface{}{
        "configsDirectory": ".",
        "configNames": []string{"configs"},
        "configType": "json",
    },
})

awsAppConfigClient, _ := configs.New(configs.Options{
	Provider: configs.AWSAppConfig,
	Params: map[string]interface{}{
        "id":          "example-go-config-client",
        "region":      os.Getenv("region"),
        "accessKeyId": os.Getenv("accessKeyId"),
        "secretKey":   os.Getenv("secretKey"),
        "app":         "sample",
        "env":         "sample",
        "configType":  "json",
        "configNames": []string{"sample"},
    }
})
Configuring Secrets Manager
  1. Add additional param secretNames as shown below
  2. App Config and SecretsManager will use same access and secret keys hence makes sure necessary access is available
  3. Secrets values are fetched on startup time and cached by library. Restart needed if changing secrets.
  4. Secret data in case of AWS should contain JSON data only.
import configs "github.com/sinhashubham95/go-config-client"

fileBasedClient, _ := configs.New(configs.Options{
    Provider: configs.FileBased,
    Params: map[string]interface{}{
        "configsDirectory": ".",
        "configNames": []string{"configs"},
        "configType": "json",
        "secretsDirectory": ".",
        "secretNames": []string{"secrets"},
        "secretType": "json",
    },
})

awsAppConfigClient, _ := configs.New(configs.Options{
	Provider: configs.AWSAppConfig,
	Params: map[string]interface{}{
        "id":          "example-go-config-client",
        "region":      os.Getenv("region"),
        "accessKeyId": os.Getenv("accessKeyId"),
        "secretKey":   os.Getenv("secretKey"),
        "app":         "sample",
        "env":         "sample",
        "configType":  "json",
        "configNames": []string{"sample"},
        "secretNames": []string{"SecretName"},
    }
})
Getting Secrets Value
  1. New methods have been exposed to fetch secrets GetSecret, GetIntSecret, GetFloatSecret and GetStringSecret.
  2. Secrets values are fetched on startup time and cached by library. Restart needed if changing secrets.
Getting Configs

There are 2 types of methods available.

  1. Plain methods which take the config name and the key.
  2. Methods with default values which take the config name, key and the default value. The default value will be used in case the value is not found in the config mentioned corresponding to the key asked for.
Note

For File Based Config Client, the config name is the name of the file from where the configurations have to be referenced, and the key is the location of the config being fetched from that configuration file.

For AWS App Config Client, the config name is the name of the configuration profile deployed, and the key is the location of the config being fetched from that configuration profile.

Documentation

Index

Constants

View Source
const (
	AppConfigStaticCredentialsMode = iota
	AppConfigSharedCredentialMode
)

credential modes

View Source
const (
	FileBased = iota
	AWSAppConfig
)

These are the providers available

Variables

View Source
var (
	ErrConfigNotAdded = errors.New("config not added")
	ErrSecretNotAdded = errors.New("secret not added")
	ErrKeyNotFound    = errors.New("key not found")
)

generic errors

View Source
var ErrProviderNotSupported = errors.New("provider not supported")

ErrProviderNotSupported is the error used when the provider is not supported

Functions

This section is empty.

Types

type ChangeListener

type ChangeListener func(params ...interface{})

ChangeListener is called whenever any change happens in the config

type Client

type Client interface {
	// AddChangeListener is used to add a listener to the changes happening to the config
	// for which it is added.
	AddChangeListener(config string, listener ChangeListener) error

	// RemoveChangeListener is used to remove the change listener added to a particular config.
	RemoveChangeListener(config string) error

	Get(config, key string) (interface{}, error)
	GetSecret(secret, key string) (interface{}, error)
	GetD(config, key string, defaultValue interface{}) interface{}
	GetInt(config, key string) (int64, error)
	GetIntSecret(secret, key string) (int64, error)
	GetIntD(config, key string, defaultValue int64) int64
	GetFloat(config, key string) (float64, error)
	GetFloatSecret(secret, key string) (float64, error)
	GetFloatD(config, key string, defaultValue float64) float64
	GetString(config, key string) (string, error)
	GetStringD(config, key string, defaultValue string) string
	GetStringSecret(secretName, key string) (string, error)
	GetBool(config, key string) (bool, error)
	GetBoolD(config, key string, defaultValue bool) bool
	GetSlice(config, key string) ([]interface{}, error)
	GetSliceD(config, key string, defaultValue []interface{}) []interface{}
	GetIntSlice(config, key string) ([]int64, error)
	GetIntSliceD(config, key string, defaultValue []int64) []int64
	GetFloatSlice(config, key string) ([]float64, error)
	GetFloatSliceD(config, key string, defaultValue []float64) []float64
	GetStringSlice(config, key string) ([]string, error)
	GetStringSliceD(config, key string, defaultValue []string) []string
	GetBoolSlice(config, key string) ([]bool, error)
	GetBoolSliceD(config, key string, defaultValue []bool) []bool
	GetMap(config, key string) (map[string]interface{}, error)
	GetMapSecret(secret, key string) (map[string]interface{}, error)
	GetMapD(config, key string, defaultValue map[string]interface{}) map[string]interface{}
	GetIntMap(config, key string) (map[string]int64, error)
	GetIntMapD(config, key string, defaultValue map[string]int64) map[string]int64
	GetFloatMap(config, key string) (map[string]float64, error)
	GetFloatMapD(config, key string, defaultValue map[string]float64) map[string]float64
	GetStringMap(config, key string) (map[string]string, error)
	GetStringMapD(config, key string, defaultValue map[string]string) map[string]string
	GetBoolMap(config, key string) (map[string]bool, error)
	GetBoolMapD(config, key string, defaultValue map[string]bool) map[string]bool

	Unmarshal(config, key string, value interface{}) error

	// Close is used to perform any closing actions on the config client
	Close() error

	ListAppConfig() []string
}

Client is the contract that can be used and will be followed by every implementation of the config client

func New

func New(options Options) (Client, error)

New is used to initialise and get the instance of a config client

func NewFromAppID

func NewFromAppID(configAppID string, options Options) (Client, error)

type Options

type Options struct {
	// This is the provider to be used
	Provider int
	// These are the set of parameters required for the initialisation of the chosen parameter
	Params map[string]interface{}
}

Options is the set of configurable parameters required for initialisation of the config client

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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