config

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

config

Build Status GoDoc License Go version Go version

After a search for a simple application configuration parser turned up many complex libraries, I decided to write my own.

Features

  • File formats: JSON and YAML
  • Strongly typed configuration data (no key-value maps)
  • Data structure is defined by the application.

Usage

A common use-case for configuration files is for microservice applications. As an example, let's say we need to configure the port number the web service listens on and the connection information for a database. The YAML file could look like this:

---
server:
  port: 8080

database:
  driver:   postgres
  hostname: localhost
  port:     5432
  username: postgres
  password: dummy
  name:     my_database

Now in the Go application a data structure is defined to match:

type AppConfig struct {
    Server struct {
        Port int16
    }
    Database struct {
        Driver   string
        Hostname string
        Port     int16
        Username string
        Password string
        Name     string
    }
}

To ingest the configuration file into the application is two lines of code:

var c = new(AppConfig)
var err = config.FromFile("/path/of/config.yaml", c)

Contributing

  1. Fork it
  2. Create a feature branch (git checkout -b new-feature)
  3. Commit changes (git commit -am "Added new feature xyz")
  4. Push the branch (git push origin new-feature)
  5. Create a new pull request.

Maintainers

License

Copyright 2019 MediaExchange.io

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

The config package provides a simple application configuration system with strongly-typed objects. Configuration is read from either a YAML or JSON file and returned to the application.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromFile

func FromFile(path string, konf interface{}) error

FromFile reads application configuration from a file found at path, then returns the decoded data to the application-supplied object, konf.

Errors reading the file, decoding the file, or if the file is of an unexpected type are returned.

Example

Reads the AppConfig data from config_example.yaml and prints it.

os.Setenv("PORT", "9000")
var c = new(AppConfig)
if err := FromFile("config_example.yaml", c); err != nil {
	fmt.Printf("Error: %v", err)
}

fmt.Printf("%+v\n", *c)
Output:

{Server:{Port:9000} Database:{Driver:postgres Hostname:localhost Port:5432 Username:postgres Password:dummy Name:my_database}}

Types

This section is empty.

Jump to

Keyboard shortcuts

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