fortio

package module
v0.0.0-...-826a55a Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2019 License: Apache-2.0 Imports: 16 Imported by: 1

README

Fortio

What is Fortio?

Fortio is a config auto wiring module at CrowdStrike. Fortio means cargo in Greek, configs are our cargo that needs to be loaded and carried along in our services. Fortio is used to auto wire configurations from multiple sources like environment variables, config files, HTTP endpoints etc and follows the precedence order as viper into predefined defined config structs at startup. Fortio makes use of cobra and viper to achieve some of these features and not reinvent the wheel, but Fortio is also designed in such a way that replacing cobra or viper to a different solution should be easier and seamless.

Using Fortio

Get Fortio

go get github.com/CrowdStrike/fortio

Define your config that must be auto wired

import (
	"encoding/json"
	"github.com/CrowdStrike/fortio"
	"gopkg.in/yaml.v2"
)

type ExampleConfig struct {
	Timeout fortio.Duration  `config:"env=TIMEOUT,default=100ms;usage=Timeout for service" json:"timeout"`
	Name    string           `config:"default=;usage=Name of service" json:"name"`
}

// Validates assigned config values
func (ec *ExampleConfig) Validate() error {
	if ec.Timeout.Duration > time.Duration(100) * time.Millisecond {
		return errors.New("Timeout can't be greater than 100ms")
	}
	if ec.Name == "" {
		return errors.New("Name can't be empty")
	}
	return nil
}

// DumpJSON will return JSON marshalled string of config
func (ec *ExampleConfig) DumpJSON() (string, error) {
	v, err := json.Marshal(ec)
	return string(v), err
}

// DumpYAML will return YAML marshalled string of config
func (ec *ExampleConfig) DumpYAML() (string, error) {
	v, err := yaml.Marshal(ec)
	return string(v), err
}

In your main file

import "github.com/CrowdStrike/fortio"

func main() {
	// Initialize empty config as pointer
	config := &ExampleConfig{}
	// Initialize config manager
	cm := fortio.NewConfigManager("fortio-test", "My Fortio example")
	// Pass config pointer to be loaded from env variables
	err := cm.Load(config)
	if err != nil {
		// handle error
	}

	// validate configs loaded
	err = config.Validate()
	if err != nil {
		// handle error
	}
}

Checkout above example from example.go

Contributors

Praveen Bathala

Wes Widner

Contribute

We will be happy to accept contributions, please open a issue first on what you are trying to fix or solve and once you get feedback on it from other team members, go ahead and submit a pull request and we will be happy to take a look at merge it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Log used in the client. Initially its set to an empty logger and must be
	// set with a specific logging implementation in order to log messages
	Log = Logger(EmptyLogger{})

	//LogLevels represents the different supported levels for logging messages
	LogLevels = struct {
		Debug LogLevel
		Info  LogLevel
		Warn  LogLevel
		Error LogLevel
		Fatal LogLevel
		Off   LogLevel
	}{
		LogLevel(0),
		LogLevel(1),
		LogLevel(2),
		LogLevel(3),
		LogLevel(4),
		LogLevel(5),
	}
)

Functions

This section is empty.

Types

type CmdLineConfigLoader

type CmdLineConfigLoader struct {
}

CmdLineConfigLoader is config loader that makes the given config fields available as a command line flags for overwriting.

func (*CmdLineConfigLoader) Load

func (cmd *CmdLineConfigLoader) Load(config Config) error

Load will load the config field values as command line flags

type Config

type Config interface {
	// Validates assigned config values
	Validate() error

	// DumpJSON will return JSON marshalled string of config
	DumpJSON() (string, error)

	// DumpYAML will return YAML marshalled string of config
	DumpYAML() (string, error)
}

Config defines a interface that needs to be implemented by all config struct's to be used with ConfigManager

type ConfigLoader

type ConfigLoader interface {
	// Load takes in a implementation of Config and populates field values
	Load(config Config) error
}

ConfigLoader defines a interface that needs to be implemented by a config loader to be able to plug into config manager

type Duration

type Duration struct {
	time.Duration
}

Duration is wrapper on time.Duration to support via new config structure for auto wiring

func (*Duration) ParseString

func (d *Duration) ParseString(s string) error

func (*Duration) Set

func (d *Duration) Set(s string) error

Set will parse given string as time.Duration and sets to Duration

func (*Duration) String

func (d *Duration) String() string

String return string version of Duration, same as time.Duration

func (*Duration) Type

func (d *Duration) Type() string

Type returns type name

type EmptyLogger

type EmptyLogger struct{}

EmptyLogger does not log any log messages

func (EmptyLogger) Debug

func (l EmptyLogger) Debug(args ...interface{})

Debug will log out the args at the debug level if debug level or lower is set

func (EmptyLogger) Debugf

func (l EmptyLogger) Debugf(format string, args ...interface{})

Debugf will log out the args using the format string at the debug level if debug level or lower is set

func (EmptyLogger) Error

func (l EmptyLogger) Error(args ...interface{})

Error will log out the args at the error level if error level or lower is set

func (EmptyLogger) Errorf

func (l EmptyLogger) Errorf(format string, args ...interface{})

Errorf will log out the args using the format string at the error level if error level or lower is set

func (EmptyLogger) Fatal

func (l EmptyLogger) Fatal(args ...interface{})

Fatal will log out the args at the fatal level if fatal level or lower is set

func (EmptyLogger) Fatalf

func (l EmptyLogger) Fatalf(format string, args ...interface{})

