From 3640bcdc46c4700a3f9546cc70e79f8a77c4ed36 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Wed, 5 Jun 2024 09:26:25 +0200 Subject: [PATCH] feat: allow setting options on the global viper instance Signed-off-by: Mark Sagi-Kazar --- viper.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/viper.go b/viper.go index 24e9d68..f5a3163 100644 --- a/viper.go +++ b/viper.go @@ -271,6 +271,23 @@ func NewWithOptions(opts ...Option) *Viper { return v } +// SetOptions sets the options on the global Viper instance. +// +// Be careful when using this function: subsequent calls may override options you set. +// It's always better to use a local Viper instance. +func SetOptions(opts ...Option) { + keyDelim := v.keyDelim + + for _, opt := range opts { + opt.apply(v) + } + + // reset encoding if key delimiter changed + if keyDelim != v.keyDelim { + v.resetEncoding() + } +} + // Reset is intended for testing, will reset all to default settings. // In the public interface for the viper package so applications // can use it in their testing as well.