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

Overview

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.

Issues
  • 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
    opened by jpxor 0
  • added ability to set a custom logger

    added ability to set a custom logger

    • gives user more control over the logging,
    • added test for setting logger,
    opened by jpxor 0
Owner
Josh Simonot
Josh Simonot
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

Kailash Nadh 692 Jan 15, 2022
⚙️ 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

Eliaz Bobadilla 7 Nov 11, 2021
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.

krako 1 Jan 14, 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

null 0 Nov 15, 2021
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:

Bogdan Daragan 43 Dec 17, 2021
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

John Barton 4.5k Jan 20, 2022
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

xdrm-brackets 0 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

Patrick Connolly 3 Jul 9, 2021
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

Patrick Connolly 4 Jul 9, 2021
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

INI 2.8k Jan 16, 2022
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

cristaltech 330 Jan 15, 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

Anthony DeDominic 1 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

Kai Dinghofer 2 Dec 26, 2021
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

Tarcisio Gruppi 1 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

GoLobby 238 Jan 12, 2022
go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config

HOCON (Human-Optimized Config Object Notation) Configuration library for working with the Lightbend's HOCON format. HOCON is a human-friendly JSON sup

Gürkan Kaymak 37 Dec 19, 2021
Composable, observable and performant config handling for Go for the distributed processing era

Konfig Composable, observable and performant config handling for Go. Written for larger distributed systems where you may have plenty of configuration

Lalamove 618 Jan 15, 2022
🔥🔥 🌈 Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs.

Viper remote for Nacos Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs. runtime_viper := viper.New(

yoyofxteam 19 Dec 29, 2021
create a bootable disk image from Docker image or a yaml config

docker2boot docker2boot creates a bootable disk from either a Docker image or a config yaml file Features status dns Y cloud-init Y network Y ssh TODO

Bin Chen 6 Dec 21, 2021