github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/conf/conf.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build !js 14 15 package conf 16 17 import ( 18 "os" 19 "os/user" 20 21 "github.com/spf13/viper" 22 ) 23 24 // Conf represents a loaded configuration. 25 type Conf struct { 26 *viper.Viper 27 } 28 29 // New returns an empty Conf. 30 func New() *Conf { 31 return &Conf{Viper: viper.New()} 32 } 33 34 // Load loads the specified config file. 35 func Load(file string) (*Conf, error) { 36 if file != "" { 37 return load(file) 38 } 39 c, err := load("") 40 if _, ok := err.(viper.ConfigFileNotFoundError); ok { 41 return c, nil 42 } 43 return c, nil 44 } 45 46 func load(file string) (*Conf, error) { 47 v := viper.New() 48 if file == "" { 49 v.SetConfigName("serve") 50 v.SetConfigType("toml") 51 v.AddConfigPath(".") 52 if u, err := user.Current(); err == nil { 53 if u.HomeDir != "" { 54 v.AddConfigPath(u.HomeDir + string(os.PathSeparator) + "kivik/") 55 } 56 } 57 v.AddConfigPath("/etc/kivik/") // TODO: Add explicit support for Windows & MacOS 58 } else { 59 v.SetConfigFile(file) 60 } 61 return &Conf{v}, v.ReadInConfig() 62 }