Super Simple Config: read from json file, overwrite with ENV vars

Go CodeQL

ssconfig

Super Simple Config: read from json file, overwrite with ENV vars.

  • values are read directly into your config struct,
  • field names of the struct match the ENV vars (with optional prefix)
  • single source file
  • full test coverage
  • no dependencies

Example Usage

go get github.com/jpxor/ssconfig
package main

import (
    "github.com/jpxor/ssconfig"
)

type my_config struct {
  PORT        string
  WWW_ROOT    string
  PRIVATE_KEY string
}

func main() {
  var env my_config
  
  // default: load from environment variables matching struct field names
  ssconfig.Load(&env)
 
  // or set optional parameters to:
  err := ssconfig.Set{
    FilePath: "config.json",    // load from config file before env vars,
    EnvPrefix: "MY_APP_",       // set env var name prefix,
    Logger: myLogger,           // use custom log.Logger
  }.Load(&env)

  // any error will contain a list of fields that failed to load
  if err != nil {
    log.Println(err)
  }
  log.Printf("%+v\n", env)
}

Documentation

See the example above, that's 99% what you need to know. The other 1%:

  • ssconfig supports loading all types via "encoding/json" unmarshal. You can use the json struct tags for loading from config file.
  • ssconfig logs to default logger if one is not set. It names the Fields loaded, but not their values (keeps any private api keys or passwords out of the logs).
  • need default values? Then set them before loading; the load will overwrite the existing values.

About

I tried a few different config packages from a 'curated list' and they all had lots of dependencies and features I didn't need, or returned errors I wasn't expecting (config was missing from config file, so it gave up instead of looking in the env vars). So I quickly whipped up a config package that does one simple thing, has no dependencies, and doesn't immediately give up when it can't find the config value for one of the fields.

Similar Resources

App that generates a Matterbridge config file based on channel names.

Matterbridge Auto-Config App that generates a Matterbridge config file based on channel names. This remote configuration file* is generated based on s

Oct 15, 2022

Package ini provides INI file read and write functionality in Go.

INI Package ini provides INI file read and write functionality in Go. Features Load from multiple data sources(file, []byte, io.Reader and io.ReadClos

Dec 29, 2022

Library for setting values to structs' fields from env, flags, files or default tag

Configuration is a library for injecting values recursively into structs - a convenient way of setting up a configuration object. Available features:

Dec 7, 2022

A Go port of Ruby's dotenv library (Loads environment variables from `.env`.)

GoDotEnv A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) From the original Library: Storing configuration in the

Jan 5, 2023

Simple, useful and opinionated config loader.

aconfig Simple, useful and opinionated config loader. Rationale There are many solutions regarding configuration loading in Go. I was looking for a si

Dec 26, 2022

Simple Config Format for Golang.

IndentText Simple Configuration Format that tries to be easy to use and understand at a glance. Unlike other formats, IndentText does not have any typ

Nov 26, 2021

A simple multi-layered config loader for Go. Made for smaller projects. No external dependencies.

config ⚠️ Work in progress! A simple multi-layered config loader for Go. Made for smaller projects. No external dependencies. Installation go get -u g

Dec 26, 2021

Quick and easy way to load config files based on a simple set of rules.

Quick and easy way to load config files based on a simple set of rules.

config Quick and easy way to load config files based on a simple set of rules. Project inspired by https://github.com/lorenwest/node-config Important

Apr 9, 2021

A lightweight yet powerful config package for Go projects

Config GoLobby Config is a lightweight yet powerful config package for Go projects. It takes advantage of env files and OS variables alongside config

Dec 11, 2022
Comments
  • simplified env parsing uing json package

    simplified env parsing uing json package

    • This change cleans up and simplifies both env var parsing and error testing by relying on standard json package to parse env var strings, and return errors,
    • increases the number of supported types (now supports slices, maps, etc) for env vars
    • added test for []int type
Go-config - Config parser for go that supports environment vars and multiple yaml files

go-multiconfig This package is able to parse yaml config files. It supports gett

Jun 23, 2022
Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

genv Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables

Dec 21, 2022
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

koanf (pronounced conf; a play on the Japanese Koan) is a library for reading configuration from different sources in different formats in Go applicat

Jan 8, 2023
⚙️ Dead Simple Config Management, load and persist config without having to think about where and how.

Configo Dead Simple Config Management, load and persist config without having to think about where and how. Install go get github.com/UltiRequiem/conf

Apr 6, 2022
Gonfigure - Read and write config files in go

Gonfigure Reads ini files in golang. Reading INI Files Load file File can be loa

Jan 27, 2022
Tmpl - A tool to apply variables from cli, env, JSON/TOML/YAML files to templates

tmpl allows to apply variables from JSON/TOML/YAML files, environment variables or CLI arguments to template files using Golang text/template and functions from the Sprig project.

Nov 14, 2022
formicidate is a small tool for Go application can update the value of environment variables in a .env file with code

formicidae Update .env files in Go with code. What is fomicidae? formicidate is a small tool for Go application. You can update the value of environme

Jan 23, 2022
A simple tool that utilizes already existing libraries such as joho/godotenv to add .env-files to global path

Go dotenv A simple tool that utilizes already existing libraries such as joho/godotenv to add .env-files to global path. Created as a practical way to

Nov 15, 2021
Little Go tool to infer an uncrustify config file from an expected format

uncrustify-infer Little Go tool to infer an uncrustify config file from an expected format Install This tool relies on an uncrustify executable, you m

Oct 8, 2021
A viewer for creating a one-page app that renders the config file as a webpage.

Matterbridge Config Viewer A viewer for creating a one-page app that renders the config file as a webpage. Local Development npm install npm run-scrip

Jul 9, 2021