Fatalf will log out the args using the format string at the fatal level if fatal level or lower is set

func (EmptyLogger) Info

func (l EmptyLogger) Info(args ...interface{})

Info will log out the args at the info level if info level or lower is set

func (EmptyLogger) Infof

func (l EmptyLogger) Infof(format string, args ...interface{})

Infof will log out the args using the format string at the info level if info level or lower is set

func (EmptyLogger) Warn

func (l EmptyLogger) Warn(args ...interface{})

Warn will log out the args at the warn level if warn level or lower is set

func (EmptyLogger) Warnf

func (l EmptyLogger) Warnf(format string, args ...interface{})

Warnf will log out the args using the format string at the warn level if warn level or lower is set

type LogLevel

type LogLevel int

LogLevel is a specific level of logging

type Logger

type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

Logger is an interface the provides basic logging functionality that can be implemented to wrap an existing logging framework

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager auto wires the given config pointer with given set of config loaders to NewConfigManager API

func NewConfigManager

func NewConfigManager(appName, description string, configLoaders ...ConfigLoader) *Manager

NewConfigManager returns new instance of ConfigManager

func NewConfigManagerWithRootCmd

func NewConfigManagerWithRootCmd(rootCmd *cobra.Command, configLoaders ...ConfigLoader) *Manager

NewConfigManagerWithRootCmd returns a configManager using the provided rootCmd

func (*Manager) CreateCommandLineFlags

func (cm *Manager) CreateCommandLineFlags(config interface{}) error

CreateCommandLineFlags will create command line flags for given config via Cobra and Viper to support command line overriding of config values

func (*Manager) Load

func (cm *Manager) Load(config Config) error

Load will create command line flags for given config and loads values into it from environment variables

func (*Manager) SetLogger

func (cm *Manager) SetLogger(logger Logger)

SetLogger will set given logger and uses it for logging

type MapObject

type MapObject struct {
	Mapping map[string]interface{}
}

MapObject implements StringParsable and read int a JSON object into map[string]interface{}

func (*MapObject) ParseString

func (jm *MapObject) ParseString(s string) error

ParseString exists to satisfy the StringParsable interface

func (*MapObject) Set

func (jm *MapObject) Set(s string) error

Set will set the JSON/YAML string to MapObject

func (*MapObject) String

func (jm *MapObject) String() string

String returns JSON string of the MapObject

func (*MapObject) Type

func (jm *MapObject) Type() string

Type returns type of the object

type StdLogger

type StdLogger struct {
	// contains filtered or unexported fields
}

StdLogger will write log messages to either stdout or stderr with an appropriate prefix depending on the level of the log message

func NewStdLogger

func NewStdLogger(level LogLevel, flags int) *StdLogger

NewStdLogger creates a new StdLogger using the provided level and flags

func (*StdLogger) Debug

func (l *StdLogger) Debug(args ...interface{})

Debug will log out the args at the debug level if debug level or lower is set

func (*StdLogger) Debugf

func (l *StdLogger) Debugf(format string, args ...interface{})

Debugf will log out the args using the format string at the debug level if debug level or lower is set

func (*StdLogger) Error

func (l *StdLogger) Error(args ...interface{})

Error will log out the args at the error level if error level or lower is set

func (*StdLogger) Errorf

func (l *StdLogger) Errorf(format string, args ...interface{})

Errorf will log out the args using the format string at the error level if error level or lower is set

func (*StdLogger) Fatal

func (l *StdLogger) Fatal(args ...interface{})

Fatal will log out the args at the fatal level if fatal level or lower is set

func (*StdLogger) Fatalf

func (l *StdLogger) Fatalf(format string, args ...interface{})

Fatalf will log out the args using the format string at the fatal level if fatal level or lower is set

func (*StdLogger) Info

func (l *StdLogger) Info(args ...interface{})

Info will log out the args at the info level if info level or lower is set

func (*StdLogger) Infof

func (l *StdLogger) Infof(format string, args ...interface{})

Infof will log out the args using the format string at the info level if info level or lower is set

func (*StdLogger) LogLevel

func (l *StdLogger) LogLevel() LogLevel

LogLevel returns LogLevel of the Logger

func (*StdLogger) SetLogLevel

func (l *StdLogger) SetLogLevel(level LogLevel)

SetLogLevel will set the LogLevel for the logger to the specified LogLevel

func (*StdLogger) Warn

func (l *StdLogger) Warn(args ...interface{})

Warn will log out the args at the warn level if warn level or lower is set

func (*StdLogger) Warnf

func (l *StdLogger) Warnf(format string, args ...interface{})

Warnf will log out the args using the format string at the warn level if warn level or lower is set

type StdinConfigLoader

type StdinConfigLoader struct{}

StdinConfigLoader provides autowiring of config values piped in from stdin

func (*StdinConfigLoader) Load

func (s *StdinConfigLoader) Load(config interface{}) error

Load trigger recursive load of config values from yaml piped to stdin

type StringList

type StringList []string

StringList is of type []string

func (*StringList) Set

func (sl *StringList) Set(s string) error

Set sets the value of the StringList given the argument from cmdline

func (*StringList) String

func (sl *StringList) String() string

String returns the string value of a StringList

func (*StringList) Type

func (sl *StringList) Type() string

Type returns type of StringList

type StringParsable

type StringParsable interface {
	ParseString(string) error
}

StringParsable interface can be implemented by any struct that needs to be loaded as string as is and not go through its fields recursively

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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