Socket
Book a DemoInstallSign in
Socket

sustainyfacts.dev/anyconfig

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sustainyfacts.dev/anyconfig

Go Modules
Version
v0.1.0
Version published
Created
Source

AnyConfig

With a single line of code, bring flexible configuration for to command line utilities or microservices.

Requires Go 1.22 or newer.

Features

  • Configuration struct: one struct to rule them all: define a single struct to hold all your configuration, its validation and documentation.
  • Environment variables: automatically bind your configuration from environment variables using Envconfig.
  • File configuration: read your configuration from a JSON or YAML file. Useful for command-line utilities that need a persistent configuration.
  • Validation: Define simple validation rules for your configuration using Validator.
  • Defaults: provide clever defaults for your configuration

Notes: this is a very simple wrapper around Envconfig, and most of the features provide this library and json/yaml unmarshallers.

Usage

Simple example

package main

import (
    "log"
    
    "sustainyfacts.dev/anyconfig"
)

func main() {
  var conf struct {
    Port int    `env:"PORT"`
    Host string `env:"USERNAME"`
  }

  if err := anyconfig.Read(&conf); err != nil {
    log.Fatal(err)
  }
}

Complex example

package main

import (
    "log"
    
    "sustainyfacts.dev/anyconfig"
)

// Configuration
type Config struct {
  Server struct {
    Port     int    `env:"PORT" validate:"gte=0"`
    Hostname string `json:"host" yaml:"host" env:"USERNAME" validate:"hostname"`
  } `env:", prefix=SERVER_"`
  Logging struct {
    Environment string `json:"env" yaml:"env" env:"ENV, overwrite" validate:"oneof=prod staging dev"`
    Level       string `env:"LEVEL" validate:"oneof=debug info warn error"`
  } `env:", prefix=LOGGING_"`
}

func main() {
  var conf Config
  if err := anyconfig.Read(&conf, anyconfig.WithFile("conf.yaml")); err != nil {
    log.Fatal(err)
  }
}

Together with the following YAML file:

# This is am example yaml config file
server:
  host: example.com
  port: 8080
logging:
  env: dev
  level: debug

Or with the following Enviroment variables:

SERVER_HOST=example.com
SERVER_PORT=8080
LOGGING_ENV=dev
LOGGING_LEVEL=debug

For the complete reference for "env:" annotations, please refer to the project Envconfig.

License

AnyCache is released under the Apache 2.0 license (see LICENSE)

FAQs

Package last updated on 16 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts