config

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config provides the configuration for the application.

Index

Constants

This section is empty.

Variables

View Source
var (
	Version   string
	Commit    string
	BuildTime = time.Now().Format("2006-01-02T15:04:05.000")
	BuiltBy   = "local"
	BuiltWith = runtime.Version()
)

These variables are set at build time and describe the version and build of the application

View Source
var (
	// ErrFieldNotFound is returned when a field is not found.
	ErrFieldNotFound = errors.New("field not found")

	// ErrMsgTypeMismatch is returned when the type of the default value, example value, or flag type does not match.
	ErrMsgTypeMismatch = "type mismatch for config field %s, flag type is '%s', default value type is '%s', example value type is '%s'"

	// ErrMsgUnsupportedType is returned when a type is not supported.
	ErrMsgUnsupportedType = "unsupported type %s for config flag %s, need to add support for this type in bindFlag"

	// ErrMsgDefaultNil is returned when a required field has no default value.
	ErrMsgDefaultNil = "default value is nil for required config field %s, need to set a default value"

	// ErrMsgFlagEmpty is returned when a field has no flag.
	ErrMsgFlagEmpty = "flag is empty for config field %s, need to set a flag"

	// ErrMsgEnvVarEmpty is returned when a field has no env var.
	ErrMsgEnvVarEmpty = "env var is empty for config field %s, need to set an env var"

	// ErrMsgTypeEmpty is returned when a field has no type.
	ErrMsgTypeEmpty = "type is empty for config field %s, need to set a type"
)
View Source
var (
	// Fields is a list of all configuration fields.
	Fields = append(coreFields, append(githubFields, append(trunkFields, jiraFields...)...)...)
)

Functions

func BindConfig

func BindConfig(cmd *cobra.Command, v *viper.Viper) error

BindConfig binds the configuration to command flags and viper env vars.

func GetDefault

func GetDefault[T any](flag string) (T, error)

GetDefault returns the default value for a configuration field by name.

func MustBindConfig

func MustBindConfig(cmd *cobra.Command, v *viper.Viper)

MustBindConfig is BindConfig but panics if there is an error.

func VersionString

func VersionString() string

VersionString gives a full string of the version of the application.

Types

type Config

type Config struct {
	LogLevel string `mapstructure:"LOG_LEVEL"`
	LogPath  string `mapstructure:"LOG_PATH"`
	Port     int    `mapstructure:"PORT"`

	// Secrets
	GitHub GitHub `mapstructure:",squash"`
	Trunk  Trunk  `mapstructure:",squash"`
	Jira   Jira   `mapstructure:",squash"`
}

Config is the application configuration, set by flags, then by environment variables.

func Load

func Load(options ...Option) (Config, error)

Load loads config from environment variables and flags.

func MustLoad

func MustLoad(options ...Option) Config

MustLoad is Load but panics if there is an error.

type Field

type Field struct {
	// EnvVar is the environment variable name. It is also the key in viper.
	EnvVar      string
	Description string
	Flag        string
	ShortFlag   string
	Type        reflect.Type
	Default     any
	Example     any
	Persistent  bool
	Required    bool
}

Field represents a configuration field.

func GetField

func GetField(flag string) (Field, error)

GetField returns a configuration field by name.

type GitHub

type GitHub struct {
	Token   string `mapstructure:"GITHUB_TOKEN"`
	BaseURL string `mapstructure:"GITHUB_BASE_URL"`
	// GitHub App configuration
	AppID          string `mapstructure:"GITHUB_APP_ID"`
	PrivateKey     string `mapstructure:"GITHUB_PRIVATE_KEY"`
	PrivateKeyFile string `mapstructure:"GITHUB_PRIVATE_KEY_FILE"`
	InstallationID string `mapstructure:"GITHUB_INSTALLATION_ID"`
}

GitHub configures authentication to the GitHub API.

type Jira

type Jira struct {
	BaseDomain        string `mapstructure:"JIRA_BASE_DOMAIN"`
	ProjectKey        string `mapstructure:"JIRA_PROJECT_KEY"`
	OAuthClientID     string `mapstructure:"JIRA_OAUTH_CLIENT_ID"`
	OAuthClientSecret string `mapstructure:"JIRA_OAUTH_CLIENT_SECRET"`
	OAuthAccessToken  string `mapstructure:"JIRA_OAUTH_ACCESS_TOKEN"`
	OAuthRefreshToken string `mapstructure:"JIRA_OAUTH_REFRESH_TOKEN"`
	Username          string `mapstructure:"JIRA_USERNAME"`
	Token             string `mapstructure:"JIRA_TOKEN"`
}

Jira configures authentication to the Jira API.

type Option

type Option func(*configOptions)

Option is a function that can be used to configure loading the config.

func WithCommand

func WithCommand(cmd *cobra.Command) Option

WithCommand sets the command to use for binding flags to config values.

func WithConfigFile

func WithConfigFile(configFile string) Option

WithConfigFile sets the exact config file to load.

func WithViper

func WithViper(v *viper.Viper) Option

WithViper sets the viper instance to use. A new viper instance is created if not provided.

type Trunk

type Trunk struct {
	Token         string `mapstructure:"TRUNK_TOKEN"`
	WebhookSecret string `mapstructure:"TRUNK_WEBHOOK_SECRET"`
}

Trunk configures authentication to the Trunk API.

Jump to

Keyboard shortcuts

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