config

package module
v0.0.0-...-76fb5b7 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 5 Imported by: 0

README

Config Module

ci go report codecov PkgGoDev

Configuration module based on Viper.

Installation

go get github.com/ankorstore/yokai/modules/config

Documentation

Configuration files

By default, the module expects configuration files:

  • to be present in the root . or ./configs directories of your project
  • to be named config.{format} (ex: config.yaml, config.json, etc.)
  • to offer env overrides files named config.{env}.{format} based on the env var APP_ENV (ex: config.test.yaml if env var APP_ENV=test)

Also:

  • the config file name and lookup paths can be configured
  • the following configuration files format are supported: JSON, TOML, YAML, HCL, INI, and env file.
Configuration usage

For the following examples, we will be considering those configuration files:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: false
config:
  values:
    string_value: default
    int_value: 0
  placeholder: foo-${BAR}-baz
  substitution: foo

and

# ./configs/config.test.yaml
app:
  env: test
  debug: true
config:
  values:
    string_value: test

and the following *config.Config instance:

package main

import "github.com/ankorstore/yokai/modules/config"

var cfg, _ = config.NewDefaultConfigFactory().Create()

// equivalent to:
var cfg, _ = config.NewDefaultConfigFactory().Create(
  config.WithFileName("config"),          // config files base name
  config.WithFilePaths(".", "./configs"), // config files lookup paths
)
Configuration access

This module offers helper methods, as well as all Viper methods to access configuration values:

package main

import (
  "fmt"

  "github.com/ankorstore/yokai/modules/config"
)

func main() {
  // config
  cfg, _ := config.NewDefaultConfigFactory().Create()

  // helpers
  fmt.Printf("name: %s", cfg.AppName())         // name: app
  fmt.Printf("env: %s", cfg.AppEnv())           // env: dev
  fmt.Printf("version: %s", cfg.AppVersion())   // version: 0.1.0
  fmt.Printf("debug: %v", cfg.AppDebug())       // debug: false

  // others
  fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: default
  fmt.Printf("int_value: %s", cfg.GetInt("config.values.int_value"))          // int_value: 0
}
Configuration dynamic env overrides

This module offers the possibility to override dynamically (by merging) configuration files depending on the env var APP_ENV value.

For example, if APP_ENV=test, the module will use config.yaml values and merge / override them with config.test.yaml values.

package main

import (
  "fmt"
  "os"

  "github.com/ankorstore/yokai/modules/config"
)

func main() {
  // env vars
  os.Setenv("APP_ENV", "test")

  // config
  cfg, _ := config.NewDefaultConfigFactory().Create()

  // helpers
  fmt.Printf("name: %s", cfg.AppName())        // name: app
  fmt.Printf("env: %s", cfg.AppEnv())          // env: test
  fmt.Printf("version: %s", cfg.AppVersion())  // version: 0.1.0
  fmt.Printf("debug: %v", cfg.AppDebug())      // debug: true

  // others
  fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: test
  fmt.Printf("int_value: %s", cfg.GetInt("config.values.int_value"))          // int_value: 0
}

You can use any value for APP_ENV (to allow you to reflect your own envs): for example if APP_ENV=custom, the module will use config.yaml values and override them with config.custom.yaml values (you just need to ensure that config.custom.yaml exists).

Configuration env var placeholders

This module offers the possibility to use placeholders in the config files to reference an env var value, that will be resolved at runtime.

Placeholder pattern: ${ENV_VAR_NAME}.

package main

import (
  "fmt"
  "os"

  "github.com/ankorstore/yokai/modules/config"
)

func main() {
  // env vars
  os.Setenv("BAR", "bar")

  // config
  cfg, _ := config.NewDefaultConfigFactory().Create()

  // env var placeholder value
  fmt.Printf("placeholder: %s", cfg.GetString("config.placeholder")) // placeholder: foo-bar-baz
}
Configuration env var substitution

This module offers the possibility to perform configuration files values substitution from env var values.

For example, if you have a configuration key config.substitution=foo, providing the CONFIG_SUBSTITUTION=bar env var will override the value from foo to bar.

package main

import (
  "fmt"
  "os"

  "github.com/ankorstore/yokai/modules/config"
)

func main() {
  // env vars
  os.Setenv("CONFIG_SUBSTITUTION", "bar")

  // config
  cfg, _ := config.NewDefaultConfigFactory().Create()

  // env var substitution value
  fmt.Printf("substitution: %s", cfg.GetString("config.substitution")) // substitution: bar
}

Documentation

Index

Constants

View Source
const (
	AppEnvProd = "prod" // prod environment
	AppEnvDev  = "dev"  // dev environment
	AppEnvTest = "test" // test environment

	DefaultAppName    = "app"     // default application name
	DefaultAppVersion = "unknown" // default application version
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	*viper.Viper
}

Config allows to access the application configuration, and inherits of all Viper features.

func (*Config) AppDebug

func (c *Config) AppDebug() bool

AppDebug returns if the application debug mode is enabled (from config field app.debug or env var APP_DEBUG).

func (*Config) AppEnv

func (c *Config) AppEnv() string

AppEnv returns the configured application environment (from config field app.env or env var APP_ENV).

func (*Config) AppName

func (c *Config) AppName() string

AppName returns the configured application name (from config field app.name or env var APP_NAME).

func (*Config) AppVersion

func (c *Config) AppVersion() string

AppVersion returns the configured application version (from config field app.version or env var APP_VERSION).

func (*Config) IsDevEnv

func (c *Config) IsDevEnv() bool

IsDevEnv returns if the application is running in dev mode.

func (*Config) IsProdEnv

func (c *Config) IsProdEnv() bool

IsProdEnv returns if the application is running in prod mode.

func (*Config) IsTestEnv

func (c *Config) IsTestEnv() bool

IsTestEnv returns if the application is running in test mode.

type ConfigFactory

type ConfigFactory interface {
	Create(options ...ConfigOption) (*Config, error)
}

ConfigFactory is the interface for Config factories.

func NewDefaultConfigFactory

func NewDefaultConfigFactory() ConfigFactory

NewDefaultConfigFactory returns a DefaultConfigFactory, implementing ConfigFactory.

type ConfigOption

type ConfigOption func(o *Options)

ConfigOption are functional options for the ConfigFactory implementations.

func WithFileName

func WithFileName(n string) ConfigOption

WithFileName is used to specify the file base name (without extension) of the config file to load.

func WithFilePaths

func WithFilePaths(p ...string) ConfigOption

WithFilePaths is used to specify the list of file paths to lookup config files to load.

type DefaultConfigFactory

type DefaultConfigFactory struct{}

DefaultConfigFactory is the default ConfigFactory implementation.

func (*DefaultConfigFactory) Create

func (f *DefaultConfigFactory) Create(options ...ConfigOption) (*Config, error)

Create returns a new Config, and accepts a list of ConfigOption. For example:

var cfg, _ = config.NewDefaultConfigFactory().Create()

is equivalent to:

var cfg, _ = config.NewDefaultConfigFactory().Create(
	config.WithFileName("config"),          // config files base name
	config.WithFilePaths(".", "./configs"), // config files lookup paths
)

type Options

type Options struct {
	FileName  string
	FilePaths []string
}

Options are options for the ConfigFactory implementations.

func DefaultConfigOptions

func DefaultConfigOptions() Options

DefaultConfigOptions are the default options used in the DefaultConfigFactory.

Jump to

Keyboard shortcuts

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