env2config

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

env2config

Convert environment variables to configuration files. Great for containerizing existing projects that rely on config files.

Some projects just don't support environment variables out of the box. env2config can easily be copied in and inserted into the container's ENTRYPOINT for simple conversion of environment variables to various config file formats.

Getting Started

To get started, just add these lines to your Dockerfile:

COPY --from=johnstarich/env2config:latest /env2config /
ENTRYPOINT ["/env2config"]

If you already have an entrypoint, inject env2config just before the first arg: ENTRYPOINT ["/env2config", "/myentrypoint.sh"]

Then add your configs, for example:

# Comma separated config names
ENV E2C_CONFIGS=myconf,other

# <name>_OPTS_<setting> are generation settings for this config.
# The FILE and FORMAT opts are required, TEMPLATE is optional.
# Supported formats: yaml, json, toml, ini
ENV MYCONF_OPTS_FILE=/output/my-config.yaml
ENV MYCONF_OPTS_FORMAT=yaml
# <name>_<key> are mappings from config file keys to environment variables.
ENV MYCONF_bind_url=http://example.com
ENV MYCONF_db.address=db.example.com

ENV OTHER_OPTS_FILE=/output/other.yaml
ENV OTHER_OPTS_FORMAT=yaml
ENV OTHER_addresses.0=http://replica0.example.com
ENV OTHER_addresses.1=http://replica1.example.com

At runtime, env2config will generate the above configuration with 2 new yaml files from environment variables and output it to /output/my-config.yaml and /output/other.yaml.

/output/my-config.yaml:

bind_url: http://example.com
db:
  address: db.example.com

/output/other.yaml:

addresses:
    - http://replica0.example.com
    - http://replica1.example.com

To require an environment variable with a custom source, use the pattern <name>_OPTS_IN_<key>=<env>. For example, MYCONF_OPTS_IN_url=BIND_URL will require the $BIND_URL variable, then set it in the myconf config as url.

Projects using env2config

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFormat

func RegisterFormat(format string, marshaler Marshaler)

Types

type Config

type Config struct {
	Name   string
	Opts   Opts // NAME_OPTS_*
	Values Values
	// contains filtered or unexported fields
}

func New

func New(name string) (Config, error)

func (Config) Write

func (c Config) Write() error

type Marshaler

type Marshaler interface {
	Marshal(io.Writer, interface{}) error
}

Marshaler serializes the given value and writes it to the writer

type Opts

type Opts struct {
	File               string   `required:"true"`
	Format             string   `required:"true"`
	TemplateFile       string   `split_words:"true"`
	TemplateDeleteKeys []string `split_words:"true"`

	Inputs Values // NAME_OPTS_IN_*
}

type Unmarshaler added in v0.1.1

type Unmarshaler interface {
	Unmarshal(io.Reader, interface{}) error
}

Unmarshaler deserializes contents of the reader into the provided value pointer. For a value to be mergeable, it must be of type map[string]interface{} or []interface{}.

type Values

type Values map[string]string

func (Values) Set

func (v Values) Set(value string) error

Directories

Path Synopsis
cmd
ini

Jump to

Keyboard shortcuts

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