Help us shape the future of Go! Take the 2026 Go Developer Survey

autobind

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 9 Imported by: 3

README

= README for Autobind
Author <jeroen@hierynomus.com>
:source-highlighter: pygments

[![GoDoc](https://godoc.org/github.com/hierynomus/autobind?status.svg)](https://godoc.org/github.com/hierynomus/autobind)

Autobind is a library that can use Golang struct annotations to automatically bind Viper and Cobra flags to struct fields.

It is inspired by the excellent work of @spf13 on Viper and Cobra.

Installation
------------

Installing Autobind is as simple as running:

[source,sh]
----
go get github.com/hierynomus/autobind
----

Usage
-----

[source,go]
----
    package main

    import (
        "github.com/hierynomus/autobind"
        "github.com/spf13/cobra"
        "github.com/spf13/viper"
    )

    type Config struct {
        Host string `cobra:"host" env:"HOST" viper:"host" default:"localhost"`
        Port int    `cobra:"port" env:"PORT" viper:"port" default:"8080"`
    }

    func main() {
        var config Config

        vp := viper.New()
        vp.SetConfigName("config")
        vp.AddConfigPath(".")
        vp.SetConfigType("yaml")
        if err := vp.ReadInConfig(); err != nil {
            if _, ok := err.(viper.ConfigFileNotFoundError); ok {
                logger.Warn().Msg("No config file found... Continuing with defaults")
                // Config file not found; ignore error if desired
            } else {
                fmt.Printf("%s", err)
                os.Exit(1)
            }
        }

        cmd := &cobra.Command{
            Run: func(cmd *cobra.Command, args []string) {
                // Do something with config
            },
        }

        binder := &autobind.Autobinder{
            ConfigObject: &config,
            Viper: vp,
            EnvPrefix: "MYAPP",
        }

        binder.Bind(context.Background(), cmd, []string{})

        cmd.Execute()
    }
----

Configuration
-------------

The following items can be set on the `Autobind` struct:

[options="header"]
|=====================
| Name | Description
| `ConfigObject` | The (pointer to the) struct that should be bound to the flags and environment variables.
| `Viper` | The instantiated Viper instace.
| `UseNesting` | Whether to use nested keys in Viper if encountering a sub-struct in the config object.
| `EnvPrefix` | The prefix to use for environment variables.
| `SetDefaults` | Whether to set the default values on config object.
| `Casters` | A map of custom cast functions to use for specific types.

Documentation

Index

Constants

View Source
const (
	ViperTag = "viper"
	CobraTag = "cobra"
	EnvTag   = "env"
)

Variables

This section is empty.

Functions

func AutoBind

func AutoBind(vp *viper.Viper, cfg interface{}) func(cmd *cobra.Command, args []string) error

Types

type Autobinder added in v0.0.2

type Autobinder struct {
	ConfigObject interface{}
	Viper        *viper.Viper
	UseNesting   bool
	EnvPrefix    string            // Viper prefix for environment variables, viper does not expose this, and because we construct the ENV variables, the prefix isn't set by Viper.
	SetDefaults  bool              // Set default values for Viper keys that are not set
	Casters      map[string]Caster // Custom casters
}

func (*Autobinder) Bind added in v0.0.2

func (b *Autobinder) Bind(ctx context.Context, cmd *cobra.Command, prefix []string)

func (*Autobinder) Cast added in v0.0.8

func (b *Autobinder) Cast(key string, caster Caster)

type Caster added in v0.0.8

type Caster func(interface{}) interface{}

Jump to

Keyboard shortcuts

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