Documentation ¶
Index ¶
- Variables
- func AdaptGetter[T any](key string, getter func(v *viper.Viper) func(key string) T, v *Viper) func(key string) T
- type Viper
- func (v *Viper) AllSettings() map[string]any
- func (v *Viper) BindEnv(vars ...string) error
- func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error
- func (v *Viper) Notify(ch chan<- struct{})
- func (v *Viper) RegisterAlias(alias string, key string)
- func (v *Viper) Set(key string, value any)
- func (v *Viper) SetDefault(key string, value any)
- func (v *Viper) SetFs(fs afero.Fs)
- func (v *Viper) Watch(ctx context.Context, static *viper.Viper, minWaitInterval time.Duration) (cancel context.CancelFunc, err error)
- func (v *Viper) WriteConfig() error
Constants ¶
This section is empty.
Variables ¶
var ErrDuplicateWatch = vterrors.New(vtrpc.Code_FAILED_PRECONDITION, "duplicate watch")
ErrDuplicateWatch is returned when Watch is called on a synced Viper which has already started a watch.
Functions ¶
func AdaptGetter ¶
func AdaptGetter[T any](key string, getter func(v *viper.Viper) func(key string) T, v *Viper) func(key string) T
AdaptGetter wraps a get function (matching the signature of viperutil.Options.GetFunc) to be threadsafe with the passed-in synced Viper.
It must be called prior to starting a watch on the synced Viper; it will panic if a watch has already been established.
This function must be called at most once per key; it will panic if attempting to adapt multiple getters for the same key.
Types ¶
type Viper ¶
type Viper struct {
// contains filtered or unexported fields
}
Viper is a wrapper around a pair of viper.Viper instances to provide config- reloading in a threadsafe manner.
It maintains one viper, called "disk", which does the actual config watch and reload (via viper's WatchConfig), and a second viper, called "live", which Values (registered via viperutil.Configure with Dynamic=true) access their settings from. The "live" config only updates after blocking all values from reading in order to swap in the most recently-loaded config from the "disk".
func (*Viper) AllSettings ¶
AllSettings returns the current live settings.
func (*Viper) Notify ¶
func (v *Viper) Notify(ch chan<- struct{})
Notify adds a subscription that this synced viper will attempt to notify on config changes, after the updated config has been copied over from disk to live.
Analogous to signal.Notify, notifications are sent non-blocking, so users should account for this when consuming from the channel they've provided.
This function must be called prior to setting up a Watch; it will panic if a a watch has already been established on this synced Viper.
func (*Viper) RegisterAlias ¶
func (*Viper) SetDefault ¶
func (*Viper) Watch ¶
func (v *Viper) Watch(ctx context.Context, static *viper.Viper, minWaitInterval time.Duration) (cancel context.CancelFunc, err error)
Watch starts watching the config used by the passed-in Viper. Before starting the watch, the synced viper will perform an initial read and load from disk so that the live config is ready for use without requiring an initial config change.
If the given static viper did not load a config file (and is instead relying purely on defaults, flags, and environment variables), then the settings of that viper are merged over, and this synced Viper may be used to set up an actual watch later. Additionally, this starts a background goroutine to persist changes made in-memory back to disk. It returns a cancel func to stop the persist loop, which the caller is responsible for calling during shutdown (see package servenv for an example).
This does two things — one which is a nice-to-have, and another which is necessary for correctness.
1. Writing in-memory changes (which usually occur through a request to a /debug/env endpoint) ensures they are persisted across process restarts. 2. Writing in-memory changes ensures that subsequent modifications to the config file do not clobber those changes. Because viper loads the entire config on-change, rather than an incremental (diff) load, if a user were to edit an unrelated key (keyA) in the file, and we did not persist the in-memory change (keyB), then future calls to keyB.Get() would return the older value.
If this synced viper is already watching a config file, this function returns an ErrDuplicateWatch. Other errors may be returned via underlying viper code to ensure the config file can be read in properly.
func (*Viper) WriteConfig ¶
WriteConfig writes the live viper config back to disk.